【发布时间】:2018-10-23 21:48:53
【问题描述】:
我正在尝试每天 12 点通过 crontab 运行以下脚本:
#!/bin/sh
mount -t nfs 10.1.25.7:gadal /mnt/NAS_DFG
echo >> ~/Documents/Crontab_logs/logs.txt
date >> ~/Documents/Crontab_logs/logs.txt
rsync -ar /home /mnt/NAS_DFG/ >> ~/Documents/Crontab_logs/logs.txt 2>&1
unmout /mnt/NAS_DFG
由于它需要在 sudo 中运行,我在 'sudo crontab' 中添加了以下行:
someone@something:~$ sudo crontab -l
# 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
0 12 * * * ~/Documents/Crontab_logs/Making_save.sh
但它没有运行。我提到只是执行脚本虽然:
sudo ~/Documents/Crontab_logs/Making_save.sh
运行良好,只是没有将 rsync 命令的输出写入日志文件。
任何想法出了什么问题?我想我检查了错误的主要来源,即使用 shell,最后留下一个空行等等......
【问题讨论】:
-
将此标记为迁移到 unix.stackexchange.com
-
~从 cron 运行时可能无法正常工作。将其更改为绝对路径。如果您致力于不指定完整路径,则可以使用$HOME变量代替~。请理解$HOME可能是 root 的家,而不是您的家,您的脚本可能仍然找不到。 -
这有一个可疑的荣誉,几乎可以解决您可能遇到的所有问题。值得一提的是,Stack Overflow
crontag info page 提供了相当详尽的故障排除提示列表。
标签: linux ubuntu cron ubuntu-18.04