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.
- Start the process.
- Realize that I forgot to do my
&&
command. - Hit Ctrl+Z to suspend the process and return to the shell.
- Run
fg && shutdown -h now
to:- Foreground and resume the process you just suspended.
- 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