Q:

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

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?

A:

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.

A:

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.

A:

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

