crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?
当前回答
(2>/dev/null crontab -l ; echo "0 3 * * * /usr/local/bin/certbot-auto renew") | crontab -
cat <(crontab -l 2>/dev/null) <(echo "0 3 * * * /usr/local/bin/certbot-auto renew") | crontab -
#输出当前crontab
crontab -l > mycron 2>/dev/null
#echo new cron到cron文件中
echo "0 3 * * * /usr/local/bin/certbot-auto renew" >> mycron
#安装新的cron文件
crontab mycron
rm mycron
其他回答
你可以在飞行中完成
crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -
Crontab -l列出当前的Crontab任务,cat打印它,echo打印新命令,Crontab -将所有打印的内容添加到Crontab文件中。您可以通过执行一个新的crontab -l来查看效果。
我想找一个这样的例子,所以可能会有帮助:
COMMAND="/var/lib/postgresql/backup.sh"
CRON="0 0 * * *"
USER="postgres"
CRON_FILE="postgres-backup"
# At CRON times, the USER will run the COMMAND
echo "$CRON $USER $COMMAND" | sudo tee /etc/cron.d/$CRON_FILE
echo "Cron job created. Remove /etc/cron.d/$CRON_FILE to stop it."
您可以向crontab添加如下内容:
#write out current crontab
crontab -l > mycron
#echo new cron into cron file
echo "00 09 * * 1-5 echo hello" >> mycron
#install new cron file
crontab mycron
rm mycron
Cron行解释
* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
nixCraft来源。
关于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更容易,也更可扩展,但并不总是最好的解决方案。
CRON="1 2 3 4 5 /root/bin/backup.sh"
cat < (crontab -l) |grep -v "${CRON}" < (echo "${CRON}")
给grep exact命令添加-w参数,不带-w参数添加cronjob "testing"会导致删除cronjob "testing123"
脚本函数添加/删除cronjob。无重复条目:
cronjob_editor () {
# usage: cronjob_editor '<interval>' '<command>' <add|remove>
if [[ -z "$1" ]] ;then printf " no interval specified\n" ;fi
if [[ -z "$2" ]] ;then printf " no command specified\n" ;fi
if [[ -z "$3" ]] ;then printf " no action specified\n" ;fi
if [[ "$3" == add ]] ;then
# add cronjob, no duplication:
( crontab -l | grep -v -F -w "$2" ; echo "$1 $2" ) | crontab -
elif [[ "$3" == remove ]] ;then
# remove cronjob:
( crontab -l | grep -v -F -w "$2" ) | crontab -
fi
}
cronjob_editor "$1" "$2" "$3"
测试:
$ ./cronjob_editor.sh '*/10 * * * *' 'echo "this is a test" > export_file' add
$ crontab -l
$ */10 * * * * echo "this is a test" > export_file