Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Bash中使用crontab_Python_Bash - Fatal编程技术网

Python 在Bash中使用crontab

Python 在Bash中使用crontab,python,bash,Python,Bash,我希望在Bash中使用crontab来运行python脚本。我下面的东西不起作用 SHELL=/bin/bash 11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py 这也不是: SHELL=/bin/bash PATH=/usr/lib/python2.7 5 22 * * * username python /mnt/c/Users/Eric/Documents/

我希望在Bash中使用crontab来运行python脚本。我下面的东西不起作用

 SHELL=/bin/bash
11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
这也不是:

SHELL=/bin/bash
PATH=/usr/lib/python2.7
5 22 * * * username python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
您需要删除cron命令中的用户名。否则,它将尝试将其作为参数python/mnt/c/Users/Eric/Documents/Feedparser/crontab.py的可执行文件运行

只要说:

5 22 * * * python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py

您可能希望考虑使Python文件可执行并直接调用它。然后,您的crontab会说:

5 22***./mnt/c/Users/Eric/Documents/Feedparser/crontab.py

5 22***cd/mnt/c/Users/Eric/Documents/Feedparser&&./crontab.py

为此,请使文件可执行:

chmod +x /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
并将shebang添加到python文件的第一行:

#!/usr/bin/env python

您可以尝试使用以下方法在crontab中记录命令的输出和执行错误:

11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py > /tmp/crontab.log 2>&1

这可能会让您了解问题所在。

路径应该包括目录,而不是可执行文件。运行该命令不需要用户名。该命令代表保存crontab的用户运行,该用户首先尝试使用一些伪cron作业并尝试获取其日志。。例如,-1****日期>>/tmp/crontest.logIt取决于是否在系统crontab中/etc/crontab;它有用户名字段或每用户crontab,但没有用户名字段。看,我试过使用用户名和不使用用户名。我添加shell和路径是否正确?它真的需要知道python 2.7在哪里吗?