Cron job setup on Linux system

How to setup cron jobs on linux?

First, let us take a look at the basic syntax.

It is a string consisting of two parts. One part determines when the job will be run, and the other specifies what to run. When to run The time part consists of five subparts like this:
* * * * *
Where each subpart specifies a unique timescale in the following order:
Minute (0-59)
Hour (0-23)
Day of month (1-31)
Month (1-12)
Day of week (0-6 where 0 is Sunday)

By just adding an asterisk (*), the cron job will run at every point in the specific timescale.
So by saying:
* * * 2 *

We will be scheduling our cron job to run every minute, every hour, every day of the month during February.If we want to make it run every 10 minutes, we can write the following:
/10 * * * *

Where it is important to notice the slash, which is responsible for the behavior.
What to run The second part of the string specifies the command to run. In this case, we will just be adding the path to a shell script that we will be running. It could for instance be: /var/cronscripts/

If we would like to run the “” shell script. Instead of linking to another script, you could chose to add the entire command to run instead.

Step 1 – Add the script to run

To better manage and test our cron jobs, we will be adding all the scripts in a folder. This will allow us to test each script individually before adding them to cron. It also gives more flexibility over how many commands you will be able to add to you script.

You can create the desired folder using the mkdir command like this:

sudo mkdir /path/to/dir

To create the file, this can be done using the nano text editor

sudo nano /path/to/file

Cron is running the script as the current user, so in order for this to work, it is very important that the user has rights to execute the script. This can be done by running the following command:

sudo chmod 755 /path/to/file

Step 2 – Adding the cron job

To add the cron job, run the following command:

crontab -e

This will open a file for editing, where your can add you cron job like

* * * * * /path/to/file

After having added the cron job, you will have to restart the cron service with the following command:

sudo service cron restart

To confirm that the cron job has been added, run the command below and look for it on the list.

crontab –l

Add logging
An optional step, is to add logging, to help determine when the cron job was run. In the following example, the start and stop times are logged to one log and the output of the command to another.

php -f /path/to/scripts/datelogstart.php >> /path/to/logs/myscript.wasrun.log
php -f / path/to/scripts/myscript.php >> /path/to/logs/myscript.log
php -f / path/to/scripts/datelogend.php >> /path/to/logs/myscript.wasrun.log

The content of datelogstart.php is as follows:

echo '[Start]: '. date('Y-m-d h:i:s')."\r\n";

And the content of datelogend.php is:

 echo '[End]: '. date('Y-m-d h:i:s')."\r\n";


For more information, visit the link below:

11 Cron Scheduling Task Examples in Linux - Tecmint'

Bjarne Bonde