How to Run Multiple Commands within the Same Cron Job

4

Manually executing duties is tiring and, in some circumstances, inapplicable. However, cron software permits a consumer to agenda quite a lot of jobs at quite a lot of occasions. You can agenda your server to create a backup weekly or some other job you deem essential. There is some way higher method of working a couple of instructions in a single cron task. You can agenda the entire duties in a single cron task. Want to discover ways to do this? Read on to determine.

Working with Crontab File

When you wish to have to agenda a role, you get started via defining the date and time, adopted via the command or script to run. That method, when the scheduled time involves go, the task will robotically execute.

Each consumer has a crontab record and you’ll create a cron task the use of the crontab -e command. In this instructional, we can create 3 cron jobs one at a time, then continue to look how we will be able to mix them into one cron task.

Creating Cron Jobs

We will create 3 cron jobs. The first executes a backup script. The 2nd will create a brand new record and the remaining will rename the created record if it exists. We will agenda the duties to run at other occasions however at the similar day the use of the instructions under.

As proven, we’ve added the cron jobs on the backside of the crontab record.

The downside with this configuration is that it stresses your CPU’s reminiscence in working the entire duties independently, and should you have been executing in depth jobs, it might drain your bandwidth. The answer is to run all 3 duties in the similar task.

How to Execute Multiple Commands in One Cron Job

You can use two choices to set a couple of instructions in the similar cron task.

1. Use the &&: the double ampersand specifies that the second one command must handiest run if the only sooner than it’s a hit. For example, the command under means that if the backup script runs effectively, a brand new record gets created at the /Desktop. And as soon as created, it is going to rename it.

2. Use a semi-colon (;): the semi-colon units the roles to run concurrently. Whether the primary run effectively or no longer, the only following it is going to run as every is impartial. In the instance under, the device will start via working the backup script. Once carried out, it is going to create a brand new record and rename it.

Depending on which job or script you need to execute, any possibility above will turn out to be useful in the use of a couple of instructions with one cron task. Note that the cron jobs will run concurrently, one at a time, relying at the possibility you select. Combining a couple of instructions is beneficial when the results of one determines how the following command must run.

Conclusion

This information covers how you’ll use a couple of instructions in a single cron task. We’ve observed use the && or semi-colon to set your cron jobs in a selected method. Besides, you’ll agenda a couple of jobs to run concurrently or according to whether or not those sooner than it used to be a hit. Using this information, you presently know the way to automate quite a lot of duties in a single cron task.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More