Python Script Not Working Via Cron?

2020-04-16 19:35发布

问题:

I have a python script that runs another python script. that 'other script' (script being run) takes ~ 45[min] to complete.

When the 'execution' script is run from Ubuntu's shell, it all works perfectly.

I added an entry to cron to run the 'execution' python script. It seems to run at first, but the program is terminated after a few seconds:


user@internet:~/REMOTE_CONTROL$ ps aux | grep python
user      1603  0.0  1.4  31504 15184 ?        S    22:50   0:00 python /usr/share/system-config-printer/applet.py

root      1714  0.0  0.7  13672  8012 ?        S    22:51   0:00 /usr/bin/python /usr/lib/system-service/system-service-d

**user      7956  109  1.0  14380 10692 ?        R    23:48   0:01 python /media/READ_ONLY/DEVELOPER/COMPOSITE/CURRENT/DataLoader.py**

user      8029  0.0  0.0   3320   788 pts/1    S+   23:48   0:00 grep --color=auto python
user@internet:~/REMOTE_CONTROL$ ps aux | grep python

user      1603  0.0  1.4  31504 15184 ?        S    22:50   0:00 python /usr/share/system-config-printer/applet.py

root      1714  0.0  0.7  13672  8012 ?        S    22:51   0:00 /usr/bin/python /usr/lib/system-service/system-service-d

**user      7956  100  1.1  15448 11900 ?        R    23:48   0:07 python /media/READ_ONLY/DEVELOPER/COMPOSITE/CURRENT/DataLoader.py**

user      8033  0.0  0.0   3320   788 pts/1    S+   23:48   0:00 grep --color=auto python
user@internet:~/REMOTE_CONTROL$ ps aux | grep python

user      1603  0.0  1.4  31504 15184 ?        S    22:50   0:00 python /usr/share/system-config-printer/applet.py

root      1714  0.0  0.7  13672  8012 ?        S    22:51   0:00 /usr/bin/python /usr/lib/system-service/system-service-d

**user      7956 97.2  1.2  15992 12412 ?        R    23:48   0:11 python /media/READ_ONLY/DEVELOPER/COMPOSITE/CURRENT/DataLoader.py**

user      8035  0.0  0.0   3320   792 pts/1    S+   23:48   0:00 grep --color=auto python
user@internet:~/REMOTE_CONTROL$ ps aux | grep python

user      1603  0.0  1.4  31504 15184 ?        S    22:50   0:00 python /usr/share/system-config-printer/applet.py

root      1714  0.0  0.7  13672  8012 ?        S    22:51   0:00 /usr/bin/python /usr/lib/system-service/system-service-d

user      8037  0.0  0.0   3320   792 pts/1    S+   23:48   0:00 grep --color=auto python

any idea what I might be missing? I tried to replace the script that is being triggered by the 'execution' script, and have it call a simple script that just does the following:

while(1):

....print "hi"

and it still does not work.

Any ideas? thanks.

(I am using Ubuntu 10.04 LTS)

回答1:

There are usually a few common misunderstandings made when trying to run things from cron.

  • There is no session. If you have code that needs a session you have to do additional setup.
  • The PATH and other environment variables are almost always different between your user session and the cron execution environment.

When cron fails it will by default attempt to send mail. You can look in your local mail box or queue (if your mta is down) for errors.