How to create a cron job using Bash automatically

2019-01-03 11:30发布

Does crontab have an argument for creating cron jobs without using the editor (crontab -e). If so, What would be the code create a cronjob from a Bash script?

标签: bash shell cron
18条回答
劫难
2楼-- · 2019-01-03 12:06

Here is a bash function for adding a command to crontab without duplication

function addtocrontab () {
  local frequency=$1
  local command=$2
  local job="$frequency $command"
  cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -
}
addtocrontab "0 0 1 * *" "echo hello"
查看更多
戒情不戒烟
3楼-- · 2019-01-03 12:07

For a nice quick and dirty creation/replacement of a crontab from with a BASH script, I used this notation:

crontab <<EOF
00 09 * * 1-5 echo hello
EOF
查看更多
Lonely孤独者°
4楼-- · 2019-01-03 12:08

Bash script for adding cron job without the interactive editor. Below code helps to add a cronjob using linux files.

#!/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
查看更多
孤傲高冷的网名
5楼-- · 2019-01-03 12:08

You can probably change the default editor to ed and use a heredoc to edit.

EDITOR=ed
export EDITOR

crontab -e << EOF
> a
> * * * * * Myscript
> * * * * * AnotherScript
> * * * * * MoreScript
> .
> w
> q
> EOF

Note the leading > in that code means the return/enter key is pressed to create a new line.

The a means APPEND so it will not overwrite anything.

The . means you're done editing.

The w means WRITE the changes.

The q means QUIT or exit ed.

you can check it out

crontab -l

You can delete an entry too.

EDITOR=ed
export EDITOR

crontab -e << EOF
> /Myscript/
> d
> .
> w
> q
> EOF

That will delete the crontab entry with Myscript in it.

The d means delete the pattern inside the / /.

No check it again

crontab -l

This solution works inside a script too less the > of course :-)

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-03 12:09

EDIT (fixed overwriting):

cat <(crontab -l) <(echo "1 2 3 4 5 scripty.sh") | crontab -
查看更多
混吃等死
7楼-- · 2019-01-03 12:10

Thanks everybody for your help. Piecing together what I found here and elsewhere I came up with this:

The Code

command="php $INSTALL/indefero/scripts/gitcron.php"
job="0 0 * * 0 $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -

I couldn't figure out how to eliminate the need for the two variables without repeating myself.

command is obviously the command I want to schedule. job takes $command and adds the scheduling data. I needed both variables separately in the line of code that does the work.

Details

  1. Credit to duckyflip, I use this little redirect thingy (<(*command*)) to turn the output of crontab -l into input for the fgrep command.
  2. fgrep then filters out any matches of $command (-v option), case-insensitive (-i option).
  3. Again, the little redirect thingy (<(*command*)) is used to turn the result back into input for the cat command.
  4. The cat command also receives echo "$job" (self explanatory), again, through use of the redirect thingy (<(*command*)).
  5. So the filtered output from crontab -l and the simple echo "$job", combined, are piped ('|') over to crontab - to finally be written.
  6. And they all lived happily ever after!

In a nutshell:

This line of code filters out any cron jobs that match the command, then writes out the remaining cron jobs with the new one, effectively acting like an "add" or "update" function. To use this, all you have to do is swap out the values for the command and job variables.

查看更多
登录 后发表回答