Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I've created a while loop in a bash script to run a couple of PHP commands. The reason why I've created the while loop is because I can't use cronjobs (I don't want to create a new instance of the bash script while the other one is still running).

Previously I ran the command using screen but it seems eventually my resources were all eating up by this process.

So my question is; what is a good alternative to run the bash script?

Script is similar to:

echo "Starting daemon.."

while true; do
   echo "Running runJobs.."
   cd /var/www/wiki && nice -n 10 /usr/bin/php maintainAll.php maintainwiki runJobs >> /var/log/runJobs.log
   echo "Sleeping 60 seconds.."
   sleep 60
share|improve this question
What are you trying to accomplish? – l0b0 Jan 29 '14 at 12:31
A script that runs php scripts forever. – Youri Jan 29 '14 at 12:41
I think l0b0 meant: What do you want to do with "running PHP scripts forever"? How are you going to use that? – edwin Jan 29 '14 at 12:56
As @edwin said, It depends on you applications/objectives. For in my case when I want to run a script which infinite loop with lower cpu resource, I use sleep command to make delays and renice processes to a lower priority after I launch them in background using &. – Sneetsher Jan 29 '14 at 13:41
Its for running maintenance scripts – Youri Jan 29 '14 at 13:42

1 Answer 1

A good alternative is to let PHP do the looping, rather than doing a looped call to PHP.

Really, as soon as you have that, all you need to do then is have Ubuntu keep a copy of the script running in some form. That can be accomplished with a super-simple Upstart script. You could put this in /etc/init/myphpscript.conf:

description "My PHP script"

start on startup
stop on shutdown

exec sudo -u www-data -- php -f /path/to/script.php

Note that as soon as PHP exits, Upstart is going to pump out another copy so if you don't put a sleep in the PHP, you'll be in a very quick, possibly very expensive (I don't know what your code does) loop.

share|improve this answer

Your Answer


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.