我正在运行Ubuntu 10.04(64位),并在/etc/cron.hourly中具有以下脚本
cd /home/chris/path/to/directory git add . git commit -m "Commit message" git push origin master
该文件是775,我可以手动运行它运行该脚本,并看到它提交到服务器上的提交。 以下是手动运行的输出:
chris@IronHide:~$ /etc/cron.hourly/auto-commit [master 8dc5299] Commit message 4 files changed, 8 insertions(+), 6 deletions(-) rewrite 1h/1m/c.-b.-6.dat (100%) Counting objects: 17, done. Compressing objects: 100% (7/7), done. Writing objects: 100% (9/9), 4.64 KiB, done. Total 9 (delta 3), reused 0 (delta 0) To <repo_path_cencored>.git 6c6d0ad..8dc5299 master -> master
不知道是否重要,但我的/ 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 # mh 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 ) #
我究竟做错了什么?
它看起来像当你手动运行脚本,你做它的chris 。 那么你为什么要在cron下以root身份运行呢? /etc/crontab (以及扩展名为/etc/cron.*目录)用于系统作业。 要以您的用户身份运行作业,请运行crontab -e并编写一行
01 * * * * cd ~/path/to/directory && ~/bin/auto-commit
这在每小时一分钟的时间内在指定的目录中每小时运行~/bin/auto-commit 。