2

I have the following script which I run using the command:

./thescript.sh 2>&1 &

and if I kill the sub process spawned by it, after a few seconds it is restarted. Why is this?

thescript.sh:

#!/bin/bash

#...
#other stuff
#...

while true; do
         nohup /usr/bin/php ../thing/script.php scriptargs >my_log.log 2>&1
        /bin/mail -s "$SUBJECT" "[email protected]" < $EMAILMESSAGE
done

the result of

     ps -ax | grep scriptargs

gives

19624 pts/0    S      0:00 /bin/bash ./thescript.sh
19643 pts/0    S      0:00 /usr/bin/php ../thing/script.php scriptargs
19771 pts/0    S+     0:00 grep scriptargs

if I run

kill 19643

I get:

./thescript.sh: line 24: 19643 Terminated /usr/bin/php ../thing/script.php scriptargs >my_log.log 2>&1

but if then run:

     ps -ax | grep scriptargs

again I get:

19624 pts/0    S      0:00 /bin/bash ./thescript.sh
19824 pts/0    S      0:00 /usr/bin/php ../thing/script.php scriptargs
19862 pts/0    S+     0:00 grep scriptargs

I get the email - but then it respawns that process.

Why is this?

1 Answer 1

3

This would be because of the loop

while true; do

done

When the process gets killed, it exits and allows the mail to be sent. The shell then hits the done statement and loops back the the while true which is erm true so it runs the commands within, again.

4
  • In other words: You kill just the sub-process so the main script keeps looping around the while and starts the script again. You will have to kill both the main process and the sub-process.
    – Tonny
    Oct 5, 2012 at 14:04
  • so I don't actually need a loop. The parent script will await the execution of the sub script.
    – Stevo
    Oct 5, 2012 at 14:30
  • @Stevo: Yes, if that's what you want.
    – user9517
    Oct 5, 2012 at 14:32
  • Cool - that's aided my understanding. I should have started my comment with a D'Oh!
    – Stevo
    Oct 5, 2012 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .