How to create crontab via script

I need to add a cron job through a script that I run to configure the server. I am currently using Ubuntu. I can use crontab -e , but this will open an editor to edit the current crontab. I want to do this programmatically.

Can this be done?

+119
linux shell cron ubuntu crontab
Feb 02 2018-11-28T00:
source share
12 answers

Cron jobs are usually stored in a user file in /var/spool/cron

The simplest thing you can do is probably just create a text file with a customized task, then copy it to the cron spool folder and make sure that it has the necessary permissions.

+11
Feb 02 2018-11-22T00:
source share

Here's a one-liner that doesn't use / requires the new job to be in the file:

 (crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/job -with args") | crontab - 

2>/dev/null it is important that you do not receive a no crontab for username message that some * nixes produce if there are currently no crontab entries.

+294
Mar 08 '12 at 21:30
source share

For the crontabs user (including root) you can do something like:

 crontab -l -u user | cat - filename | crontab -u user - 

where a file named "filename" contains items to add. You can also manipulate text with sed or another tool instead of cat . You should use the crontab command instead of directly modifying the file.

A similar operation would be:

 { crontab -l -u user; echo 'crontab spec'; } | crontab -u user - 

If you modify or create crontab, you can manage them just like regular text files. They are stored in the directories /etc/cron.d , /etc/cron.hourly , /etc/cron.daily , /etc/cron.weekly , /etc/cron.monthly and in the files /etc/crontab and /etc/anacrontab .

+53
Feb 03 2018-11-11T00:
source share

On Ubuntu and many other distributions, you can simply put the file in the /etc/cron.d directory containing one line with a valid crontab entry. There is no need to add a line to an existing file.

If you just need to run something every day, just put the file in /etc/cron.daily . Similarly, you can also drop files in /etc/cron.hourly , /etc/cron.monthly and /etc/cron.weekly .

+26
Feb 02 2018-11-21T00:
source share

Crontab files are simply text files and, as such, can be thought of as any other text file. The purpose of the crontab command is to make editing crontab files safer. When editing this command, the file is checked for errors and saved only in the absence.

crontab [path to file] can be used to specify crontab stored in a file. Like crontab -e , this will install the file only if it is error free.

Therefore, the script can either directly write cron tab files, or write them to a temporary file and load them using the crontab [path to temp file] command. Recording directly saves the need to write a temporary file, but also avoids security checks.

+16
Feb 02 2018-11-22T00:
source share

An even simpler answer to your question:

 echo "0 1 * * * /root/test.sh" | tee -a /var/spool/cron/root 

You can configure cronjob on remote servers as shown below:

 #!/bin/bash servers="srv1 srv2 srv3 srv4 srv5" for i in $servers do echo "0 1 * * * /root/test.sh" | ssh $i " tee -a /var/spool/cron/root" done 

On Linux, the default location of the default crontab file is /var/spool/cron/ . Here you can find crontab files crontab all users. You just need to add your cronjob entry to the corresponding user file. In the above example, the crontab root user file is added using cronjob to run /root/test.sh every day at 1am.

+15
Jan 30 '15 at 19:04
source share

As a fix for anyone offering crontab -l | crontab - crontab -l | crontab - crontab -l | crontab - crontab -l | crontab - : this does not work on all systems. For example, I had to add a job to the root crontab on dozens of servers running under the old version of SUSE (don't ask why). Old SUSE adds comment lines to the output of crontab -l , making crontab -l | crontab - crontab -l | crontab - crontab -l | crontab - crontab -l | crontab - non crontab -l | crontab - idempotent (Debian recognizes this problem in the crontab man page and fixed its version of Vixie Cron to change the default behavior of crontab -l by default).

To programmatically edit crontabs on systems where crontab -l adds comments, you can try the following:

EDITOR=cat crontab -e > old_crontab; cat old_crontab new_job | crontab -

EDITOR=cat tells crontab to use cat as an editor (not the usual default vi), which does not modify the file, but instead copies it to standard output. This can still fail if crontab - expects input in a format different from what crontab -e prints. Do not try to replace the last crontab - crontab -e - it will not work.

+5
Jan 16 '15 at 20:32
source share

(I don't have enough reputation to comment, so I'm adding as an answer: feel free to add it as a comment next to his answer)

Joe Casadonte one -l iner is perfect unless you run with set -e , i.e. if your script is configured to fail on error, and if there are no cronjobs yet. In this case, one iner -l will NOT create a cronjob, but will NOT stop the script. A silent mistake can be very deceiving.

The reason is that crontab -l returns with a return code of 1 , as a result of which the subsequent command ( echo ) is not executed ... thus, a cronjob is not created. But since they execute as a subprocess (due to parentheses), they do not stop the script.

(Interestingly, if you run the same command again, it will work: after you run crontab - once, crontab -l still doesn’t display anything, but does not return an error (you do not get no crontab for <user> more). So the subsequent echo is done and crontab is created)

In any case, if you run with set -e , the line should be:

 (crontab -l 2>/dev/null || true; echo "*/5 * * * * /path/to/job -with args") | crontab - 
+4
Jul 24 '18 at 11:25
source share

Well /etc/crontab just an ascii file, so the easiest way is just

  echo "*/15 * * * * root date" >> /etc/crontab 

which will add a task that will send you an email every 15 minutes. Correct the taste and test with grep or other means whether the line has already been added to create an idempotent script.

In Ubuntu et al, you can also drop files in /etc/cron.* , which are easier to run and test --- plus you don’t go into configuration files (system), such as /etc/crontab .

+2
Feb 02 2018-11-21T00:
source share

Here's how to change the cron entry without directly editing the cron file (which is deprecated).

 crontab -l -u <user> | sed 's/find/replace/g' | crontab -u <user> - 

If you want to delete the cron entry, use this:

 crontab -l -u <user> | sed '/find/d' | crontab -u <user> - 

I understand that this is not what Gaurav requested, but why not have all the solutions in one place?

+2
Mar 09 '18 at 17:47
source share

I wrote a crontab deployment tool in python: https://github.com/monklof/deploycron

 pip install deploycron 

Installing your crontab is very simple, it will integrate crontab into your existing crontab system.

 from deploycron import deploycron deploycron(content="* * * * * echo hello > /tmp/hello") 
+1
Aug 24 '16 at 7:34
source share

This is the approach for gradually adding a cron job:

  ssh USER_NAME@$PRODUCT_IP nohup "echo '*/2 * * * * ping -c2 PRODUCT_NAME.com >> /var/www/html/test.html' | crontab -u USER_NAME -" 
0
Jun 30 '19 at 13:40
source share



All Articles