Prevent CRON from running PHP code more than once

Prevent CRON from running your PHP code more than once by using this simple Bash hack!

YouTube player

CRON will execute your PHP code regardless of its success or failure. Slow running PHP code could eventually result in many simultaneous processes running the same PHP code. This Bash script will only run the PHP code if it is not running, thus avoiding a race condition.

The Bash script:

#!/bin/bash

#find existing processes with the same name
processId=`ps aux | grep myPhpCode.php | grep -v grep`

#note the ` character is deliberate and should not be swapped with a ' (single quote)

if [ -z "$processId" ]; then

    #starting your code, if no existing process was found
    /usr/bin/php /var/www/myPhpCode.php >> /var/www/myPhpCode.log &

    #note the >> operator appends the output of your code.
fi

Note that you must change:

  • ‘myPhpCode.php’ to the name of your PHP code.
  • ‘/usr/bin/php’ to the path of your PHP install.
  • ‘/var/www/myPhpCode.php’ to the path of your PHP code.
  • ‘/var/www/myPhpCode.log’ to the path and name of the PHP code log file.

Run the Bash script after you set execute permissions on it. You will see the log file created when the script is executed. The log file will capture the output from your PHP code. Running the Bash script will only result in the PHP code run once; if there is no current process for the PHP code.

How does this Bash script work?

This Bash script uses the ‘ps’ command (short for ‘process status’), which is used to get detail about the current Linux processes. We add the ‘aux’ argument to the ‘ps’ command to add more information. The ‘aux’ argument requests process information for (a) all users, (u) add the user/owner column, and (x) processes that were not executed by the terminal.  

We then use the ‘grep’ command to filter the result of the ‘ps’ command for processes that contain the same name as your PHP script. The ‘grep’ command with the ‘-v’ argument subsequently filters the result and removes the ‘grep’ process.

The ‘processId’ variable stores the result from the above-mentioned steps.

The ‘If -z’ statement returns true if the string stored in the ‘processId’ variable is empty. Alternatively, the ‘If -z’ statement will return false.

Your PHP script will only execute if the Bash script cannot find existing processes with the same name. Use the Bash script to execute your PHP script via CRON instead of using CRON to run your script directly.

Wrapping up

Now you can prevent CRON from running your PHP code more than once!

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.