crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?
当前回答
Bash脚本,用于在没有交互式编辑器的情况下添加cron作业。 下面的代码帮助您使用linux文件添加cronjob。
#!/bin/bash
cron_path=/var/spool/cron/crontabs/root
#cron job to run every 10 min.
echo "*/10 * * * * command to be executed" >> $cron_path
#cron job to run every 1 hour.
echo "0 */1 * * * command to be executed" >> $cron_path
其他回答
谢谢大家的帮助。把我在这里和其他地方的发现拼凑起来,我得出了这个结论:
的代码
command="php $INSTALL/indefero/scripts/gitcron.php"
job="0 0 * * 0 $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -
我想不出如何在不重复自己的情况下消除对这两个变量的需求。
命令显然是我想调度的命令。Job接受$命令并添加调度数据。我需要在执行工作的代码行中分别使用这两个变量。
细节
Credit to duckyflip, I use this little redirect thingy (<(*command*)) to turn the output of crontab -l into input for the fgrep command. fgrep then filters out any matches of $command (-v option), case-insensitive (-i option). Again, the little redirect thingy (<(*command*)) is used to turn the result back into input for the cat command. The cat command also receives echo "$job" (self explanatory), again, through use of the redirect thingy (<(*command*)). So the filtered output from crontab -l and the simple echo "$job", combined, are piped ('|') over to crontab - to finally be written. And they all lived happily ever after!
简而言之:
这行代码过滤掉与命令匹配的任何cron作业,然后用新的cron作业写出剩余的cron作业,有效地充当“添加”或“更新”函数。 要使用它,您所要做的就是替换命令和作业变量的值。
关于crontab的使用已经有很多不错的答案,但是没有提到更简单的方法,比如使用cron。
使用cron将利用位于/etc/crontab、/etc/cron.目录的系统文件和目录Daily、weekly、hour或/etc/cron.d/:
cat > /etc/cron.d/<job> << EOF
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
01 * * * * <user> <command>
EOF
在上面的例子中,我们在/etc/cron.目录中创建了一个文件D /为命令成功执行提供了环境变量,并为命令和命令本身提供了用户。该文件不应该是可执行的,名称应该只包含字母-数字和连字符(详细信息如下)。
为了给出一个彻底的答案,让我们看看crontab和cron/crond之间的区别:
crontab -- maintain tables for driving cron for individual users
对于那些希望在系统上的用户上下文中运行作业的人来说,使用crontab可能非常有意义。
cron -- daemon to execute scheduled commands
对于那些使用配置管理或希望为其他用户管理作业的人,在这种情况下,我们应该使用cron。
从手册中摘录了一些例子,告诉你应该做什么,不应该做什么:
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. The files under /etc/cron.d do not need to be executable, while the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly do, as they are run by run-parts (see run-parts(8) for more information). Source: http://manpages.ubuntu.com/manpages/trusty/man8/cron.8.html
从系统的角度来看,以这种方式管理cron更容易,也更可扩展,但并不总是最好的解决方案。
脚本函数添加cronjob。检查重复条目,可用表达式* > "
cronjob_creator () {
# usage: cronjob_creator '<interval>' '<command>'
if [[ -z $1 ]] ;then
printf " no interval specified\n"
elif [[ -z $2 ]] ;then
printf " no command specified\n"
else
CRONIN="/tmp/cti_tmp"
crontab -l | grep -vw "$1 $2" > "$CRONIN"
echo "$1 $2" >> $CRONIN
crontab "$CRONIN"
rm $CRONIN
fi
}
测试:
$ ./cronjob_creator.sh '*/10 * * * *' 'echo "this is a test" > export_file'
$ crontab -l
$ */10 * * * * echo "this is a test" > export_file
来源:我的大脑;)
很有可能您正在将此工作自动化,并且您不希望单个工作添加两次。 在这种情况下使用:
__cron="1 2 3 4 5 /root/bin/backup.sh"
cat <(crontab -l) |grep -v "${__cron}" <(echo "${__cron}")
这只在使用BASH时有效。我不知道正确的DASH (sh)语法。
更新:如果用户还没有crontab,这将不起作用。更可靠的方法是:
(crontab -l ; echo "1 2 3 4 5 /root/bin/backup.sh") | sort - | uniq - | crontab -
或者,如果你的发行版支持它,你也可以使用一个单独的文件:
echo "1 2 3 4 5 /root/bin/backup.sh" |sudo tee /etc/crond.d/backup
在另一个SO问题中找到的。
如果你正在使用Vixie Cron,例如在大多数Linux发行版中,你可以把一个文件放在/etc/cron.D与个人cronjob。
当然,这只适用于根。如果您的系统支持这一点,您应该在其中看到几个示例。(注意行中包含的用户名,语法与旧的/etc/crontab相同)
这是cron中一个可悲的错误特性,没有办法作为普通用户来处理这个问题,而且很多cron实现根本没有办法处理这个问题。