Monday, July 25, 2022

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

Hello guys, If you have been using Linux for some time then you might know about cron jobs. They are the scheduler that 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 jobs but cron is a Linux command as opposed to Autosys which is a separate application altogether. In Linux, the crontab command is used for scheduling and automating jobs or processes. 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 the crontab -e option.

This puts 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 the Linux environment and essential commands themselves, then, I suggest you first go through these online Linux courses for beginners and fill that gap.

It's an excellent course that 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 from a file in Linux? Example

In order to demonstrate how to use the 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 the server and the 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, you can further see these compressive Linux courses for Programmers and System administrators.

how to backup and load crontab in Linux




How to backup cron jobs in Linux? Example

Before making any change to the 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 the 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 the 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 the 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 the VISUAL/EDITOR environment variable and crontab will pick that. You can further see these free bash online courses to learn more about how to schedule and automate jobs using cron in Linux.

Linux crontab examples for beginners


That's all about how to backup and upload crontab files 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 the 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 it's best to take a backup of existing crontab entries before uploading new jobs using this method. In the event anything goes wrong, you can always restore old cron entries from your backup.

Other Linux Tutorials and Resources you may like
  • How to send mail with attachments from Linux? (mailx)
  • 10 examples of Networking commands in Unix (nslookup)
  • 7 Best Linux courses for Developers and Cloud Engineers (Courses)
  • 5 Example of kill commands in Unix and Linux (example)
  • How to find all files matching specific text in Linux (grep)
  • 10 Best Courses to learn the Linux command line (courses)
  • How does the 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)
  • 5 Best Online courses to learn Linux in-depth (online courses)
  • 10 Examples of curl command in Linux (cURL)
  • 6 Websites to Learn Linux Online for FREE (websites)
  • 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 these UNIX crontab examples then please share them with your friends and colleagues. If you have any questions or comments 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.

4 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

Feel free to comment, ask questions if you have any doubt.