Mysql – Cron job using MySQL/PHP

cronMySQLPHP

I have Ubuntu 10.04 server with Lightspeed (web server), PHP5, Mysql, TCPDF and sendmail i need to run the cron job for queue in case if any job is running already then it has to create queue, if not the then it will goto tcpdf folder sendmail.

for i got one code from serverfault

`PIDFILE=/tmp/`basename $0`.pid

if [ -f $PIDFILE ]; then
  if ps -p `cat $PIDFILE` > /dev/null 2>&1; then
      echo "$0 already running!"
      exit
  fi
fi
echo $$ > $PIDFILE

trap 'rm -f "$PIDFILE" >/dev/null 2>&1' EXIT HUP KILL INT QUIT TERM`

but i m not getting how to run the show can same help me on the same sorry for my English.

Best Answer

You could make it all in php, for example my_cron.php content:

<?php
$pid = '/tmp/my_cron.pid';

if (file_exists($pid)) {
    echo "Already running.";
    exit;
} else {
    exec ("touch $pid"); # create the pid file
    # do what you are supose to do here
}

unlink($pid);
?>

Then at your cronjob have something like this:

23 0-23/2 * * * /usr/bin/php /path/to/my/php/file/my_cron.php
Related Topic