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 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" "me@home.com" < $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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 14:30
    
@Stevo: Yes, if that's what you want. –  Iain Oct 5 '12 at 14:32
    
Cool - that's aided my understanding. I should have started my comment with a D'Oh! –  Stevo Oct 5 '12 at 14:37
add comment

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.