crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建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

来源:我的大脑;)

其他回答

您可以向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来源。

编辑(固定覆盖):

cat <(crontab -l) <(echo "1 2 3 4 5 scripty.sh") | crontab -
echo "0 * * * * docker system prune --force >/dev/null 2>&1" | sudo tee /etc/cron.daily/dockerprune

不,crontab中没有修改cron文件的选项。

您必须:获取当前的cron文件(crontab -l > newfile),更改它并将新文件放置到位(crontab newfile)。

如果你熟悉perl,你可以使用Config::Crontab这个模块。

LLP安德里亚

你可以在飞行中完成

crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -

Crontab -l列出当前的Crontab任务,cat打印它,echo打印新命令,Crontab -将所有打印的内容添加到Crontab文件中。您可以通过执行一个新的crontab -l来查看效果。