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!"
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.

  • Please format your code (4 spaces before every line). Where did you save that code? Could you rephrase your question, it's not very obvious.
    – Lekensteyn
    Aug 24, 2010 at 19:26
  • next time i'll make sure i use 4 spaces thanks!!!
    – neolix
    Aug 25, 2010 at 9:25
  • make sure you dont make it a community wiki aswell unless it is needed.
    – Prix
    Aug 25, 2010 at 13:42
  • next time it not happens thanks for you help man !!!
    – neolix
    Aug 25, 2010 at 14:42

1 Answer 1


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

$pid = '/tmp/my_cron.pid';

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


Then at your cronjob have something like this:

23 0-23/2 * * * /usr/bin/php /path/to/my/php/file/my_cron.php
  • Its working for me your great man. I have one more question for, if suppose one job is already running that time i don't want run 2nd job 2nd job has go to queue and wait till 1st got over for what i have to add in your above code. Thanks a ton
    – neolix
    Aug 25, 2010 at 9:24
  • 1
    the above code will open only one instance at a time, if you try to run it again while it is running it won't. if you make a cron job to run every 5 minutes for example, you won't need it to be queue.
    – Prix
    Aug 25, 2010 at 13:43
  • ok let me try out and i'm using htop to check the pid is it ok!
    – neolix
    Aug 25, 2010 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.