Find the answer to your Linux question:
Results 1 to 7 of 7
Hello, dear community Please help if you know the answer for the following problem: How can i specify custom variables so that they will be recognized by programs which are ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Location
    St. Petersburg, Russia
    Posts
    7

    Program initiated via python's "os.system()" doesn't work (when in crontab script)

    Hello, dear community

    Please help if you know the answer for the following problem: How can i specify custom variables so that they will be recognized by programs which are executed by "os.system()" in python?

    I have a python script which executes several programs via os.system() call. It runs fine when i execute it manually from interactive shell (bash), but when i put it in crontab it some of those programs don't work since they don't find several special environment variables that are present in my .bashrc file.

    Trying to figure the problem out i have put two lines in the beginning of my crontab file, so that it looks like this:

    Code:
    SHELL=/bin/bash
    USER=misha
    */30 * * * * /opt/sevzapmodis/pymodis.py >> /home/misha/pymodis.log
    but it didn't help. Any ideas what could be the solution?

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    show your Python script.

  3. #3
    Just Joined!
    Join Date
    Apr 2007
    Location
    St. Petersburg, Russia
    Posts
    7
    Here is the script:
    Attached Files Attached Files

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i notice you have */30 * * * * in your cron
    change that to

    * * * * *

    and see what happens.

  5. #5
    Just Joined!
    Join Date
    Apr 2007
    Location
    St. Petersburg, Russia
    Posts
    7
    In such way it only runs every minute instead of once per two minutes. Everything goes in the same way.
    I have also tried to play with os.environ[] call:

    Code:
    import os
    logfile = open('/home/misha/pymodis.log','w')
    logfile.write(os.environ['USER']+'\n')
    logfile.write(os.environ['HOME']+'\n')
    logfile.write(os.environ['SEADAS']+'\n') # SEADAS is path to a directory'/etc/Seadas' in my ~/.bashrc
    logfile.write(os.environ['HOME']+'\n')
    At the outuput only two first lines appear.

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i'm not sure really what you want. you can try change "w" to "a" in your open() statement if you need to log to a log file without overwriting the previous entries

  7. #7
    Just Joined!
    Join Date
    Apr 2007
    Location
    St. Petersburg, Russia
    Posts
    7
    I was just trying to see which variables will be recognized, and which won't be. Somehow if i wanted to write to a text file a variable value which is not recognized then everything else after it (like one more time value of a "HOME" variable) is not written as well. Weird, i have to study this behavior more. Or is there any obvious mistake of mine?

    For now i managed with the problem via workaround, assigned all of the variables in the beginning of subscripts called by os.system(). But i still wonder, how can i pass values of some variables to a python script (a crontab entry) instead of writing it to subscripts...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...