Take the 2-minute tour ×

I have a shell script which is executing a php script (worker for beanstalkd).

Here is the script:


if [ $# -eq 0 ]
   echo "You need to specify an argument"
   exit 0;

CMD="/var/webserver/user/bin/console $@";
echo "$CMD";
nice $CMD;

## Possibilities
# 97    - planned pause/restart
# 98    - planned restart
# 99    - planned stop, exit.
# 0     - unplanned restart (as returned by "exit;")
#        - Anything else is also unplanned paused/restart

if [ $ERR -eq 97 ]
   # a planned pause, then restart
   echo "97: PLANNED_PAUSE - wait 1";
   sleep 1;
   exec $0 $@;

if [ $ERR -eq 98 ]
   # a planned restart - instantly
   echo "98: PLANNED_RESTART";
   exec $0 $@;

if [ $ERR -eq 99 ]
   # planned complete exit
   echo "99: PLANNED_SHUTDOWN";
   exit 0;

If I execute the script manually, like this:

[user@host]$ ./workers.sh

It's working perfectly, I can see the output of my PHP script. But if I detach the process from the console, like this:

[user@host]$ ./workers.sh &

It's not working anymore. However I can see the process in the background.

[user@host]$ jobs
[1]+  Stopped                 ./workers.sh email

The Queue jobs server is filling with jobs and none of them are processed until I bring the detached script in the foreground, like this:

[user@host]$ fg

At this moment I see all the job being process by my PHP script. I have no idea why this is happening. Could you help, please?

Thanks, Maxime


I've create a shell script to run x workers, I'm sharing it here. Not sure it's the best way to do it but it's working well at the moment:


declare -A Queue

for key in "${!Queue[@]}"
  echo "Launching ${Queue[$key]} instance(s) of $key Worker..."
  for (( l=1; l<=${Queue[$key]}; l++ ))
        INSTANCE="$CMD $l"
        echo "lnch instance $INSTANCE"
        nice $INSTANCE > /dev/null 2> /dev/null &
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Background processes are not allowed to write to the terminal, which your script tries to do with the echo statements. You just need to redirect standard output to a file when you put it to the background.

[user@host]$ ./workers.sh > workers.output 2> workers.error &

(I've redirected standard error as well, just to be safe.)

share|improve this answer
Thanks mate, you made my day ! –  maxwell2022 Jul 27 '12 at 1:22
add comment

Your Answer


