How can I add to a already running process an additional 'shutdown' command?

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.


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