139

I want to be able to programatically add a new cron job, what is the best way to do this?

From my research, it seems I could dump the current crontab and then append a new one, piping that back into crontab:

(crontab -l ; echo "0 * * * * wget -O - -q http://www.example.com/cron.php") | crontab -

Is there a better way?

davidmytton
  • 35,554
  • 36
  • 83
  • 91

19 Answers19

144

The best way if you're running as root, is to drop a file into /etc/cron.d

if you use a package manager to package your software, you can simply lay down files in that directory and they are interpreted as if they were crontabs, but with an extra field for the username, e.g.:

Filename: /etc/cron.d/per_minute

Content: * * * * * root /bin/sh /home/root/script.sh

electronix384128
  • 6,286
  • 10
  • 39
  • 64
MarkR
  • 59,334
  • 14
  • 109
  • 144
  • 5
    Just make sure the version of cron in use supports /etc/cron.d/. Most modern Linux distributions do. – sleske Mar 06 '09 at 11:09
  • I think cron.d is a Redhat patch. It is however, a very widespread one, due the the fact that it's so useful for packaging. – MarkR Mar 24 '11 at 13:47
  • 8
    This is the simplest, most elegant solution. Sure beats having to parse and edit an existing crontab with other non-related entries. – Cerin Oct 07 '11 at 14:01
  • When your cron supports this, this is the best solution imho – Jaap Versteegh May 09 '13 at 10:40
  • 13
    One potential caveat to this is the frequency at which cron picks up new additions to this folder (once per hour). If you're expecting your job to begin running right away, beware. – JonathanK Apr 17 '14 at 02:53
  • 6
    @JonathanK, do you happen to know if cron can be asked to re-scan `/etc/cron.d/*`? It's hard to know if something is working if one has to wait for an hour to check! – halfer Dec 09 '14 at 15:57
  • 1
    At least on CentOS & RHEL, "The cron daemon checks the /etc/crontab file, the /etc/cron.d/ directory, and the /var/spool/cron/ directory every minute for any changes." https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-autotasks-cron-configuring.html https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/ch-autotasks.html – dskrvk Feb 10 '17 at 00:33
  • 4
    how should perms set for this file? chmod +x /etc/cron.d/per_minute ? – giò Sep 03 '17 at 09:03
  • This solution is very dependent on the specifics of your distribution. Not all *nix-es are compatible with `cron.d` directory and each has its own little quirks (some allow dashes in the filename, some don't, some need specific file permissions, some don't, some allow `root` crontabs, some don't, etc etc). I couldn't get it to work on Ubuntu 16.04 – Ciprian Tomoiagă Jul 12 '19 at 08:44
  • @giò the debian man page in Buster at least says: **The files under /etc/cron.d do not need to be executable** https://manpages.debian.org/buster/cron/cron.8.en.html – Badr Elmers Feb 10 '21 at 08:26
  • debian too will check the cron files every minute: from man page: **cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute.** – Badr Elmers Feb 10 '21 at 08:27
  • i'd much prefer this were the selected answer – Reinsbrain Apr 13 '21 at 16:43
108

OP's solution has a bug, it might allow entries to be added twice, use below to fix.

(crontab -l ; echo "0 * * * * your_command") | sort - | uniq - | crontab -
rogerdpack
  • 50,731
  • 31
  • 212
  • 332
JohnZ
  • 1,217
  • 1
  • 9
  • 5
25

To Add something to cron

(crontab -l ; echo "0 * * * * hupChannel.sh") 2>&1 | grep -v "no crontab" | sort | uniq | crontab -

To remove this from cron

(crontab -l ; echo "0 * * * * hupChannel.sh") 2>&1 | grep -v "no crontab" | grep -v hupChannel.sh |  sort | uniq | crontab -

hope would help someone

Matas Vaitkevicius
  • 49,230
  • 25
  • 212
  • 228
user3865482
  • 251
  • 3
  • 3
  • 1
    This is exactly what I was looking for I would just add this: | grep -v '^#' | to filter out the comments – Tjunkie Aug 29 '14 at 17:01
  • 4
    You actually shouldn't need `2>&1 | grep -v "no crontab"` because when there is no crontab, the output line `crontab: no crontab for...` is sent to stderr. There's no reason to capture that output, send it to stdout, and then filter it out using grep. If your goal is to avoid seeing `crontab: no crontab for...` in your output, then use `2> /dev/null | sort....`. – matty Sep 02 '16 at 13:43
8

Most of the solutions here are for adding lines to the crontab. If you need more control, you'll want to be able to control the entire contents of the crontab.

You can use piping to do this pretty elegantly.

To completely rewrite the crontab, do

echo "2 2 2 2 2 /bin/echo foobar" |crontab -

This should be easy to combine with other answers described here like

crontab -l | <something> | tee |crontab -

Or, if you have the contents in a file, it is even simpler

cat <file> |crontab -
Eldamir
  • 8,236
  • 5
  • 36
  • 62
6

Simply change the editor to tee command:

export EDITOR="tee"
echo "0 * * * * /bin/echo 'Hello World'" | crontab -e
Destroyica
  • 4,007
  • 2
  • 30
  • 48
6

If you're planning on doing it for a run-once scenario for just wget'ing something, take a look at 'at'

Christian Witts
  • 10,417
  • 1
  • 29
  • 42
5

Assuming that there is already an entry in your crontab, the following command should work relatively well. Note that the $CMD variable is only there for readability. Sorting before filtering duplicates is important, because uniq only works on adjacent lines.

CMD='wget -O - -q http://www.example.com/cron.php"'
(crontab -l ; echo "0 * * * * $CMD") | sort | uniq | crontab -

If you currently have an empty crontab, you will receive the following error to stderr:

no crontab for user

If you want to avoid this, you can add a little bit of complexity add do something like this:

(crontab -l ; echo "0 * * * * $CMD") 2>&1 | sed "s/no crontab for $(whoami)//"  | sort | uniq | crontab -
Craig S. Anderson
  • 6,150
  • 4
  • 27
  • 42
Tim McNamara
  • 16,863
  • 3
  • 48
  • 77
3

Here's another one-liner way, that avoids duplicates

(crontab -l 2>/dev/null | fgrep -v "*/1 *  *  *  * your_command"; echo "*/1 *  *  *  * your_command") | crontab -

And here's a way to do JohnZ's answer and avoid no crontab for user message, or if you need to operate in a set -eu type environment and can't have anything return a failure (in which case the 2>/dev/null part is optional):

( (crontab -l 2>/dev/null || echo "")  ; echo "0 * * * * your_command") | sort -u - | crontab -

Or if you want to split things up so that they're more readable:

new_job="0 * * * * your_command"
preceding_cron_jobs=$(crontab -l || echo "")
(echo "$preceding_cron_jobs" ; echo "$new_job") | sort - | uniq - | crontab -

Or optionally remove any references to your_command (ex: if the schedule has changed, you only want it ever cron'ed once). In this case we no longer need uniq (added bonus, insertion order is also preserved):

new_job="0 * * * * your_command"
preceding_cron_jobs=$(crontab -l || echo "")
preceding_cron_jobs=$(echo "$preceding_cron_jobs" | grep -v your_command )
(echo "$preceding_cron_jobs" ; echo "$new_job") | crontab -
rogerdpack
  • 50,731
  • 31
  • 212
  • 332
2

Adding to JohnZ's answer, here's the syntax to schedule as root if you are a sudoer:

(sudo crontab -l ; echo "0 * * * * your_command") | sort - | uniq - | sudo crontab -
BuvinJ
  • 7,867
  • 3
  • 61
  • 78
2

man crontab is also useful:

CRONTAB(1)

NAME

   crontab - manipulate per-user crontabs (Dillon's Cron)

SYNOPSIS

   crontab file [-u user] - replace crontab from file

   crontab - [-u user] - replace crontab from stdin

   crontab -l [user] - list crontab for user
vitaly.v.ch
  • 2,278
  • 3
  • 24
  • 35
1
function cronjob_exists($command){

    $cronjob_exists=false;

    exec('crontab -l', $crontab);


    if(isset($crontab)&&is_array($crontab)){

        $crontab = array_flip($crontab);

        if(isset($crontab[$command])){

            $cronjob_exists=true;

        }

    }
    return $cronjob_exists;
}

function append_cronjob($command){

    if(is_string($command)&&!empty($command)&&cronjob_exists($command)===FALSE){

        //add job to crontab
        exec('echo -e "`crontab -l`\n'.$command.'" | crontab -', $output);


    }

    return $output;
}

    append_cronjob('* * * * * curl -s http://localhost/cron/test.php');
RafaSashi
  • 14,170
  • 8
  • 71
  • 85
1

This would check to ensure that your command doesn't already exist before adding it.

crontab -l 2>/dev/null | grep -q '/path/to/script' || echo "5 * * * * /path/to/script" | crontab -

Cheers.

Ezra Obiwale
  • 1,430
  • 11
  • 13
0

Piping stdout into crontab didn't install the new crontab for me on macOS, so I found this solution instead, using the tee editor in a sub shell:

(EDITOR=tee && (crontab -l ; echo "@daily ~/my-script.sh" ) | uniq - | crontab -e)
Simon Hänisch
  • 3,634
  • 1
  • 27
  • 35
0

If you want the task to run as a user:

crontab -l | { cat; echo "@weekly what_you_want_to_execute"; } | crontab -

If you want the task to run with privileges:

sudo crontab -l | { cat; echo "@weekly what_you_want_to_execute"; } | sudo crontab -

and check task (with or without 'sudo'):

crontab -l | sed '/^$/d; /#/d'
ajcg
  • 286
  • 2
  • 11
0

also you can add your tasks to /etc/cron.*/

vitaly.v.ch
  • 2,278
  • 3
  • 24
  • 35
0

Below is what I use in my script

1.

(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 -

#write out current crontab

crontab -l > mycron 2>/dev/null

#echo new cron into cron file

echo "0 3 * * * /usr/local/bin/certbot-auto renew" >> mycron

#install new cron file

crontab mycron

rm mycron
Anh Quach
  • 11
  • 1
0

To have flexibility when changing the command in the future you can do this (works best with an update script)

MARK=SOME_UNIQUE_MARK_TEXT
LINE="This is the command # $MARK"
# NOTE: I'm using -e because I might want to avoid weird bash expansions for '*' or '$' and place:
# \x2A instead of *
# \x24 instead of $
( crontab -l | grep -v $MARK ; echo -e "0 * * * *" $LINE ) | crontab -

In this way I can update an old crontab command which no longer serves current purpose.

Basically I get the current crontab, I eliminate the line containing the MARK (grep -v) and replace it with the new one by appending it at the end of the crontab file via echo -e. In this particular case I want it executed every hour at minute 0 0 * * * * as it can be seen from here.

INS
  • 9,798
  • 4
  • 52
  • 88
-2

You could also edit the cron table text file directly, but your solution seems perfectly acceptable.

Alex Fort
  • 17,293
  • 5
  • 40
  • 51
  • 1
    I would be leery of this due to concerns of the chance of concurrent edits causing file corruption. Using the command-line crontab command should avoid that problem. – Craig S Mar 05 '09 at 02:28
  • Craig, without more research I wouldn't be sure that the command line version is atomic and race-condition-safe. Probably "quite safe" anyway. – tuomassalo Mar 16 '11 at 13:03
  • "Editing the file directly" is not feasible without root access (provided you can figure out where the user's crontab file lives, and how to make sure the cron daemon correctly receives your changes), and "editing" programmatically seems to be what the question wants advice on in the first place. – tripleee May 19 '15 at 09:12
-27

It's always worked well for me.

You should consider a slightly more sophisticated script that can do three things.

  1. Append a crontab line; assuring that it didn't exist. Adding when it already exists is bad.

  2. Remove the crontab line. Perhaps only warning if it didn't exist.

  3. A combination of the above two features to replace the crontab line.

S.Lott
  • 359,791
  • 75
  • 487
  • 757