| by Arround The Web | No comments

How to Run Multiple Commands in the Same Cron Job

Manually executing tasks is tiring and, in some cases, inapplicable. However, cron utility allows a user to schedule various jobs at various times. You can schedule your server to create a backup weekly or any other task you deem necessary. There is a way better way of running multiple commands in one cron job. You can schedule all the tasks in one cron job. Want to learn how to do that? Read on to find out.

Working with Crontab File

When you want to schedule a job, you start by defining the date and time, followed by the command or script to run. That way, when the scheduled time comes to pass, the job will automatically execute.

Each user has a crontab file and you can create a cron job using the crontab -e command. In this tutorial, we will create three cron jobs separately, then proceed to see how we can combine them into one cron job.

Creating Cron Jobs

We will create three cron jobs. The first executes a backup script. The second will create a new file and the last will rename the created file if it exists. We will schedule the tasks to run at different times but on the same day using the commands below.

$ crontab -e

As shown, we’ve added the cron jobs at the bottom of the crontab file.

The problem with this configuration is that it stresses your CPU’s memory in running all the tasks independently, and if you were executing intensive jobs, it could drain your bandwidth. The solution is to run all three tasks in the same job.

How to Execute Multiple Commands in One Cron Job

You can use two options to set multiple commands in the same cron job.

1. Use the &&: the double ampersand specifies that the second command should only run if the one before it is successful. For instance, the command below implies that if the backup script runs successfully, a new file will get created on the /Desktop. And once created, it will rename it.

2. Use a semi-colon (;): the semi-colon sets the jobs to run simultaneously. Whether the first run successfully or not, the one following it will run as each is independent. In the example below, the system will begin by running the backup script. Once done, it will create a new file and rename it.

Depending on which task or script you wish to execute, any option above will come in handy in using multiple commands with one cron job. Note that the cron jobs will run simultaneously, one after the other, depending on the option you choose. Combining multiple commands is helpful when the result of one determines how the next command should run.

Conclusion

This guide covers how you can use multiple commands in one cron job. We’ve seen how to use the && or semi-colon to set your cron jobs in a specific way. Besides, you can schedule multiple jobs to run simultaneously or based on whether the ones before it was successful. Using this guide, you now understand how to automate various tasks in one cron job.

Share Button

Source: linuxhint.com

Leave a Reply