Linux Process Management - The Easy Way
Tags: devops, linux, processmanagement, sysadmin
Published: 2024-02-20 15:36:21

A Linux process is an instance of a program in execution. When you open an application or run a command on your Linux system, a process is created to carry out that specific task. Each process operates independently with its own set of resources, including memory, CPU time, and open files.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83hxd1h8zjs30georjsb.gif)

- Each process has a process id also called as **PID**
- Every process has a parent process called as **PPID**

 _Child process is often started by the parent process_

> init process (runit,systemd,openrc,s6,dinit) have **PID** as 1, i.e it is the first process that boot your Linux system. _If you don't know about what init in your system it's systemd_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ass77pxekrjrhm9huyi9.gif)

When you want a process to die you can **kill** it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ozlea81q2ys0jccdtmx.gif)

**Process that starts at system startup and keeps on running forever are called daemon.** _The daemon never dies_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irnbc74wfm37xlfb17v3.gif)

When a process is killed but it is still showing up in the system then that process is known as **zombie**. _You can't kill zombie coz they are already dead_ **Zombie processes never occupy the resources like CPU or RAM, only an entry remains in the process but these process are already killed.**

_Syntax_
```
ps [options]
```

_options_ we will check in a while. But what if we just type 

```
ps
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkz426bvutia65ge8dwh.png)

We will can see two processes one is **current shell** and another one is the **ps command** itself that we just entered. 
<br>

- **PID** is the _process ID_ (every process has unique id)
- **TTY** is the _terminal type_ of user logged into.
- **TIME** is _amount of CPU in min and sec_ that process has been running.
- **CMD** is name of the command that _launch the process_

---

# To check the process id of your shell
_prints the PID of your shell_
```
echo $$
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j2qkjca81skivi9p86v.png)

---

_prints PID with the process name_
```
ps -C bash
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afwk6gga66e3ozexqrq9.png)

# Print the process id of the parent

```
echo $PPID
```

---

_Not only print the process id of current shell, but the process id of it's parent as well._
```
echo $$ $PPID
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghjr9pfbk6h5kn8j4562.png)

> first id is **current** and second id is **parent id**

# Check the level of the shell

```
echo $SHLVL
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1840ggifazdqmp6bdbiw.png)

_3 is the level of the shell_

> The parent will always have less number as PID, coz parent came first. (parent will start the child process, so always the PID of parent is less in number.)

# pidof command

Find the process ID of a running program

```
pidof bash
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wulpibdttpwjlnr31ub6.png)

Remember we discussed about init process that PID is 1 as it is the first process executes when we power on the machine, _in my case i am running runit as my init_ , **in your case it will be systemd**

```
pidof runit
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzbuck2f9wdpt48yt2qo.png)

---

> When a process starts another process in **two phases** First the process create a fork of itself then a identical copy, then the fork process executes and exec to replace fork process with the target child process.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf9vsqod0g1f1qvgie16.gif)

IK, lol we will see what that even mean with that following example

_In bash shell_
```
echo $$
echo $$ $PPID
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fef2rk91521w3k5ox4v.png)

_Switch to zsh or any other shell_
```
echo $$
echo $$ $PPID
exec bash
echo $$ $PPID
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me3s7rsc2gg4anwnwgtz.png)

---

# To see all the processes in system

```
ps fx
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/paordwg9xd1ng41wala8.png)

- PID : Process ID
- TTY : Terminal from where the process has started
- STAT : State and Signals (High/Low priorities,Stop/Idle conditions)
- TIME : Time 
- COMMAND : The command for which that process has started

# To check a particular process

```
ps fx | grep bash
```

# With some options

```
ps -ef
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdzn8w6k8728zbflzmy1.png)

_We will see output in different format_

# Get the process id 

_Directly grep the process_
```
pgrep bash
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xq98w1g3hk2lb54qwi9.png)

---

We can even see the processes in tree format.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3jls8znf6nzsgqkbegr.gif)

# To check the process in tree format

_Process there child along with how many processes in tree format_
```
pstree
```

Now, let see with _p option_
```
pstree -p 
```
_**option -p** will show the PID with tree format_

## To check the process with respect to user

_For which user,which process is running_

```
pstree -p -u username
```

- **-p option** means the PID
- **-u option** means the user

---

# Check the details of particular process

_sleep process will run on background_
```
sleep 200 &
```

**-p option** means PID, **-s option** means process and the **number is the PID of sleep which is running on background**
```
pstree -p -s 8379
```

```
ps -C sleep
```

_To kill the process_
```
kill 8379
```

Now, if we check the state of that process
```
ps fx | grep 8379
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkorvo5b5q7humsapyfp.png)

---

> 
- D : uninterruptible sleep (usually IO)
- R : running or runnable (on run queue)
- S : interruption sleep (warning for an event to complete
- T : stopped, either by a job control signal or because it is being traced
- W : paging (not valid since the 2.6.X.X kernal)
- X : dead (should never be seen)
- Z : defunct (**zombie**) process, terminated but not repead by it parent 
- I : idle state

_Here are the different values that the **s**, **stat** and **state** output specifies (header "**STAT**" or "**S**") will display to describe the state of process_
<br>

For BSD formats & when the state keyword is used,additional characters may display

> 
- < high priority (not nice to other user)
- N low priority (nice to other users)
- L has pages locked into memory (for real-time & custom IO)
- s is a session leader
- l is multi-threaded (using CLONE_THREAD, like NPTL thread)
+ is in the foreground process group

---

# Kill process

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjs3zj1re3objj3ijdio.gif)

```
sleep 80 &
```
```
jobs
```
```
ps -C sleep
```
```
kill 9439
```
```
jobs
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g49nfcm1mrc6zytsv564.png)

## Signals in kill command

There are many signals in kill, to list all
```
kill -l
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq6t4sb3w9bjog9jswue.png)

> By default _kill_ means **kill -15**

Let, us see the most used signals one by one

1 SIGHUP : The process should re-read it's configuration file.

```
kill -1 1
```
_This command will re-read init (runit in my case) conf. file_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khb3khzklwx7zsklangt.png)

15 SIGTERM : When we run kill command that means **kill -15**  (standard kill)

```
sleep 100 &
```
```
kill -15 10386
```
_or kill 10386_

```
ps -C sleep
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yckowa2yhbsre2fnmwgc.png)

<br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgyrk0dw1zxx2karx3v.gif)

> When we kill some process normally, few process did't got killed. So, we kill them from kernel itself.

9 SIGKILL : To kill the process from kernel (sure kill)
_The kernel will shoot down the process and as a developer you have no means to intercept a **kill -9 signal**_

```
sleep 120 &
```
```
kill -9 10572
```
```
ps -C sleep
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/019loiw7i44i1i53l67e.png)

**In TTY we can see _Killed_ , _Terminate and Killed_ are not same**

<br>

Now, it's not so much in use but in case you have to see the system calls
 
> It, may not installed by default in some distros

## strace

```
sleep 120 &
```
```
strace -p 7430
```
```
kill -9 7430
```
```
strace -p 7430
```

18 SIGCONT : To start any process
19 SIGSTOP : To stop the process (we can resume it later)

```
sleep 280 &
```
_process stopped_
```
kill -19 10744
```
_process started_
```
kill -18 10744
```
```
ps -C sleep
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mbknuwdumhdu2jjf4v2.png)

---

# Kill a process it's name (**pkill**)

```
sleep 160 &
```
```
sleep 280 &
```
```
pkill sleep
```
```
jobs
```
```
ps -C sleep
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5fd8er3ntiu2hrxfbcs.png)

_No sleep processes running all sleep process are killed_

# Kill Multiple processes (**killall**)

```
sleep 120 &
```
```
sleep 200 &
```
_In new terminal_
```
top
```
```
killall sleep top
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7myhg1uznm6b0bpjmk89.png)

---

That's all you need to manage Linux processes :)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2d7o0rl6x0mij8frngj.gif)