Crontab任务启动需要sudo权限的脚本

时间:2018-12-10 18:08:32

标签: shell cron sudo cron-task

我当前正在使用以下脚本Making_save.sh:

#!/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
umount /mnt/NAS_DFG
date >> ~/Documents/Crontab_logs/logs.txt

在NAS上自动对我的计算机进行rsync。在讨论之后,我想到了该脚本:Crontab not launching script

我的sudo配置包含以下行:

gadal ALL=(ALL) NOPASSWD: /bin/mount -t nfs 10.1.25.7\:gadal /mnt/NAS_DFG, /bin/umount /mnt/NAS_DFG

还有cron:

0 12 * * * /home/gadal/Documents/Crontab_logs/Making_save.sh

通过执行rsync并只返回日志文件,它实际上工作了一段时间。

vendredi 26 octobre 2018, 12:00:01 (UTC+0200)
rsync: opendir "/home/gadal/.cache/dconf" failed: Permission denied (13)
rsync: opendir "/home/gadal/.dbus" failed: Permission denied (13)
rsync: opendir "/home/lost+found" failed: Permission denied (13)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1196) [sender=3.1.2]

对我来说很完美。

但是,现在返回:

lundi 10 décembre 2018, 12:00:01 (UTC+0100)
rsync: recv_generator: mkdir "/mnt/NAS_DFG/home" failed: Permission denied (13)
*** Skipping any contents from this failed directory ***
rsync: opendir "/home/gadal/.cache/dconf" failed: Permission denied (13)
rsync: opendir "/home/gadal/.dbus" failed: Permission denied (13)
rsync: opendir "/home/lost+found" failed: Permission denied (13)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1196) [sender=3.1.2]

并且不执行rsync命令。

请注意,当我使用sudo手动启动脚本时,它可以工作。但是,这些命令应该能够在不询问密码的情况下运行。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

添加:

gadal ALL=(ALL) NOPASSWD: /bin/mount -t nfs 10.1.25.7\:gadal /mnt/NAS_DFG, /bin/umount /mnt/NAS_DFG
sudo配置的

仅允许命令在sudo下执行而无需密码。 它仍然需要在第一个脚本中使用sudo,该脚本现在显示为:

#!/bin/sh
sudo 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
sudo umount /mnt/NAS_DFG
date >> ~/Documents/Crontab_logs/logs.txt