Cron – automated task using php

cron automated task phpFor last few weeks I have been working in a web application, where I’ve to do some automated tasks. This is the first time I learned cron process, wrote php script for the cron process and some basic shell script that runs the php script. So here I’m describing the things I learned.

Summary of the tasks:

  1. Write a php script
  2. Test the script
  3. Confirm only one process of the php script is running
  4. Set the cron so that the php script will run in every 2 minute

So lets see the flow chart of the task:

Task example

Write a php script:

The script is as usual you write for normal web application. Here one thing you’ve to keep in mind that, when you use path, it have to be absolute, not relative. This is true for when you run the script as cron not for your localhost. For example: you have to include the code config.php in the script, so if you write the script

   include_once "config.php" //get configuration

It will work when you test the script from shell within the same directory of the file, but this will not work when you run the script from cron process in server. So the modified example would be:

   include_once "/var/www/domain/yourprojectpath/config.php" //get configuration

Testing the script

After writing the script just run the script from shell either in your pc or in server to test if everything is working fine or not. If you don’t know how to run php from shell then learn it. Its very easy to run php script from shell. The command is:

php cronscript.php

Confirm only one process of the php script is running

To do this there is a shell script using that I run the php script.

#author:mahmud ahsan
#shell script that will run php script

if ps -ef | grep -v grep | grep cronphp.php ; then
       echo 'Cron already running'
       exit 0
       echo 'Run Cron'
       php cronphp.php #if you run the script from shell within the same file directory
       php /var/www/domain/yourprojectpath/cronscript.php # when you set the script as cron
       exit 0

So set this script as a cron process. Now when this scripts run by cron, it will check if the php script as a process already running or not. If running it exists otherwise it run the php script. Keep in mind that when you set this as a cron process, you definitely provide the absolute path of the php script. Otherwise it will not work.

Set the cron so that the php script will run in every 2 minute

There is a very good tutorial to learn about basics of cron and how to set it. Just follow the tutorial


Tags: , ,
mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like


  • bashir
    March 31, 2011 at 10:02 am

    good job !

  • Hoan Huynh
    April 13, 2011 at 5:00 pm

    Thank you for sharing, good staring 🙂

  • Endri
    May 17, 2011 at 1:01 am

    Waw..nice article,very helped for person like me:) Thanks for share this

  • Gehrings
    May 17, 2011 at 1:02 am

    Great Job,Dude:)

  • vishal
    August 29, 2011 at 1:33 pm

    its nice 1……

  • Software en la Nube (@kopernet)
    December 20, 2012 at 1:27 am

    Great Post! Thank you very much

  • automated traffic bot
    June 26, 2013 at 1:29 am

    I don’t know if it’s just me or if everyone else experiencing issues with your website.
    It looks like some of the text within your posts are running off the screen.
    Can somebody else please provide feedback and let me know if this is happening to them too?
    This could be a issue with my web browser because
    I’ve had this happen before. Appreciate it