crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?
当前回答
很有可能您正在将此工作自动化,并且您不希望单个工作添加两次。 在这种情况下使用:
__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问题中找到的。
其他回答
这个较短的表不需要临时文件,它不受多次插入的影响,并且允许您更改现有条目的计划。
假设你有这些:
croncmd="/home/me/myfunction myargs > /home/me/myfunction.log 2>&1"
cronjob="0 */15 * * * $croncmd"
将它添加到crontab,不重复:
( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -
将它从crontab中删除,不管它当前的日程安排是什么:
( crontab -l | grep -v -F "$croncmd" ) | crontab -
注:
grep -F逐字匹配字符串,因为我们不想将其解释为正则表达式 我们也忽略了时间调度,只寻找命令。这种方式;可以在没有向crontab添加新行风险的情况下更改时间表
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
谢谢大家的帮助。把我在这里和其他地方的发现拼凑起来,我得出了这个结论:
的代码
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作业,有效地充当“添加”或“更新”函数。 要使用它,您所要做的就是替换命令和作业变量的值。
脚本函数添加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的变体:
CMD="/sbin/modprobe fcpci"
JOB="@reboot $CMD"
TMPC="mycron"
grep "$CMD" -q <(crontab -l) || (crontab -l>"$TMPC"; echo "$JOB">>"$TMPC"; crontab "$TMPC")
推荐文章
- RVM不是一个函数,用' RVM use…’不会起作用
- 如何打破一个循环在Bash?
- 如何从终端机发送电子邮件?
- Linux Bash中双&和分号有什么区别?
- 如何合并2 JSON对象从2个文件使用jq?
- 在Bash中模拟do-while循环
- 在Bash中将输出赋给变量
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 从Docker容器获取环境变量
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- 如何循环通过一个目录递归删除具有某些扩展名的文件
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量
- 删除Bash脚本中的重复条目