Linux Cron Job Tutorial

Here I will demonstrate how to add cron job to various Linux web servers to automate your daily, hourly or weekly tasks.

How to Set Up a Cron Job on Hostgator  and other shared Servers with Cpanel

Here is a screenshot of the cpanel page you use to add cron jobs in cpanel when using a Hostgator or similar server:

crons

So, yes, all you do is fill in the form and click the Add button, but you have to understand the settings, so you may need to read on. If you’re lucky, below the above box in cpanel, there are current existing cron jobs listed. You should be able to figure out how to fill in the form properly by following the examples if there are any. If not, you should be able to do it after readying this tutorial, so no worries.

Entering the cron command:

The cron command will consist of two parts:

  1. The location of the PHP installation on your server
  2. The path and filename of the script the cron job is to run

Simply combine those two objects with one or more spaces in between them to make up the cron command for the command field in the form you see in the image above. For example, here is what I used for the command field in the cron form:

/usr/bin/php /home1/gold2013/public_html/amazing-agents.com/crontest.php

Then in the first drop-down in the form, the one called “Common Settings”, select to run every 15 min. for testing or if you are confident and don’t want to test, set it for exactly how you want your cron job to be ran.

Test Cron Script

The following script is a great way to test cron jobs. All it does is creates a file called cronresults.txt or appends to it if it’s already been created. It writes a simple message with the time of the cron job to that file and that’s it. Nice and simple, the way I like it!

Copy and paste the following code into notepad and save it as crontest.php

<?php
$ctime = date(‘l jS \of F Y h:i:s A’);
$myFile = “cronresults.txt”;
$fh2 = fopen($myFile, ‘a’) or die(“can’t open file to append”);
$stringData = “appended text from cron job ran at $ctime….\n”;
fwrite($fh2, $stringData);
fclose($fh2);
?>

Upload crontest.php to an active web directory using your favorite FTP client.  Make sure the path in your cron job matches the directory you upload to and you will be good.

Now wait 15 minutes or however long you set your cron job to run and check to see if the file named cronresults.txt was created in the same directory that you put crontest.php. Download cronresults.txt and you should see a list of the times that your test cron job has ran. If it didn’t work, try changing the permissions of crontest.php to 777 while testing and tighten up security asap.

Cron eMail Notices

One nice thing about cron is that it sends you an email every time it runs. Make sure the email in Cpanel, just above the cron form, is set to the correct email where you can receive emails. If there is an error in the cron job, that email message is the easiest place to read the error information. Yes, errors are emailed to you! You can suppress the emails once your cron job is set up and error-free however.

How to set up a cron job from Amazon ec2 cloud server

We used the basic Linux 64bit AMI but these directions should work for any Linux web server. Here are the simple steps to creating a cron job on a Linux server:

First, open a command prompt using Putty or a command window from your desktop if you have access that way.

From the command prompt, type:

which php
returns:/usr/bin/php

using the response from which php command above, add the proper shebang to your PHP script like so:

#!/usr/bin/php -q

the  -q parameter makes it so you don’t get an email every time cron job runs.

Continuing from the command prompt, type:

crontab -e

Then use VI editor to add the following line to crontab which will run  your PHP script every minute:

* * * * * /usr/bin/php /home/ec2-user/crontest.php

or every hour:

0 * * * * …

The five cron positions are as follows:

minute         0-59
hour           0-23
day of month   0-31
month          1-12
day of week    0-7 (sun = 0 or 7)

Start a PHP file named crontest.php and enter the following contents:

———————-code—————————-
#!/usr/bin/php -q
<?php
//script to test cron on adam’s cloud server:
$message = “If ur reading this then the cron job worked!!!!nn”;

$myFile = “testFile.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);

fwrite($fh, $message);
$stringData = “Coming to you from cloud cron! the script that sent this was in home/ec2-user/crontest.phpn”;
fwrite($fh, $stringData);
fclose($fh);
?>
——————————————————–

Start another PHP file named testFile.php. It will just be a blank .txt file with no content at all.

Then upload crontest.php and testFile.php to /home/ec2-user and change permissions of both to 755 or 777 if 755 doesn’t work but 777 can cause a security issue.

Important note: it seems that if you change the regular crontab using crontab -e call that the files have to be in the user’s directory that you are in when making the call which in this case is user ec2-user.

To switch user use:

su ec2-user

Then do your crontab -e command again and it will work.

Cron Job Examples:

To run a cron job once a day, the syntax would be:

minute hour * * * command

so just replace minute with a number from 0 to 59 and the hour with a number from 1 to 24 as in the following working example:

30 9 * * * /usr/bin/php /home/ec2-user/crontest.php

The above example would run the crontest.php script once a day at 9:30am.

Running a cron job every 15 minutes

 0,15,30,45 * * * * /path/to/command

or

*/15 * * * * /path/to/command

 

 

 

 

 

2 Replies to “Linux Cron Job Tutorial”

Leave a Reply

Your email address will not be published. Required fields are marked *