Python script not executing in crontab

点点圈 提交于 2021-02-08 05:14:43

问题


So I'm trying to have my server execute a Python script every hour.

When I go into the directory and run it with python twitter.py, it works fine.

However, I have this entry in crontab and it doesn't work:

0 * * * * /run/twitterparse/twitter.py > /run/twitterparse

I am trying to have it execute every hour, on the hour.

Here's the output to the syslog:

Aug 5  13:00:01 localhost CRON[11474]: (root) CMD (/run/twitterparse/twitter.py >/run/twitterparse/)

Aug 5  13:00:01 localhost CRON[11473]: (CRON) info (No MTA installed, discarding output)

Now what it should be doing is accessing a database and saving information from the web to that database. The script does that fine when run manually, but not automatically.


回答1:


I fixed my problem by doing this

0 * * * * python /run/twitterparse/twitter.py > /run/twitterparse

and the py file should be executable

chmod +x twitter.py



回答2:


You probably need to add

#!/usr/bin/python

to the beginning of the main script, with the location of YOUR python interpreter.

and change the permissions of the script to executable

chmod +x <main script name .py>

Hope this helps




回答3:


Any output of a cron script that goes to the terminal (i.e. stdio or stderr) is sent to the person who's account the script is registered under.

You can see from the error message that it is trying to send this mail but "No MTA is installed" so it is discarding all the diagnostic messages.

Your first step would be to either fix the problem with the MTA so you can receive the mail or to make sure that stdio and stderr are written to a log file.

The obvious problem that I can see is that the output is being directed to a non-file.

/run/twitterparse/twitter.py > /run/twitterparse

Isn't /run/twitterparse a folder? I guess:

/run/twitterparse/twitter.py > /run/twitterparse/ouput.txt

would solve half the issues..



来源:https://stackoverflow.com/questions/11816746/python-script-not-executing-in-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!