Sunday, June 30, 2013

Controlling Linux Processes

In my last post, I discussed about viewing linux processes.
Today you will learn how to controll linux processes.

If you run command from terminal, It is attached process OR foreground process.

for exiting from current process, Press Control+C (Ctrl+C). The process will be terminated.

For example, run gedit command. it will open gedit program and this program is attached with your current terminal.
You can not do anything in this terminal without closing this program. So, for closing this program Press Ctrl+C into your terminal.
The running gedit program is closed now.

If you need to run any program in background or detached from the terminal, you can add "&" at the end of the command.
For Example: run "gedit &". it will open gedit and it is detached from the terminal. The terminal should show you it's pid number which you need to close this program.


if you have run multiple background program using "&" and if you need to see all jobs, run "jobs" command.
it will show you all jobs with it's job ID.
Now if you want to attach the terminal to the 2nd program, run fg %2
If you want to attach the terminal to the 1st program, run fg %1
After attaching the program you can simply press Ctrl+C to terminate the program.

Similary you can use bg %1 to move foreground to background.
run gedit and then Press Ctrl+Z. it will stop the program but will not terminate. then move it to background with bg command.


If you want to kill a program run kill [pid]. example kill 4935 here 4935 is the process id.
You can also use job id to kill a program. example: kill %1


To see process and it's pid use ps [ Check my previous post for details ]

To terminate all instances of a program, run "killall programName". example: killall gedit
it will terminate all gedit instances.