无法在Cron中运行Python脚本 [英] Unable to Run Python Script In Cron

查看:95
本文介绍了无法在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!

这篇关于无法在Cron中运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆