How to configure Cron job

What is Cron?

Cron is a system utility that allows you to schedule your jobs or commands at a certain time on the server.

How to configure a cron job?

DirectAdmin

  1. Go to your client area to the “Services – My Services” section and click the “Open Direct Admin” button;
  2. The Direct Admin panel will be opened. Go to the “Advanced Features” section and click the “Cron Jobs” button;
  3. Click the “Create Cron Job” button and specify the following information;
  4. A new window where you can specify the following information will be opened;
    • Minute – any integer from 0 to 59;
    • Hour – any integer from 0 to 23;
    • Day of Month – any integer from 1 to 31 (use the correct day if the month is specified);
    • Month – any integer from 1 to 12;
    • Day of Week – any integer from 0 to 7 (0 and 7 equals Sunday);
    • Command – your job or command which must be executed. The command must contain the path to the handler and the path to the script that will be executed by this handler;
  5. The “Prevent E-Mail” button adds “>/dev/null 2>&1” to the end of your command. This means that the Cron will not be sending messages to your email every time after this job is executed;
  6. Once all the information is specified, click the “Create” button. Done.

Specify the address of the executable file in this format:

/usr/bin/php -f /home/login/domains/domain_name/public_html/script.php

An example of a command to run through the wget utility:

/usr/bin/wget -t 1 -O - 'http://domain_name/script.php'

There is also the “Send all Cron output to e-mail” link in the “Cron Jobs” section. This function allows you to receive reports to your email when the cron job is executed. This report contains both successful and unsuccessful results, which is very useful for error tracking. Click this link, type in your email like manager@domain.com, and click the “Save” button. After this, the Cron will be sending reports to your email after every execution of your cron jobs (except those with “>/dev/null 2>&1” at the end).

cPanel

  1. Go to your client area to the “Services – My Services” section and click the “Open cPanel” button;
  2. The cPanel will be opened. Go to the “Advanced” section and click the “Cron Jobs” button;
  3. cPanel by default sends an email each time a cron job runs. It also redirects the output of the command or file into the email. The default email for this is your system account, but you can change this email too. To change the email on which you want to receive cron email, firstly find out the Cron Email interface in Cron Jobs. Then enter your new email in which you wish to get cron emails. Click “Update Email” to update your email;
  4. Scroll down to see the “Add New Cron Job” interface;
  5. You can specify the following information:
    • Common Settings- the frequency of the job. Select one of the options from the drop-down list or specify the required parameters manually;
    • Minute – any integer from 0 to 59;
    • Hour – any integer from 0 to 23;
    • Day of Month – any integer from 1 to 31 (use the correct day if the month is specified);
    • Month – any integer from 1 to 12;
    • Day of Week – any integer from 0 to 7 (0 and 7 equals Sunday);
    • Command – your job or command which must be executed. The command must contain the path to the handler and the path to the script that will be executed by this handler;
  6. After completing the data entry, click the “Add New Cron Job” button.

Specify the address of the executable file in this format:

/usr/bin/php -f /home/login/path_to_script/script.php

An example of a command to run through the wget utility:

/usr/bin/wget -t 1 -O - 'http://domain_name/script.php'

If your cron runs more frequently, then you may not want to receive emails, you can disable receiving emails by redirecting your output into null by writing >/dev/null 2>&1 at the end of the command.

To edit or delete your existing cron jobs, you can scroll below on the “Current Jobs interface” to see current cron jobs. Once you select the job, you can click either the delete link or the edit link to edit or delete a particular cron job.

ISPmanager

  1. Go to your client area to the “Services – My Services” section and click the “Open ISPmanager” button;
  2. You can add a cron job by going to the section “Dashboard – Scheduler (cron)”;
  3. Click the “Add” button at the top of the center column;
  4. Fill in the fields for the cron:
    • Email – to receive messages from the system about the results of script execution;
    • Command – your job or command which must be executed. The command must contain the path to the handler and the path to the script that will be executed by this handler;
    • Description – provide any information related to this cron job;
    • Scheduler  – select the time frame of the cron job. Basic mode allows to choose a variant, the expert mode allows to enter a certain value;
    • Do not email report – check the box if you do not want to receive an email notifying you of a specific cron job;
  5. Click the “Ok” button.

Specify the address of the executable file in this format:

/usr/bin/php /var/www/login/data/www/domain_name/script.php

An example of a command to run through the wget utility:

/usr/bin/wget -t 1 -O - 'http://domain_name/script.php'

To check any of the cron jobs, select the one you need from the list and click the “Run” button. In the window that will appear you will see the cron job results.

Plesk

  1. Go to your client area to the “Services – My Services” section and click the “Open Plesk” button;
  2. The Plesk panel will be opened. Go to the “Websites and Domains” section and click the “Scheduled Tasks” button from the right-hand menu;
  3. Click the “Add Task” button;
  4. You need to select the task type, depending on your purpose:
    • Run a command – specify the full path to the executable file to be run. This includes binary files, shell scripts, and batch files;
    • Fetch a URL – specify the URL only; there is no need to type the command, such as “curl” or “wget”;
    • Run a PHP script – specify the path to the script relative to your virtual host directory. You can click the folder icon to quickly locate the script file;
  5. The following fields are the same for all task types:
    • Run – select the frequency of the task and schedule the execution time;
    • Description – provide any information related to this cron job;
    • Notify – select the type of notification;
  6. After entering the data, you need to click the “OK” button.

Finally, before saving the task, you have the option to do a test run to make sure that the task is configured correctly. Click the “Run Now” button and wait for the task to finish. If the task finishes with an error, it will also fail to work when executed by the scheduler, unless you find and resolve the issue first.

By default the time is in the server time zone; if you need to select a different time zone, go to the “Websites and Domains” tab > “Scheduled Tasks” > “Settings”. You can also set a description for the task, and configure notifications.

How to set time for the Cron job correctly?

  • The asterisk (*) can be specified for any time field. It means all possible values for this time field. For example, an asterisk in the hour field would be equivalent to ‘every hour’
  • The dash (‘-‘) can be specified for a range of values. For example, 1-4 is equal to every value in this range (1,2,3,4,);
  • The comma (‘,’) can be specified for a list of values. For example, value 2,4,6,7 means that your command will be executed at this certain time values (the values must be listed without spaces);
  • The slash (/) is used for step values in a specific range of values. For example, value 2-19/3 means that your command will be executed every third minute between the 2nd and 19th minutes (equals 2,5,8,11,14,17). The asterisk can be used here as well to define all the possible values in a specific range. For example, value */10 means that your command will be executed every tenth minute between the zero and 59th minutes (equals to 0,10,20,30,40,50);
  • All the specified methods can be combined to tune your execution time more accurately. For example, value 1,5,11-15,30-39/2 is equal to 1,5,11,12,13,14,15,30,32,34,36,38 minutes.

Examples of Cron Jobs

Execute the Perl script every day at 16:10 and send a report to email:

10 16 * * * /usr/bin/perl  /home/username/bin/yourscript.pl

Execute the bash script each Saturday at 2:00 and only send error reports to the email:

0 2 * * 6 /home/username/weekly/weekly-pruning.sh >/dev/null

Execute the PHP script every hour and do not send any reports to the email:

0 * * * * /usr/bin/php /home/username/cron.php >/dev/null 2>&1
Rate this article
Share this article
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
In this article