crontab -e
我已经为当前用户添加了一个 crontab 条目:
@reboot supervisord -c /etc/supervisord.conf
然而重启后,supervisordemon 并没有运行。我需要在 bash 中再次运行该命令。在设置 cronjob 后,只有第二次尝试才会出现我预期的错误。
$ supervisord -c /etc/supervisord.conf
$ supervisord -c /etc/supervisord.conf
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
为什么我的 cronjob 不工作以及如何在启动时启动主管守护进程?
答案1
cron
默认情况下在最小环境中运行,来自man 5 cron
:
Several environment variables are set up automatically by the cron(8)
daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the
/etc/passwd line of the crontab's owner. PATH is set to
"/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings
in the crontab;
您的supervisord
可执行文件可能不在/usr/bin
或中/bin
,因此cron
无法找到它并且无法运行。
一个好的做法,最安全的方法是始终在 cron 条目中使用可执行文件的完整路径,如果您不确定它是否在 cron 的默认路径中。
或者您可以PATH
通过编辑来全局更改 cron /etc/crontab
:
$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#