crontab是否有不使用编辑器(crontab -e)创建cron作业的参数?如果是,从Bash脚本创建cron作业的代码是什么?


当前回答

如果你正在使用Vixie Cron,例如在大多数Linux发行版中,你可以把一个文件放在/etc/cron.D与个人cronjob。

当然,这只适用于根。如果您的系统支持这一点,您应该在其中看到几个示例。(注意行中包含的用户名,语法与旧的/etc/crontab相同)

这是cron中一个可悲的错误特性,没有办法作为普通用户来处理这个问题,而且很多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

为了用BASH脚本快速创建/替换crontab,我使用了下面的符号:

crontab <<EOF
00 09 * * 1-5 echo hello
EOF

假设你是以“ubuntu”用户登录的,但是你想把一个作业添加到另一个用户的crontab中,比如“john”。您可以执行以下操作:

(sudo crontab -l -u john; echo "* * * * * command") | awk '!x[$0]++' | sudo crontab -u john -

此解决方案的大部分来源:https://www.baeldung.com/linux/create-crontab-script

我有吨的问题试图添加一个工作到另一个用户的crontab。它不断复制crontab,或者直接删除它们。不过,经过一些测试后,我相信这行代码将以非破坏性的方式将一个新作业附加到指定用户的crontab,包括不创建已经存在的作业。

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

谢谢大家的帮助。把我在这里和其他地方的发现拼凑起来,我得出了这个结论:

的代码

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作业,有效地充当“添加”或“更新”函数。 要使用它,您所要做的就是替换命令和作业变量的值。