Python脚本通过Cron不工作?

2 投票
1 回答
1521 浏览
提问于 2025-04-16 08:46

我有一个Python脚本,它会运行另一个Python脚本。那个“其他脚本”(被运行的脚本)大约需要45分钟才能完成。

当我从Ubuntu的命令行运行这个“执行”脚本时,一切都运行得很好。

我在cron中添加了一个条目来运行这个“执行”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  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

你知道我可能遗漏了什么吗?我尝试用一个简单的脚本替换被“执行”脚本触发的脚本,这个简单脚本只做以下事情:

while(1):

....print "hi"

但它还是不工作。

有什么想法吗?谢谢。

(我使用的是Ubuntu 10.04 LTS)

1 个回答

8

在使用cron运行任务时,通常会有一些常见的误解。

  • 没有会话。如果你的代码需要会话,你需要额外的设置。
  • PATH和其他环境变量在你的用户会话和cron执行环境中几乎总是不同的。

当cron出错时,它默认会尝试发送邮件。你可以查看本地邮箱或队列(如果你的邮件传输代理出现故障)来查找错误信息。

撰写回答