Crontab 不启动脚本
Crontab not launching script
我正在尝试每天 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、在末尾留空行等...
sudo crontab
创建一个作业 运行 在 root
的 crontab 之外(如果你设法正确配置它;root
crontabs 中的语法是不同的).当 cron
运行 是工作时,$HOME
(和 ~
如果您使用 shell 或带有波浪线扩展的脚本语言)将引用 root
等
你应该简单地添加
0 12 * * * sudo ./Documents/Crontab_logs/Making_save.sh
改为您自己的crontab
。
请注意 crontab
根本没有波浪号扩展(但我们可以相信 cron
将始终 运行 在您的主目录之外)。
...虽然这仍然会有问题,因为如果脚本 运行s 在 sudo
下并创建新文件,这些文件将归 root
所有,并且您的普通用户帐户无法更改。更好的解决方案仍然是仅 运行 实际的 mount
和 umount
命令与 sudo
,并尽量减少特权帐户上 运行 的代码量,即从您的 crontab
中删除 sudo
,而是将其添加到脚本中需要它的各个命令中。
我正在尝试每天 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、在末尾留空行等...
sudo crontab
创建一个作业 运行 在 root
的 crontab 之外(如果你设法正确配置它;root
crontabs 中的语法是不同的).当 cron
运行 是工作时,$HOME
(和 ~
如果您使用 shell 或带有波浪线扩展的脚本语言)将引用 root
等
你应该简单地添加
0 12 * * * sudo ./Documents/Crontab_logs/Making_save.sh
改为您自己的crontab
。
请注意 crontab
根本没有波浪号扩展(但我们可以相信 cron
将始终 运行 在您的主目录之外)。
...虽然这仍然会有问题,因为如果脚本 运行s 在 sudo
下并创建新文件,这些文件将归 root
所有,并且您的普通用户帐户无法更改。更好的解决方案仍然是仅 运行 实际的 mount
和 umount
命令与 sudo
,并尽量减少特权帐户上 运行 的代码量,即从您的 crontab
中删除 sudo
,而是将其添加到脚本中需要它的各个命令中。