Cron jobs are tasks scheduled in Linux systems that run automatically at their scheduled times. They enable Linux developers and system administrators to easily automate tasks and system management. Often, beginner administrators wonder whether they need to create a separate crontab for each cron job, and if they can schedule multiple cron jobs in one crontab. In this article, we will learn how to do this.
How Crontab Work
In order to run multiple cron jobs, you need to understand how crontab file works. Crontab stands for Cron Table and contains a list of cron jobs to be executed on your system, one cron job per line. Each line of cron tab file specifies a cron job to be executed at specific time. A crontab can have multiple cron jobs listed one below the other and a system can have multiple crontab files.
Crontab files are stored in different locations, depending on the type of Linux you use. They are mostly located at /var/spool/cron/crontabs with one file for each user named after the account’s username. Only a superuser will be able to edit other user’s files. A regular user can only modify their own file. In many systems, you will also find crontab files at /etc/crontab and /etc/cron.d. You can access your crontab file using ‘crontab -e’ command so you do not have to worry about these locations. When you boot or reboot your system, the cron process will look for crontab files in all the above locations and automatically schedule & run cron jobs mentioned in them.
How to Schedule Multiple Cron Jobs in One Crontab
If you need to schedule cron job, you need to open crontab document using the following command. It will open the crontab document in your default text editor.
$ crontab -e
If you want to change the default text editor of your system, then run the EXPORT editor command shown below and then run above command. Here is an example to set default editor to vi editor.
$ export EDITOR=vi
Once the crontab -e is run, it will open the document in a text editor. Each line will contain a cronjob of the following format.
If the above syntax seems confusing, then there are several online third-party tools to generate cron job commands. Now within a crontab file, you can organize each cronjob one below the other such that you have one cron job on each line, or you can organize them one after the other on a single line where the cron jobs are separated by semi colon(;), or it can be a combination of both these approaches.
Here is an example to include a different cron job on separate line. They run at different times.
5 * * * * echo 'hello world' 10 * * * * echo 'good morning' 15 * * * * echo 'how are you'
If you need to disable a specific cron job then add ‘#’ at its beginning. Here is an example to disable the second cron job.
5 * * * * echo 'hello world'
#10 * * * * echo 'good morning'
15 * * * * echo 'how are you'
Sometimes Linux system administrators need to run multiple jobs at once. Here is an example to run multiple cron jobs at the same time.
5 * * * * echo 'hello world'; echo 'good morning'; echo 'how are you' OR 5 * * * * echo 'hello world' && echo 'good morning' && echo 'how are you'
In the first example, each cron job runs one after the other while in second example, all cron jobs run at the same time. In the first case, each job is running without waiting for the exit status of the previous command. In second case, a job is run only if the exit status of previous job is 0, that is, it was executed successfully.
While we have learnt how to run cron jobs in a single file, you should also remember that it is possible to have different crontabs for each user on your system, each with a separate list of cronjobs. So the file you see on running ‘crontab -e’ command is only your file, with the cron jobs that you have scheduled. Other users will not be able to see and you will not be able to view their files unless you are superuser. Each user will have their own crontab file with its own cron jobs.
Conclusion
In this article, we have learnt how to schedule several cron jobs in same crob tab. Basically, all you need to do is open crontab file using using ‘crontab -e’ command and list the different cron jobs you want to run, one below the other. If you want to disable a cron jobs just add ‘#’ at the beginning of that line. You can also combine several cron job commands on a single line to run them at the same time.
Also read:
How to POST JSON Data in Python Requests
How to Remove SSL Certificate & SSH Passphrase
How to Capture Linux Signal in Python
How to Send Signal from Python
How to Clear Canvas for Redrawing in JavaScript
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.