Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run a single PHP instance with CRON?

Tags:

php

cron

I'm having trouble running a single instance of a PHP script using CRON. Perhaps someone can help explain what is needed. Currenty, I have a startup script that is called by crontab which checks to make sure an instance of a PHP script isn't already running before calling the PHP instance.

crontab -e entry:

* * * * * /var/www/private/script.php >> /var/www/private/script.log 2>&1 &

./startup

#!/bin/bash

if ps -ef | grep '[s]cript';
        then
                exit;
        else
                /usr/bin/php /var/www/private/script.php  >>/var/www/private/script.log 2>&1 &
                echo 'started'
fi

This doesn't seem to be working and I can't seem to get any errors logged to know how to proceed to debug this.

like image 776
Xeoncross Avatar asked Jun 11 '26 04:06

Xeoncross


1 Answers

You can use lockrun for that: http://www.unixwiz.net/tools/lockrun.html

* * * * * /usr/local/bin/lockrun --lockfile=/var/run/script.lockrun -- php /home/script.php

Or use Perl:

system('php /home/script.php') if ( ! `ps -aef|grep -v grep|grep script.php`);
like image 170
Green Black Avatar answered Jun 17 '26 18:06

Green Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!