Creating and Using Cron Jobs

A Cron Job is a Linux command for scheduling a task (command). Cron Jobs allows you to automate certain commands or scripts on your server to complete repetitive tasks automatically. 

This can be a very resourceful tool as a Cron Job can be set to run by 15 minute or hourly increments, a day of the week or month, or any combination of these. 

For example, you could set a Cron Job to delete temporary files every week so that your disk space is not being used up by unnecessary files. Some scripts, such as Drupal, may even require you to set up a Cron Job to perform certain functions

  1. Log in to DirectAdmin. 
  2. Go to Advanced Features > Cron Job. 
  3. Click on the CREATE CRON JOB button 
  4. Enter the time you want your cron to run. You can select the minute, hour, day of the month, month, and/or day of the week. To run the command every minute, hour, day, etc. use the wildcard symbol * in place of a number.
  5. Enter your cron command. This can be a Linux command, or you can execute a script that is stored somewhere on the server by entering the path to the script (e.g. /home/username/deleteOldLogs.sh).  
  6. If you check the “Run on @reboot” option the cron will be executed only once – on reboot
  7. When you’re done editing your cron, click “Create”. You can come back to edit it if you need to using the pencil icon at the end of the row.
  8. Optionally, you can enter your email address in the section ‘Send all Cron output to E-mail’ to receive email notifications with the cron output. Click the ‘Save’ button to save the details. 
  9. A cron can be deleted by selecting it and clicking on the “Delete” button.
Common Cron Commands
PHP 

Command to run a PHP5 cron job: 

php /home/username/public_html/cron.php

Optional flags are sometimes required for a PHP cron job: 

php -q /home/username/public_html/cron.php

Command to GET a remote file: 

/usr/bin/GET http://www.example.com/file.php

Command to use a specific php.ini file:

php -c /home/username/public_html/php.ini /home/username/public_html/myscript.php
Specifying the php.ini File 

Some scripts executed with a Cron Job require a specific php.ini file to be used. 

When executing a PHP script from a Cron Job (or via the command line), you can use a custom php.ini file. The most common situation where a custom php.ini file is needed is when a user’s code requires access to something such as Zend Optimizer, which is specified in the php.ini file. 

To specify an alternate php.ini file, you’ll need to use the -c flag in your PHP command line. 

For example: 

php -c /home/username/public_html/php.ini /home/username/public_html/myscript.php 

In this instance, /home/username/public_html/php.ini is the full path to the php.ini file you’d like to use and /home/username/public_html/myscript.php is the full path to the php script you’d like to run. Using your actual username, change the path to the php.ini and the file you intend to run accordingly 

Perl 

Command to run a CGI cron job: 

perl /home/username/public_html/cgi-bin/file.pl
BASH 

Command to run a shell script cron job: 

/bin/sh /home/username/public_html/file.sh
Restarting Cron

You can restart a cron service from a command line with SSH.

  1. Login to your server via SSH. 
  2. With “systemctl status crond” command you can see if service is running. 
  3. For restart run “systemctl restart crond”, it will take a few moments 
  4. You can now check the status of service again with the “systemctl status crond