Python脚本不在crontab中执行

时间:2021-07-06 15:29:00

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

所以我试图让我的服务器每小时执行一个Python脚本。

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

当我进入目录并使用python twitter运行它时。py,效果好。

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

但是,我在crontab中有这个条目,它不起作用:

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:

这是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.

现在它应该做的是访问数据库并将信息从web保存到该数据库。当手动运行时,脚本会运行良好,但不会自动运行。

3 个解决方案

#1


2  

I fixed my problem by doing this

我这样做解决了我的问题

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

and the py file should be executable

py文件应该是可执行的

chmod +x twitter.py

#2


1  

You probably need to add

你可能需要加上

#!/usr/bin/python

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

到主脚本的开头,使用python解释器的位置。

and change the permissions of the script to executable

并将脚本的权限更改为可执行文件。

chmod +x <main script name .py>

Hope this helps

希望这有助于

#3


0  

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.

向终端(即stdio或stderr)发送的cron脚本的任何输出都被发送到该脚本被注册的人。

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.

您可以从错误消息中看到,它试图发送此邮件,但“没有安装MTA”,因此它丢弃了所有诊断消息。

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.

您的第一步是修复MTA的问题,以便您可以接收邮件,或者确保stdio和stderr被写入日志文件。

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:

不是/运行/ twitterparse一个文件夹?我猜:

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

would solve half the issues..

可以解决一半的问题。

#1


2  

I fixed my problem by doing this

我这样做解决了我的问题

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

and the py file should be executable

py文件应该是可执行的

chmod +x twitter.py

#2


1  

You probably need to add

你可能需要加上

#!/usr/bin/python

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

到主脚本的开头,使用python解释器的位置。

and change the permissions of the script to executable

并将脚本的权限更改为可执行文件。

chmod +x <main script name .py>

Hope this helps

希望这有助于

#3


0  

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.

向终端(即stdio或stderr)发送的cron脚本的任何输出都被发送到该脚本被注册的人。

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.

您可以从错误消息中看到,它试图发送此邮件,但“没有安装MTA”,因此它丢弃了所有诊断消息。

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.

您的第一步是修复MTA的问题,以便您可以接收邮件,或者确保stdio和stderr被写入日志文件。

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:

不是/运行/ twitterparse一个文件夹?我猜:

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

would solve half the issues..

可以解决一半的问题。