Linux Crontab Command Example - How to backup and load Cron Jobs from a File in UNIX

If you have been using Linux for some time then you might know about cron jobs. They are the scheduler which can be used to automatically start processes in a Linux box. I have worked on many projects which used cron jobs to start the Java process and environment daily or weekly basis. 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 scheduling and automating jobs or process. You can also use it 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 commands 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 the 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.

Btw, if you are new to Linux and not very familiar with Linux environment and essential commands itself, then, I suggest you to first go through Learn Linux in 5 Days and Level Up Your Career and fill that gap.

It's an excellent course which allows you to learn Linux at a decent level by investing $10 and just 5 days of your life.  And, as I have said many times in this blog, any investment you will make in terms of time and money on SQL and UNIX will serve you for a long time.





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 created a file called /home/myappuser/mycronjobs.txt, which contains two cron jobs, one to start server and other to stop the server.

Here is how they look like:

$ cat /home/myappuser/mycronjobs.txt
53 00 * * 7 /bin/sh /home/myappuser/bin/start-server.sh
01 00 * * * /bin/sh /home/myappuser/bin/stop-server.sh

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/start-server.sh
01 00 * * * /bin/sh /home/myappuser/bin/stop-server.sh

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

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


These are some of the basic crontab command examples for scheduling and automating cron jobs in Linux. Good knowledge of cron table, jobs, and crontab command is essential for automating starting your application environment in Linux and if you want to learn more, Linux Administration Bootcamp: Go from Beginner to Advanced on Udemy is another comprehensive course for Programmers and System administrator.





How to backup cron jobs in Linux

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 in case of rollback.

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. You can further see Learn Linux in 5 Days and Level Up Your Career course on Udemy to learn more about how to schedule and automate jobs using cron in Linux.



That's all about how to backup and upload crontab file in Linux.  As I said, you can use the crontab command to schedule and automate cron jobs. This is the easiest way to add new jobs or a suite of new jobs into cron table, but you should be a little bit careful with this upload method because 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 Learning
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
Learn Linux in 5 Days and Level Up Your Career


Other Linux Tutorials and Resources you may like
  • How to send mail with attachments from Linux? (mailx)
  • 10 example of Networking commands in Unix (nslookup)
  • 5 Example of kill commands in Unix and Linux (example)
  • How to find all files matching specific text in Linux (grep)
  • How does nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • How to use the netstat command to find which process is listening on a port? (example)
  • Linux find + du + grep example (example)
  • 10 Examples of curl command in Linux (cURL)
  • 10 Examples of chmod command in Linux (chmod)
  • A Practical Guide to Linux Commands, Editors, and Shell Programming (guide)

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.

P. S. - If you are looking for some free online courses to start your Linux journey, you should check out my list of Free Linux Courses for Programmers, IT Professionals, and System Administrators.

3 comments:

  1. crontab -l > backup_date.text
    =>
    crontab -l > "backup_$(date '+%F_%I_%M_%S%P').text"
    = §backup_2017-08-29_11_01_01am.text§, meaning the date is §YYYY-MM-DD_HH_MM_SS{a,p}m§ with 12h clock. The filename is thus compatible with Window$.

    ReplyDelete
    Replies
    1. @rautamiekaa, Thanks, yes, backup is much better with a date

      Delete
    2. And more reliable, no manual messing around with sequential numbers.

      Delete