Coder Perfect

How can I use a script to generate a crontab?


I need to add a cron job to a server setup script I’m running. Currently, I’m running Ubuntu. I can use crontab -e to edit the existing crontab, but that will open an editor. This is something I’d like to do programmatically.

Is this something that can be done?

Asked by stocked

Solution #1

Here’s a one-liner that avoids the need for the new job to be saved in a file:

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

The 2>/dev/null is necessary to avoid receiving the no crontab for username message that some *nixes display if no crontab entries are currently present.

Answered by Joe Casadonte

Solution #2

You can perform something like this for user crontabs (including root):

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

where “filename” is the file containing the items to append. In place of cat, you may use sed or another program to manipulate text. Instead of editing the file directly, you should use the crontab command.

An example of a similar operation might be:

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

If you’re editing or creating system crontabs, you can treat them like any other text file. They’re kept in the directories /etc/cron.d, /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly, as well as the files /etc/crontab and /etc/anacrontab.

Answered by Dennis Williamson

Solution #3

You can simply enter a single line with a valid crontab entry into the /etc/cron.d directory in Ubuntu and many other distros. There’s no need to add a line to a file that already exists.

Simply place a file in /etc/cron.daily if you only need something to run once a day. You can also use /etc/cron.hourly, /etc/cron.monthly, and /etc/cron.weekly to store files.

Answered by IvanGoneKrazy

Solution #4

Crontab files are simple text files that can be used in the same way as any other text file. The crontab command was created to make editing crontab files safer. When using this command to edit a file, it is checked for mistakes and only saved if none are found.

A crontab stored in a file can be specified with crontab [path to file]. This, like crontab -e, will only install a file if it is free of errors.

As a result, a script can either write crontab files directly or save them to a temporary file and then load them using the crontab [path to temp file] command. Direct writing reduces the need to create a temporary file, but it also skips the safety check.

Answered by cledoux

Solution #5

Even a more straightforward response to your query would be:

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

On distant servers, you can set up cronjobs as follows:

servers="srv1 srv2 srv3 srv4 srv5"
for i in $servers
  echo "0 1 * * * /root/" | ssh $i " tee -a /var/spool/cron/root"

The crontab file is located at /var/spool/cron/ by default on Linux. All users’ crontab files may be seen here. All you have to do now is append your cronjob entry to the user’s file. The root user’s crontab file is appended with a cronjob to run /root/ every day at 1 AM in the example above.

Answered by ganesh pathak

Post is based on