Linux 脚本不会在cron下运行,但会再次手动运行
我很抱歉再次问这个问题,但我已经尝试了所有的建议。我在Linux 脚本不会在cron下运行,但会再次手动运行,linux,shell,cron,Linux,Shell,Cron,我很抱歉再次问这个问题,但我已经尝试了所有的建议。我在/var/TPbackup\u script/中有两个脚本。这是第一个: mysqldump -u root -pPASSWORD teampass > /var/TPbackups/TPbackup_$(date +"%Y-%m-%d").sql /etc/crontab 20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script 此脚本在crontab
/var/TPbackup\u script/
中有两个脚本。这是第一个:
mysqldump -u root -pPASSWORD teampass > /var/TPbackups/TPbackup_$(date +"%Y-%m-%d").sql
/etc/crontab
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
此脚本在crontab中工作。一切都很好。第二个脚本不运行:
s3cmd sync /var/TPbackups s3://PwdMgmt
/etc/crontab
中相应的cronjob:
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script
这个失败了。如果我在终端中手动运行它:
sudo sh /var/TPbackup_script/TPsyncS3_script
然后它就完美地工作了。我尝试的是:
1) 正在尝试添加shebang#/bin/sh到脚本的开头2) 将脚本重命名为TPsyncS3_script.sh
3) 我已经将脚本添加到cron.daily中,并且它在日常cron任务列表中(我通过命令
runparts--test/etc/cron.daily
看到它)没有成功。
这是我的/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
16 9 * * * 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 )
20 9 * * * root sudo sh /var/TPbackup_script/TPbackup_script
25 9 * * * root sudo sh /var/TPbackup_script/TPsyncS3_script.sh > /var/TPbackup_script/sync_log.txt
#
脚本上的所有权限都是通过sudo chmod 777
设置的顺便说一下。sync_log.txt是在cronjob之后创建的,但它是空的。
非常感谢您的帮助我也遇到过类似的问题。尝试使用root的crontab运行脚本。 做: sudo crontab-e
添加脚本并重试。它对我有效:)也有同样的问题。通过添加指定s3cfg位置的选项解决了此问题:
--config /root/.s3cfg
e、 g:
你检查过你的日志文件了吗?
fgrep cron/var/log | fgrep TPsyncS3
说了什么?此外,您实际上不需要sudo
部分,因为您已经将cronjobs设置为以root
身份运行,“s3cmd”在哪里?它在/bin还是/usr/bin中?Cron的路径非常有限。它依赖于在您的环境中找到的变量吗?Glenn,s3cmd位于/usr/bin中。但是我不明白为什么它不起作用,因为我的/etc/crontab文件中有/usr/bin路径(您可以在主消息中看到路径)。而lanzz,我没有任何与s3cmd或/var/log中的TPsyncS3连接的日志。当我从你的答案中输入命令时,什么也没发生。Upd:我已经在脚本中添加了路径参数,其中包含了我用where is s3cmd找到的所有s3cmd位置。现在我的脚本看起来像这样:#/bin/sh PATH=/usr/bin/s3cmd:/usr/bin/X11/s3cmd:/usr/share/s3cmd:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/bin s3cmd sync/var/TPbackups s3://PwdMgmt
但如果手动运行它,它仍然可以工作,但在cron中不工作。
s3cmd sync --config /root/.s3cfg /var/TPbackups s3://PwdMgmt