Sunday, January 9, 2011

Stop or suspend a running process in Linux

Introduction

If you start a process on a terminal, being it local or a remote (ssh) terminal, you normally loose the control of the terminal while the process is running, then if you want to recover that control, you have some options.

Cancel the process

By pressing CTRL+C you can stop a running process, and recover the control of your terminal, this will tell to the running application to stop anything it is doing at that moment and shut down.

Suspend a process

Another way, is to suspend the process for a moment, you will use CTRL+Z for this, then you can resume the job in any moment, when you feel like to.

Restore your stopped processes

You have now two different ways to re-start your stopped processes, you can send them to the foreground or to the background, to send it to foreground execute:
 
fg n

To send them to background execute:
 
bg n

Where n is the number of the stopped process, and you will see it in brackets when stop it.
Like this:
 
[admin@arch ~]$ amsn                                                                                                                         
^Z                                                                                                                                             
[1]+  Stopped                 amsn

You can see that the stopped process is amsn and its identifying number is “1”. Now, if you ever forget the number, you can run:
jobs
you will get an output like this one:
 
[admin@arch ~]$ jobs
[1]+  Stopped                 amsn

No comments: