How to backup and load crontab from a file in Linux and UNIX

If you have been using Linux for some time then you might know about cron jobs. They are the scheduler which is used to automatically start processes in a Linux box. I have worked in many projects which use cron jobs to start the environment daily or weekly. They are similar to Autosys when it comes to scheduling job but cron is a Linux command as opposed to Autosys which is separate application altogether. In Linux, crontab command is used for loading cron jobs from a file, listing existing cron entries, and editing them. It manages the cron table that is used by the cron daemon in Linux to execute the cron jobs. Though there are comments to view cron jobs, edit cron jobs and upload cron jobs from a file there is no command to backup existing cron jobs, but don't worry, I'll show you a trick to backup your cron job before editing them or loading a new set of cron jobs.

Many Java project uses cron jobs to start their process, which means whenever a new process is introduced, you have to add an entry for starting and stopping that process into cron table. This means editing the crontab manually by using crontab -e option.

This put the risk of accidentally modifying another job. To avoid this, instead of manually editing the crontab to add new jobs, you can also upload all the cron jobs from a file. This is helpful when you have to maintain a lot of servers that have the same cron job entries.

How to load cron jobs form a file in Linux

In order to demonstrate how to use crontab command to load cron jobs from a file, I have creatd a file called /home/myappuser/mycronjobs.txt, which contains two cron jobs, one to start server and other to stop server. Here is how they look like:

$ cat /home/myappuser/mycronjobs.txt
53 00 * * 7 /bin/sh /home/myappuser/bin/
01 00 * * * /bin/sh /home/myappuser/bin/

To upload the mycronjobs.txt jobs to current user crontab, you can use following just use crontab command in Linux as shown below:

$ crontab /home/myappuser/mycronjobs.txt

Once uploaded you should check the cron table to make sure the cron jobs are successfully uploaded. You can again use the same crontab -l command to check all cron entries as shown below:

$ crontab -l
53 00 * * 7 /bin/sh /home/myappuser/bin/
01 00 * * * /bin/sh /home/myappuser/bin/

Similarly, if you want to upload the cron job from a file to another user you can use the crontab with -u option, as shown below:

$ crontab -u id_app /home/myappuser/mycronjobs.txt

Btw, before making any change to cron table, you should backup existing cron entries so that you can restore them in the event of a rollback. Hence it's important that you first know how to backup existing cronjob in a file. So that you can upload that file later to restore previous jobs.

How to backup cron jobs in Linux

You can use the following command to backup your existing cron table in Linux:

$ crontab -l > backup_date.text

where the date is the actual date of backup e.g. you can put 20022016 etc. Basically, we are just listing all cron jobs and redirecting the output to a file, later we can use the same file to restore all cron jobs by using the same command we'll use to upload cron jobs from a file in Linux.

You can also use crontab -l to list all cron jobs for the current user and you can also use crontab -e to edit existing cron entries. By default, Linux will open cron table in VI editor for editing and once you are done i.e. perform save and exit, new cron jobs will automatically be loaded into cron table.

Btw, crontab uses the editor to edit cron jobs based upon the VISUAL or EDITOR environment variable.If you don't like Vim and want to use Emacs you can change the value of VISUAL/EDITOR environment variable and crontab will pick that. See Linux Command Line Interface (CLI) Fundamentals for more crontabl options.

That's all about how to backup and upload crontab file in Linux. This is the easiest way to add new jobs or a suite of new jobs into cron table,b ut you should be a little bit careful with this upload method becuase it will override all current job entries. That's why its best to take a backup of existing crontab entries before uploading new jobs using this method. In the event of anything goes wrong, you can always restore old cron entries form your backup.

Further Reading
How Linux Works: What Every Superuser Should Know
Linux Command Line Interface (CLI) Fundamentals
10 example of networking command in Unix
10 Example of tar commands in Unix
5 Example of kill commands in Unix and Linux
VI Editor examples and tips for beginners
The Linux Command Line: A Complete Introduction

Thanks a lot for reading this article so far. If you like this UNIX command tips then please share with your friends and colleagues. If you have any question or comment then please drop a comment.

No comments:

Post a Comment