Crontab无法启动脚本 [英] Crontab not launching script

查看:226
本文介绍了Crontab无法启动脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每天在12点通过crontab运行以下脚本:

I'm trying to run the following script through crontab every day at 12 :

#!/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'中添加了以下代码:

As it needs to run in sudo, I added the following line to 'sudo crontab' such that I have :

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 

但是它没有运行。我提到只是执行脚本thourgh:

But it does not run. I mention that just executing the script thourgh :

sudo ~/Documents/Crontab_logs/Making_save.sh

工作正常,除了在日志文件中未写入rsync命令的输出。

works well, except that no output of the rsync command is written in the log file.

有什么想法吗?我认为我检查了错误的主要根源,即使用shell,在末尾留空行等等。

Any ideas what's going wrong ? I think I checked the main source of mistakes, i.e. using shell, leaving an empty line at the end, etc ...

推荐答案

sudo crontab 创建一个作业,该作业在 root 的crontab中运行(如果您设法正确配置它; root crontabs中的语法不同)。当 cron 运行作业时, $ HOME (和 (如果您使用带波浪号扩展功能的Shell或脚本语言)将引用 root 等的目录。

sudo crontab creates a job which runs out of the crontab of root (if you manage to configure it correctly; the syntax in root crontabs is different). When cron runs the job, $HOME (and ~ if you use a shell or scripting language with tilde expansion) will refer to the home of root etc.

您应该应该简单地添加

0 12 * * * sudo ./Documents/Crontab_logs/Making_save.sh

替换为您自己的 crontab

请注意, crontab 根本没有波浪线扩展(但是我们可以依靠 cron 始终会在您的主目录中运行)。

Notice that crontab does not have tilde expansion at all (but we can rely on the fact that cron will always run out of your home directory).

...虽然这仍然会有问题,因为如果脚本在 sudo下运行并创建新文件,这些文件将由 root 拥有,并且不能由常规用户帐户更改。更好的解决方案是仅使用 sudo运行实际的 mount umount 命令。 code>,并最小化特权帐户上运行的代码量,即从 crontab sudo c>,而是将其添加到脚本中的各个命令中。

... Though this will still have issues, because if the script runs under sudo and it creates new files, those files will be owned by root, and cannot be changed by your regular user account. A better solution still is to only run the actual mount and umount commands with sudo, and minimize the amount of code which runs on the privileged account, i.e. remove the sudo from your crontab and instead add it within the script to the individual commands which require it.

这篇关于Crontab无法启动脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆