Thursday, August 9, 2012

How to Start a stopped process in Linux

While running a process in Linux, Sometimes we press Ctrl+Z unintentionally (or by choice). This makes the program STOP.

For example while running a GDB Session, I accidentally pressed Ctrl + Z, which stopped the gdb and the process came out of the gdb session, giving the Stopped message :


I wanted to continue (not restart) the session, for restarting, i couldve killed the process checking the pid and killing the process by its pid, but that would require me to start debugging my program all over again, which is a pain.

So, with a little help from google, i discovered that when we stop a process (using Ctrl +Z ), it basically goes into the background, and goes into sleep mode.

This can be seen from bg command

bash$ bg



Now to continue the program from its sleep mode, we need to bring it to the foreground.this can be done using fg command.

bash $fg <JOB_ID>


where JOB_ID is obtained using the command jobs , which matches process name to job#.

bash $jobs


Thats all! fg will bring your stopped command back to life!