简体   繁体   English

如何在Linux crontab中运行python程序?

[英]how to run a python program in linux crontab?

I have added the following line in contab 我在contab中添加了以下行

 1 * * * * /usr/bin/python /home/prkumar/Desktop/python/sample.py

my sample.py file 我的sample.py文件

  text_file = open("sample.log", "a")
  text_file.write("Hi...")
  text_file.write("\n")
  text_file.close()

If I run the python program in terminal it's working fine and also append the text in sample.log file. 如果我在终端中运行python程序,则可以正常工作,并将文本附加到sample.log文件中。 But there is no response if add the program in crontab. 但是,如果在crontab中添加程序,则没有任何响应。

Thanks 谢谢

You need a correction : 您需要更正:

* * * * * /usr/bin/python /home/prkumar/Desktop/python/sample.py

to run it every minute. 每分钟运行一次。

Your cron statement : 1 * * * * /usr/bin/python /home/prkumar/Desktop/python/sample.py 您的cron语句: 1 * * * * /usr/bin/python /home/prkumar/Desktop/python/sample.py

would run it only at first minute of every hour eg 09:01, 10:01...etc. 仅在每小时的第一分钟运行它,例如09:01、10:01 ...等。 Hence, you will have to wait 1 hour to see the result. 因此,您将需要等待1个小时才能看到结果。

更改python而不是/ usr / bin / python,如下所示更改代码。

1 * * * * python /home/prkumar/Desktop/python/sample.py >> /home/prkumar/Desktop/python/output.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM