Linux Processes

Linux is a multitasking operating system. Linux creates a process whenever a program is launched, either by user or by OS itself.

If a process runs and terminates properly, then everything is fine. If not, You need to manage the processes by commands.

If we would like to get a snapshot of what is currently happening on the system we may use a program called top

Type of processes:

Foreground Processes: They run on the screen and need input from the user.

Background Processes: They run in the background and usually do not need user input.

Let’s start with a process management commands.

ps To display the currently working processes

$ ps
2896 pts/0 00:00:00 bash
2911 pts/0 00:00:00 ps

Display all processes

$ ps ax
1 ?   Ss   0:04 /sbin/init
2 ?   S    0:00 [kthreadd]
3 ?   S    0:00 [migration/0]
4 ?   S    0:00 [ksoftirqd/0]
5 ?   S    0:00 [migration/0]
6 ?   S    0:00 [watchdog/0]
7 ?   S    0:00 [migration/1]
$ ps -ef

Use the “u” option or “-f” option to display detailed information about the processes

$ ps aux

$ ps -ef -f

Display process by user

$ ps -f -u ops

Show process by name

$ ps -C metacity

Show process by process id

$ ps -f -p 2609,2704,2585

Show process by using grep (searching)

$ ps -ef |grep "metacity"

Show process by sorting on cpu usage percentage

$ ps aux --sort=-pcpu,+pmem |head

Customizing the ps output format

$ ps -eo uname,pid,start_time,time,args |head

top Display all running process

$ top

kill pid terminate a process

$ kill -9 2895

killall proc Kill all the process named proc

$ killall -9 firefox

pkill pattern Will kill all processes matching the pattern

$ pkill -USR1 sample

bg List stopped or background jobs,resume a stopped job in the background

$ bg

fg Brings the most recent job to foreground

$ fg