Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am downloading some software; I can see the pid of the download process with top. Let's say it's 1001.

How can I make it so that my computer shuts down after process 1001 exits?

share|improve this question

3 Answers 3

Here you go. tested and working.

while [ 1 ] ; do 
   ps 1001 > /dev/null || { shutdown -h now ; break ; }
   sleep 1
done
share|improve this answer
    
That may need to run as root, depending on config. Often non-root isn't allowed to run shutdown -h (and -r, etc.). –  derobert May 19 at 18:40
    
thanks,it works –  user2983983 May 20 at 3:17

This one-liner loop checks ps output for the process ID 1001, and sleeps for 2 seconds then checks again until it doesn't find it (the process has exited), at which point the loop ends and it moves on to the next command which is a shutdown.

while [[ $(ps 1001 &> /dev/null)$? -eq 0 ]]; do sleep 2 ;done && shutdown -h now

If you want to make sure that process 1001 is the process you think it is, you can add grep to test, for instance if the process should be iTunes, you could do:

while [[ $(ps 1001 |grep -q "iTunes" &> /dev/null)$? -eq 0 ]]; do sleep 2 ;done && shutdown -h now

share|improve this answer
    
@Ramesh I had this as a one liner, if you are going to edit it to be multiline then the semi-colons no longer make sense. I felt the benefit to having it as a one-liner was that he could run it in the Terminal easily, without having to worry about potential copy and paste carriage return/new line issues. –  Gregg Leventhal May 20 at 13:22
    
No problem. You can edit it as you want. But give some description of what the command does :) –  Ramesh May 20 at 14:02

Run the following commands:

while kill -0 1001 &> /dev/null; 
do
   sleep .1
done;
poweroff

Using kill -0 sends no actual signal, but will report true while a given process is running. The redirect &> goes to /dev/null (bit-bucket) so any errors or success messages are not included in the output of your program. This will sleep a tenth of a second and check the process until it is no longer running, then will execute a poweroff.

share|improve this answer
    
Using kill -0 sends no actual signal, but will report true while a given process is running. The redirect &> goes to /dev/null (bit-bucket) so any errors or success messages are not included in the output of your program. This will sleep a tenth of a second and check the process until it is no longer running, then willexecute a poweroff. –  remroot May 20 at 1:05

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.