Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '10 at 19:26
    
next time i'll make sure i use 4 spaces thanks!!! –  neolix Aug 25 '10 at 9:25
    
make sure you dont make it a community wiki aswell unless it is needed. –  Prix Aug 25 '10 at 13:42
    
next time it not happens thanks for you help man !!! –  neolix Aug 25 '10 at 14:42

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
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 '10 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 '10 at 13:43
    
ok let me try out and i'm using htop to check the pid is it ok! –  neolix Aug 25 '10 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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