Attach command to execute when long-running thread returns

Let's say I've just started a command that will take a long time to execute, like hg clone the entire Pypy development set. Halfway through, I decide I'd like to run another process, like hg up, once the first one returns. Is there a way to attach a command to execute when the process returns, without stopping the command/process and then restarting it as a chained command? command/process?


Assuming you're using bash, and assuming you placed the long-running task into the background of the current shell, either by running longtask & or by starting the task then hitting CTRL + Z, then you can add an extra command to run, eg:

$ sleep 45 &
[1] 27722
$ wait 27722 && echo "Done"
[1]+  Done   sleep 45
Done
$

In the example above, the echo "Done" is only executed after completion of the wait on PID 27722.

The thing I've not found is a way to put this additional command into the background to allow you to carry on working in the same shell. There must be a way to do it though - this should hopefully give you a start.