使用 cron 每 5 分钟后无法 运行 一个 python 脚本
Not able to run a python script after every 5mins using cron
我用 -e 选项编辑了 crontab。然后我去 /etc/cron.d 目录创建一个文件,它将 运行 进程。我再次编辑 /etc/crontab 文件。但是我没能做到运行。我从 Whosebug 中引用了这个 article 并做了完全相同的事情,但我不知道为什么 cron 不适合我?
这是我的 crontab 的样子 -
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * anikde /home/anikde/Documents/pythonProjects/python_scripts/*
* * * * * anikde python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
我已将第 1 个作业设置为 运行 每 5 分钟一次,第 2 个作业每 1 分钟一次。但是 none 的作业会自动 运行ning。当我命令作业 运行 第一个脚本作为 bash 脚本和第二个文件作为 python 脚本时,它们实际上 运行。
我假设您正在尝试每 5 分钟自动 运行 下面的脚本:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
首先,使用 which python
命令确定 Python 可执行文件的位置。在下面的示例中,我假设返回的路径是 /usr/bin/python
.
- 如果编辑您自己的 crontab (
crontab -e
) 试试这个命令:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
如果未指定用户,则作业 运行 作为拥有 crontab 文件的用户,使用他的环境。但您也可以尝试添加用户名
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
- 如果编辑根 crontab (
sudo crontab -e
)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
我用 -e 选项编辑了 crontab。然后我去 /etc/cron.d 目录创建一个文件,它将 运行 进程。我再次编辑 /etc/crontab 文件。但是我没能做到运行。我从 Whosebug 中引用了这个 article 并做了完全相同的事情,但我不知道为什么 cron 不适合我? 这是我的 crontab 的样子 -
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/5 * * * * anikde /home/anikde/Documents/pythonProjects/python_scripts/*
* * * * * anikde python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
我已将第 1 个作业设置为 运行 每 5 分钟一次,第 2 个作业每 1 分钟一次。但是 none 的作业会自动 运行ning。当我命令作业 运行 第一个脚本作为 bash 脚本和第二个文件作为 python 脚本时,它们实际上 运行。
我假设您正在尝试每 5 分钟自动 运行 下面的脚本:
/home/anikde/Documents/pythonProjects/python_scripts/test/write.py
首先,使用 which python
命令确定 Python 可执行文件的位置。在下面的示例中,我假设返回的路径是 /usr/bin/python
.
- 如果编辑您自己的 crontab (
crontab -e
) 试试这个命令:
*/5 * * * * /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
如果未指定用户,则作业 运行 作为拥有 crontab 文件的用户,使用他的环境。但您也可以尝试添加用户名
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py
- 如果编辑根 crontab (
sudo crontab -e
)
*/5 * * * * anikde /usr/bin/python /home/anikde/Documents/pythonProjects/python_scripts/test/write.py