Schedule Multiple Cron Jobs in One Crontab

How to Schedule Multiple Cron Jobs in One Crontab

Cron jobs are tasks scheduled in Linux systems that run automatically at their scheduled times. They need to be included in crontab document. 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 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 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.

Now within a crontab file, you can organize each crojob 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.

5 * * * * echo 'hello world'
10 * * * * echo 'good morning'
15 * * * * echo 'how are you'

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 at a different time while in second example, all cron jobs run at the same time.

While we have learnt how to run multiple 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.

In this article, we have learnt how to schedule multiple cron jobs in same crob tab.

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

Leave a Reply

Your email address will not be published.