0

I am running a cron job to execute a php file multiple times per minute like this

*  *  *  *  * /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
*  *  *  *  * sleep 10; /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
*  *  *  *  * sleep 20; /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
*  *  *  *  * sleep 30; /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
*  *  *  *  * sleep 40; /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
*  *  *  *  * sleep 50; /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null

This executes myFile.php fine but when i count the number of process running
ps -ef | grep php | wc -l
It adds up and cpu usage hits 100%!

Should i be running a single bash file and from within the bash file run multiple php files staggered?

If so how would I write the bash script?

#!/bin/bash
????
????
????

edit: I would like to mention that the execution of the file should take place after the previous execution is complete.

Also a cron job is not mandatory as krisFR has pointed out

  • 1
    Wow, i think you should run a single script in background (nohup) with a loop and sleep – krisFR Jul 30 '14 at 20:38
  • @krisFR thank you for the advice. How would you write that in bash? – t q Jul 30 '14 at 20:44
1

I don't know what your script is actually doing, maybe it requires a lot of server resources, and for sure the next one is starting before the previous one has finished. Is it what you want ? I will guess "no".

I would suggest to run a single Bash script, something like :

#!/bin/bash
while true; do
   /usr/bin/php /var/www/html/myFile.php >> /var/www/html/cronlog 2> /dev/null
   sleep 10
done

Let's consider you put the above code in file /home/me/myscript.sh you can run it in background using :

nohup /home/me/myscript.sh &

Test and check if it feets your need.

  • thank you will try it out! yes sometimes it requires a lot of server resource and preferably i would like the next one to start after the first process has finished – t q Jul 30 '14 at 20:51
  • when you say single script in background(nohup) would this work for the cron? * * * * * sudo /usr/local/bin/myScript.sh httpd /var/www/html/cronlog > /dev/null 2>&1 – t q Jul 30 '14 at 20:55
  • It could but it is not my advice, it will be worst ! But warning, the sample script i've provided will execute the PHP script every 10 seconds after the previous one has finished : so, depending on the time your PHP script takes to execute, you can have more or less than 6 executions per minutes – krisFR Jul 30 '14 at 21:00
  • i see, can you recommend a better way of writing the cron please. – t q Jul 30 '14 at 21:05
  • 1
    I've updated my answer. Cron is not necessarily the way to go. Ask yourself and adjust your initial question accordingly : "Do i want a straight number of execution per minutes ? if yes, how many ? What do i do if previous execution is not finished yet when next starts ?" – krisFR Jul 30 '14 at 21:26

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.