Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

On a regular basis I forget to add && shutdown -h now to a long-running process. Is there any way to add a shutdown -h now command after the first command was invoked? It is not intended to interrupt the ongoing process. Maybe it is possible to watch the PID?

share|improve this question
    
I hope you realise that whatever comes after && will only run if the previous command was successful. If you want it to run even if the command fails use ; instead. –  Zoke Jan 30 '12 at 2:49

5 Answers 5

up vote 5 down vote accepted

There's no need to repeatedly run ps to list all processes and grep through the output. Background the process with ctrl-Z, then run

bg %1 ; wait %1 ; shutdown -h now

If you have other background jobs running, then you will be given a different jobspec instead of [1] when you ctrl-z. If so, use that instead.

share|improve this answer
    
+1, Cleanest answer yet. –  Caesium Jan 30 '12 at 3:40

You could do something like this, which will check whether the process is running every 5 seconds and take appropriate action:

while true; do
  if ps -ef | grep -q '[l]ong-running-process'; then
    sleep 5
  else
    sudo poweroff
  fi
done

Note the square brackets in the grep expression. This is to ensure that the grep process doesn't get matched if it ends up in the ps list. Surrounding any single character with square brackets is sufficient.

share|improve this answer
    
+1 since it works. Not the way I would have done it tho. –  Zoke Jan 30 '12 at 2:44
    
@Zoke: If there's another way, I'd be interested. –  Scott Severance Jan 30 '12 at 2:54
    
Posted an answer as well. Check it out if you like. –  Zoke Jan 30 '12 at 2:56
    
You can also use kill -0 (pid) to check for existence of a process without killing it. –  fluffy Jan 30 '12 at 6:15

If You know the pid you can run this:

while ps -p $PID; sleep 5; done; shutdown -h now

To run without echo from ps do this:

while ps -p $PID >/dev/null; sleep 5; done; shutdown -h now

And finally to run it in the background without any output:

(while ps -p $PID >/dev/null; sleep 5; done; shutdown -h now ) &

Scott's solution is a good one but this removes the need to grep over and over if you already know the pid. And it's a lot quicker to type in. :p

share|improve this answer

I do this all the time.

  1. Start the process.
  2. Realize that I forgot to do my && command.
  3. Hit Ctrl+Z to suspend the process and return to the shell.
  4. Run fg && shutdown -h now to:
    1. Foreground and resume the process you just suspended.
    2. Then, after it finishes, it'll run what you wanted to run.

Alternatively, you can poll as suggested in other answers like this:

while ps -p $PID ; do sleep 5 ; done ; shutdown -h now
share|improve this answer
    
That will shutdown right away. –  psusi Jan 30 '12 at 3:00
    
The fg completes right away, so... –  psusi Jan 30 '12 at 3:05

Make a shell script to run both processes:

MyShellScript.txt:

#!/bin/sh
MyProcessIdLikeToStart -o options && shutdown -h now

After saving this in the directory where you start the given process, do a:

chmod +x MyShellScript.txt

Then when you want to run the command just type ./MyShellScript.txt and it'll do both for you.

share|improve this answer

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.