Saving a process running in the foreground

Did you start a script running in a bash shell and then need to pack your laptop up in a hurry? Did you commit the dual crimes of forgetting to background your task and to nohup? Tsk, you are a bad man. Like you, I had previously consigned such tasks to the dustbin, resigned to the fact they were unsalvagable.

But wait, bash comes to the rescue. You can retroactively nohup your tasks even if they are running in the foreground! Who knew?

1) Get your shell back with CTRL-Z. This puts the process into pause mode.

2) Find the job ID using jobs –l and reactivate the process with bg <job number>

3) Finally, use the bash function disown to prevent SIGHUP signals taking down your process when your shell quits, i.e. disown –h <PID>

Voila! You can now undock your laptop and meet your hot date without losing your work progress!

Leave a Reply

Your email address will not be published.