Hen*_*ryM 5 permissions scripts cron
我有运行良好的 python 脚本。我想每 10 分钟将它作为 cron 作业运行一次
我已经完成crontab -e并进入了行
*/10 * * * * root python /path/to/script/script.py
Run Code Online (Sandbox Code Playgroud)
它似乎没有运行。
所以我在命令行上玩过,看看它在什么时候停止运行。它在它的目录中运行,并且在它之前的每个目录中运行,直到我移出我的主目录(即,如果我在其中,/home/henry/则它会运行,但如果我在,例如,/var则它不会)当我收到权限错误时试图写入文件。
# save this for next time
with open(mycsvfile, "w") as outfile:
writer = csv.writer(outfile)
writer.writerows([s.strip().encode("utf-8") for s in row ]for row in list_of_rows)
outfile.close()
Run Code Online (Sandbox Code Playgroud)
我已经chmod +x /home/user/Location/Of/Script确保脚本可以访问(我认为)。我错过了什么?谢谢你的帮助
删除字符串root(假设python存在于cron定义中PATH,否则使用绝对路径,例如/usr/bin/python):
*/10 * * * * python /path/to/script/script.py
*/10 * * * * /usr/bin/python /path/to/script/script.py
Run Code Online (Sandbox Code Playgroud)
为什么:
当您使用crontab -e打开 cron 表时,您正在打开调用用户的crontab,不允许使用用户名字段(与 /etc/crontab和不同/etc/cron.d/*)
就目前而言,您正在使用root参数运行命令(可能不可用)python和/path/to/script/script.py
另外,如果您已使脚本可执行,则应该添加一个指示脚本解释器的 shebang(例如/usr/bin/python),而不是将脚本作为解释器的参数运行。然后你可以这样做:
*/10 * * * * /path/to/script/script.py
Run Code Online (Sandbox Code Playgroud)