无法在Cron中运行Python脚本
我有一个简单的Python脚本,试图将其设置为cron作业,但它拒绝运行.当我单独运行它时,它确实会运行:
I have a simple Python script that I am trying to setup as a cron job, but it refuses to run. It does run when I run it by itself calling it as:
python script.py
我尝试在crontab中设置环境变量,但无法正常工作.我的crontab看起来像这样:
I have tried setting my evironment variables in the crontab, but I cant get it to work. My crontab looks like this:
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command
SHELL=/bin/bash
PATH=/home/netadmin/bin:/home/net/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/b$
*/2 * * * * PYTHONPATH=/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
对此有任何想法吗?
您可以创建如下所示的shell脚本(在本示例中,我们将其称为foo.sh):
You can create a shell script (we'll call it foo.sh for this example) which would look like this:
#! /bin/bash
/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
您需要使foo.sh可执行,因此您将需要运行以下命令:
You need to make foo.sh executable, so you will need to run the following to do that:
chmod +x /home/net/path-to-script/foo.sh
最后,您可以通过运行以下命令(似乎很熟悉)将shell脚本添加到cron作业中:
Finally, you can add the shell script to a cron job by running this (which you seem familiar with):
crontab -e
添加如下一行:
*/2 * * * * /home/net/path-to-script/foo.sh
那应该做到的,祝你好运!
That should do it, good luck!