|

Creating Monitoring and Killing Processes in Linux

Creating Monitoring and Killing Processes in Linux

Creating, Monitoring, and Killing Processes in Linux are a key part of working with the operating system. In Linux there are several concepts regarding processes to be aware of. In this episode we’ll take a look at some of the more important ones. First up, we’ll look at background and foreground processes, and how they are similar and different. Next, we’ll tackle the concept of persistent processes. These are the ones that are always running, yet out of sight. We’ll also see how to interact with processes and sort running processes as well. Once you start to understand the concept of processes that are continually running on the system, you’ll have a better idea of what Linux is doing for you, and how to better interact with it.


Background and Foreground Linux Processes

We’ll use an example of the sleep command to see how these differ. It will help for you to follow along on your own Linux machine to put these concepts to work as well. First we’ll trigger the command and notice that it is running in the foreground by default, and it has taken over our control of the terminal. The terminal does not respond to additional input.

vagrant@homestead:~$ sleep 500

So now we’re stuck with a blinking cursor, and not much else. Well that’s not too helpful, let’s fix that by typing CTRL + C to kill the current process, and we’ll get control of the terminal back. So what if you need to have this process run, and still have control? Enter the background.

Background Process

To place a process in the background, it is very easy. All you need to do is add the ampersand & character at the end of the command to put the job in the background. Let’s see how.

vagrant@homestead:~$ sleep 500 &
[1] 4994
vagrant@homestead:~$

Now here we can see that the process was started, but we get the terminal back to continue doing our work without issue. The feedback we are given is helpful information. It states that this is Job number 1 with a process ID of 4994. To confirm this, we can simply run the jobs command like so.

vagrant@homestead:~$ jobs
[1]+ Running sleep 500 &
vagrant@homestead:~$

Adding Jobs To The Background

We are not limited by how many jobs we can place in the background. Let’s add an additional job and see the result with the jobs command.

vagrant@homestead:~$ sleep 712 &
[2] 5001
vagrant@homestead:~$ jobs
[1]- Running sleep 500 &
[2]+ Running sleep 712 &
vagrant@homestead:~$

Pretty Slick! We can see now that there are two jobs running. The second job has a job number of 2, and a process ID of 5001.

Bring A Process To The Foreground In Linux

If you have a process running in the background, how can you interact with it? Well, you can’t, until you bring it to the foreground. To do so, you simply run the fg command with the job number as an argument. There are a few things to note here. When we bring a process to the foreground, we do have the ability to work with it, kill it, or suspend it. Note that if you have access to the job in the foreground, CTRL + C will kill it. CTRL + Z will suspend it. There is a difference! We’ll note the difference in this example here.

vagrant@homestead:~$ sleep 500 &
[1] 5158
vagrant@homestead:~$ sleep 712 &
[2] 5159
vagrant@homestead:~$ jobs
[1]- Running sleep 500 &
[2]+ Running sleep 712 &
vagrant@homestead:~$ fg 1
sleep 500

[1]+ Stopped sleep 500
vagrant@homestead:~$ jobs
[1]+ Stopped sleep 500
[2]- Running sleep 712 &
vagrant@homestead:~$ bg 1
[1]+ sleep 500 &
vagrant@homestead:~$ jobs
[1]- Running sleep 500 &
[2]+ Running sleep 712 &
vagrant@homestead:~$

In this example, we actually start each process anew, since they actually finished by the time we reached this point. This is why we see that each job, 1 and 2, have new process ids. Now, we can see that the fg command brought our job number 1 into the foreground. That’s great, but there is a problem. It has now taken over control of the terminal as well. So how can we preserve the job, and do other work as well? We do this by suspending that particular job with CTRL + Z. When we enter that command, we can see that the job still exists, however it is stopped running as confirmed by the jobs command. To set that job back in motion, we simply use the bg command, and the jobs command once again confirms that it is running again. Slick.


How Jobs Are Affected By New Sessions

What happens if the jobs are running in the background, and the current terminal session ends for some reason? When you open a new session, you will no longer have access to those jobs the way we just did. They are no longer available in the terminal. Let’s see how to interact with the processes now.

First, we run the jobs command to confirm that the prior jobs we set up are in fact no longer part of the new terminal session we have, so we can’t access them like before.

vagrant@homestead:~$ jobs
vagrant@homestead:~$

Next, we should list all of the processes that are running. We can use the ps command to do this. We’ll add three flags to this as well. The a flag is to list all processes that are accessible to us, the u flag shows us who is running the process, and the x flag makes sure to include all currently running processes, even if they are not part of the current terminal session.

vagrant@homestead:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 33640 3016 ? Ss 09:38 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 09:38 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 09:38 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/0:0]
root 5 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S 09:38 0:07 [rcu_sched] root 8 0.1 0.0 0 0 ? S 09:38 0:35 [rcuos/0] root 9 0.0 0.0 0 0 ? S 09:38 0:00 [rcu_bh] root 10 0.0 0.0 0 0 ? S 09:38 0:00 [rcuob/0] root 11 0.0 0.0 0 0 ? S 09:38 0:00 [migration/0] root 12 0.0 0.0 0 0 ? S 09:38 0:00 [watchdog/0] root 13 0.0 0.0 0 0 ? S< 09:38 0:00 [khelper] root 14 0.0 0.0 0 0 ? S 09:38 0:00 [kdevtmpfs] root 15 0.0 0.0 0 0 ? S< 09:38 0:00 [netns] root 16 0.0 0.0 0 0 ? S< 09:38 0:00 [writeback] root 17 0.0 0.0 0 0 ? S< 09:38 0:00 [kintegrityd] root 18 0.0 0.0 0 0 ? S< 09:38 0:00 [bioset] root 19 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/u3:0] root 20 0.0 0.0 0 0 ? S< 09:38 0:00 [kblockd] root 21 0.0 0.0 0 0 ? S< 09:38 0:00 [ata_sff] root 22 0.0 0.0 0 0 ? S 09:38 0:00 [khubd] root 23 0.0 0.0 0 0 ? S< 09:38 0:00 [md] root 24 0.0 0.0 0 0 ? S< 09:38 0:00 [devfreq_wq] root 25 0.0 0.0 0 0 ? S 09:38 0:19 [kworker/0:1] root 26 0.0 0.0 0 0 ? S 09:38 0:00 [khungtaskd] root 27 0.0 0.0 0 0 ? S 09:38 0:00 [kswapd0] root 28 0.0 0.0 0 0 ? SN 09:38 0:00 [ksmd] root 29 0.0 0.0 0 0 ? SN 09:38 0:00 [khugepaged] root 30 0.0 0.0 0 0 ? S 09:38 0:00 [fsnotify_mark] root 31 0.0 0.0 0 0 ? S 09:38 0:00 [ecryptfs-kthrea] root 32 0.0 0.0 0 0 ? S< 09:38 0:00 [crypto] root 44 0.0 0.0 0 0 ? S< 09:38 0:00 [kthrotld] root 64 0.0 0.0 0 0 ? S< 09:38 0:00 [deferwq] root 65 0.0 0.0 0 0 ? S< 09:38 0:00 [charger_manager] root 106 0.0 0.0 0 0 ? S< 09:38 0:00 [kpsmoused] root 107 0.0 0.0 0 0 ? S 09:38 0:00 [scsi_eh_0] root 108 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/u2:2] root 109 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/u2:3] root 154 0.0 0.0 0 0 ? S 09:38 0:00 [jbd2/sda1-8] root 155 0.0 0.0 0 0 ? S< 09:38 0:00 [ext4-rsv-conver] root 395 0.0 0.0 19476 652 ? S 09:38 0:00 upstart-udev-bridge --daemon root 397 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/u3:1] root 401 0.0 0.0 49800 1724 ? Ss 09:38 0:00 /lib/systemd/systemd-udevd --d root 430 0.0 0.0 0 0 ? S< 09:38 0:00 [iprt] root 553 0.0 0.1 10224 2412 ? Ss 09:38 0:00 dhclient -1 -v -pf /run/dhclie root 662 0.0 0.0 23420 1092 ? Ss 09:38 0:00 rpcbind statd 711 0.0 0.0 21544 1376 ? Ss 09:38 0:00 rpc.statd -L root 715 0.0 0.0 15392 660 ? S 09:38 0:00 upstart-socket-bridge --daemon root 785 0.0 0.0 0 0 ? S< 09:38 0:00 [rpciod] message+ 787 0.0 0.0 39216 1224 ? Ss 09:38 0:02 dbus-daemon --system --fork root 796 0.0 0.0 0 0 ? S< 09:38 0:00 [nfsiod] root 827 0.0 0.0 23480 416 ? Ss 09:38 0:00 rpc.idmapd root 864 0.0 0.0 43452 1788 ? Ss 09:38 0:00 /lib/systemd/systemd-logind syslog 884 0.0 0.1 255844 3180 ? Ssl 09:38 0:00 rsyslogd root 923 0.0 0.0 15404 640 ? S 09:38 0:00 upstart-file-bridge --daemon root 978 0.0 0.0 14540 956 tty4 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty4 top - 17:01:24 up 7:22, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 93 total, 2 running, 91 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.3 us, 1.0 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st KiB Mem: 2050072 total, 996312 used, 1053760 free, 22280 buffers KiB Swap: 0 total, 0 used, 0 free. 291460 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1090 mysql 20 0 1135068 460192 8772 S 0.3 22.4 0:34.47 mysqld 5223 vagrant 20 0 107692 2164 1172 S 0.3 0.1 0:00.13 sshd 1 root 20 0 33640 3016 1492 S 0.0 0.1 0:01.08 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:00.26 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0 top - 17:02:11 up 7:23, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 93 total, 2 running, 91 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.2 us, 0.8 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.2 hi, 0.0 si, 0.0 st KiB Mem: 2050072 total, 996808 used, 1053264 free, 22280 buffers KiB Swap: 0 total, 0 used, 0 free. 291460 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1052 redis 20 0 35188 7404 968 S 0.3 0.4 0:59.06 redis-server 8 root 20 0 0 0 0 R 0.2 0.0 0:37.51 rcuos/0 1370 root 20 0 216612 1060 728 S 0.2 0.1 0:12.28 VBoxService 5223 vagrant 20 0 107692 2340 1344 S 0.2 0.1 0:00.27 sshd 25 root 20 0 0 0 0 S 0.1 0.0 0:20.19 kworker/0:1 2310 vagrant 20 0 90764 1956 484 S 0.1 0.1 0:05.58 nginx vagrant@homestead:~$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 33640 3016 ? Ss 09:38 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S 09:38 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 09:38 0:00 [ksoftirqd/0] root 4 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/0:0] root 5 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S 09:38 0:08 [rcu_sched] root 8 0.1 0.0 0 0 ? S 09:38 0:38 [rcuos/0] root 9 0.0 0.0 0 0 ? S 09:38 0:00 [rcu_bh] root 10 0.0 0.0 0 0 ? S 09:38 0:00 [rcuob/0] root 11 0.0 0.0 0 0 ? S 09:38 0:00 [migration/0] root 12 0.0 0.0 0 0 ? S 09:38 0:00 [watchdog/0] root 13 0.0 0.0 0 0 ? S< 09:38 0:00 [khelper] root 14 0.0 0.0 0 0 ? S 09:38 0:00 [kdevtmpfs] root 15 0.0 0.0 0 0 ? S< 09:38 0:00 [netns] root 16 0.0 0.0 0 0 ? S< 09:38 0:00 [writeback] root 17 0.0 0.0 0 0 ? S< 09:38 0:00 [kintegrityd] root 18 0.0 0.0 0 0 ? S< 09:38 0:00 [bioset] root 19 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/u3:0] root 20 0.0 0.0 0 0 ? S< 09:38 0:00 [kblockd] root 21 0.0 0.0 0 0 ? S< 09:38 0:00 [ata_sff] root 22 0.0 0.0 0 0 ? S 09:38 0:00 [khubd] root 23 0.0 0.0 0 0 ? S< 09:38 0:00 [md] root 24 0.0 0.0 0 0 ? S< 09:38 0:00 [devfreq_wq] root 25 0.0 0.0 0 0 ? S 09:38 0:20 [kworker/0:1] root 26 0.0 0.0 0 0 ? S 09:38 0:00 [khungtaskd] root 27 0.0 0.0 0 0 ? S 09:38 0:00 [kswapd0] root 28 0.0 0.0 0 0 ? SN 09:38 0:00 [ksmd] root 29 0.0 0.0 0 0 ? SN 09:38 0:00 [khugepaged] root 30 0.0 0.0 0 0 ? S 09:38 0:00 [fsnotify_mark] root 31 0.0 0.0 0 0 ? S 09:38 0:00 [ecryptfs-kthrea] root 32 0.0 0.0 0 0 ? S< 09:38 0:00 [crypto] root 44 0.0 0.0 0 0 ? S< 09:38 0:00 [kthrotld] root 64 0.0 0.0 0 0 ? S< 09:38 0:00 [deferwq] root 65 0.0 0.0 0 0 ? S< 09:38 0:00 [charger_manager] root 106 0.0 0.0 0 0 ? S< 09:38 0:00 [kpsmoused] root 107 0.0 0.0 0 0 ? S 09:38 0:00 [scsi_eh_0] root 108 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/u2:2] root 109 0.0 0.0 0 0 ? S 09:38 0:00 [kworker/u2:3] root 154 0.0 0.0 0 0 ? S 09:38 0:00 [jbd2/sda1-8] root 155 0.0 0.0 0 0 ? S< 09:38 0:00 [ext4-rsv-conver] root 395 0.0 0.0 19476 652 ? S 09:38 0:00 upstart-udev-bridge --daemon root 397 0.0 0.0 0 0 ? S< 09:38 0:00 [kworker/u3:1] root 401 0.0 0.0 49800 1724 ? Ss 09:38 0:00 /lib/systemd/systemd-udevd --daemo root 430 0.0 0.0 0 0 ? S< 09:38 0:00 [iprt] root 553 0.0 0.1 10224 2412 ? Ss 09:38 0:00 dhclient -1 -v -pf /run/dhclient.e root 662 0.0 0.0 23420 1092 ? Ss 09:38 0:00 rpcbind statd 711 0.0 0.0 21544 1376 ? Ss 09:38 0:00 rpc.statd -L root 715 0.0 0.0 15392 660 ? S 09:38 0:00 upstart-socket-bridge --daemon root 785 0.0 0.0 0 0 ? S< 09:38 0:00 [rpciod] message+ 787 0.0 0.0 39216 1224 ? Ss 09:38 0:02 dbus-daemon --system --fork root 796 0.0 0.0 0 0 ? S< 09:38 0:00 [nfsiod] root 827 0.0 0.0 23480 416 ? Ss 09:38 0:00 rpc.idmapd root 864 0.0 0.0 43452 1788 ? Ss 09:38 0:00 /lib/systemd/systemd-logind syslog 884 0.0 0.1 255844 3180 ? Ssl 09:38 0:00 rsyslogd root 923 0.0 0.0 15404 640 ? S 09:38 0:00 upstart-file-bridge --daemon root 978 0.0 0.0 14540 956 tty4 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty4 root 981 0.0 0.0 14540 956 tty5 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty5 root 990 0.0 0.0 14540 948 tty2 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty2 root 991 0.0 0.0 14540 948 tty3 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty3 root 994 0.0 0.0 14540 944 tty6 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty6 root 1041 0.0 0.0 4368 656 ? Ss 09:38 0:00 acpid -c /etc/acpi/events -s /var/ root 1042 0.0 0.0 23656 1052 ? Ss 09:38 0:00 cron daemon 1043 0.0 0.0 19140 164 ? Ss 09:38 0:00 atd redis 1052 0.2 0.3 35188 7404 ? Ssl 09:38 1:00 /usr/bin/redis-server *:6379 mysql 1090 0.1 22.4 1135068 460192 ? Ssl 09:38 0:35 /usr/sbin/mysqld postgres 1111 0.0 0.8 249312 16788 ? S 09:38 0:00 /usr/lib/postgresql/9.4/bin/postgr postgres 1115 0.0 0.1 249420 3144 ? Ss 09:38 0:00 postgres: checkpointer process postgres 1116 0.0 0.1 249312 2688 ? Ss 09:38 0:00 postgres: writer process postgres 1117 0.0 0.0 249312 1668 ? Ss 09:38 0:00 postgres: wal writer process postgres 1118 0.0 0.1 249716 2744 ? Ss 09:38 0:00 postgres: autovacuum launcher proc postgres 1119 0.0 0.0 104588 1844 ? Ss 09:38 0:00 postgres: stats collector process beansta+ 1260 0.0 0.0 4472 620 ? S 09:38 0:00 /usr/bin/beanstalkd -l 127.0.0.1 - root 1290 0.0 0.0 0 0 ? S 09:38 0:00 [kauditd] vagrant 1309 0.0 8.0 668172 165992 ? Ssl 09:38 0:00 /usr/bin/hhvm --config /etc/hhvm/p memcache 1316 0.0 0.0 325396 1184 ? Sl 09:38 0:02 /usr/bin/memcached -m 64 -p 11211 root 1350 0.0 0.5 60540 11284 ? Ss 09:38 0:08 /usr/bin/python /usr/bin/superviso root 1370 0.0 0.0 216612 1060 ? Sl 09:38 0:12 /usr/sbin/VBoxService root 1526 0.0 1.6 182348 34492 ? Ssl 09:38 0:01 /usr/bin/ruby /usr/bin/puppet agen root 1599 0.0 1.6 112692 34532 ? Sl 09:38 0:00 ruby /usr/bin/chef-client -d -P /v root 1637 0.0 0.0 14540 960 tty1 Ss+ 09:38 0:00 /sbin/getty -8 38400 tty1 root 1865 0.0 0.1 61364 3060 ? Ss 09:38 0:00 /usr/sbin/sshd -D root 2309 0.0 0.0 90408 1528 ? Ss 09:38 0:00 nginx: master process /usr/sbin/ng vagrant 2310 0.0 0.0 90764 1956 ? S 09:38 0:05 nginx: worker process vagrant 2311 0.0 0.0 90764 1956 ? S 09:38 0:04 nginx: worker process vagrant 2312 0.0 0.0 90764 1956 ? S 09:38 0:00 nginx: worker process vagrant 2313 0.0 0.0 90764 1956 ? S 09:38 0:04 nginx: worker process root 2424 0.0 0.9 345040 18740 ? Ss 09:38 0:02 php-fpm: master process (/etc/php5 vagrant 2426 0.0 0.3 345040 6248 ? S 09:38 0:00 php-fpm: pool www vagrant 2427 0.0 0.3 345040 6248 ? S 09:38 0:00 php-fpm: pool www vagrant 5433 0.0 0.0 5916 616 ? S 17:13 0:00 sleep 500 vagrant 5434 0.0 0.0 5916 620 ? S 17:13 0:00 sleep 712 root 5436 0.1 0.2 107692 4260 ? Ss 17:13 0:00 sshd: vagrant [priv] vagrant 5490 0.5 0.1 107692 2172 ? R 17:13 0:00 sshd: vagrant@pts/2 vagrant 5491 1.0 0.1 21612 4048 pts/2 Ss 17:13 0:00 -bash vagrant 5508 0.0 0.0 17168 1292 pts/2 R+ 17:13 0:00 ps aux vagrant@homestead:~$

Wow! We can see that there are a ton of processes at work here! In our work so far with Linux, we never knew just how much the operating system was doing in the background for us. Now we have an idea of how this all works. In fact, we can use our knowledge of linux redirection and piping to find exactly what we are looking for. We can run our ps command and pipe the output to the grep command with sleep as an argument. Let’s see how to do this.

vagrant@homestead:~$ ps aux | grep sleep
vagrant 5433 0.0 0.0 5916 616 ? S 17:13 0:00 sleep 500
vagrant 5434 0.0 0.0 5916 620 ? S 17:13 0:00 sleep 712
vagrant 5512 0.0 0.0 10464 916 pts/2 S+ 17:14 0:00 grep –color=auto sleep

Nice! Look at how much easier that is to work with. If we know what we are looking for, we can search for it using grep, and we find exactly what we were looking for. The third job you see here is simply the grep command itself. Don’t worry about that, it is the first two that we are interested in. Now that we have found our processes, we can easily cancel these processes with the kill command. Let’s try that now.

vagrant@homestead:~$ kill 5433
vagrant@homestead:~$ kill 5434
vagrant@homestead:~$ ps aux | grep sleep
vagrant 5514 0.0 0.0 10464 912 pts/2 S+ 17:15 0:00 grep –color=auto sleep
vagrant@homestead:~$

Monitoring Processes With top

The top command is a really cool way to get a list of all the currently running processes on your Linux machine, much like the way you can use task manager in a Windows operating system. It provides a lot of information and can be sorted by cpu use or memory use. All you need to do is type top at the command line. By default, it sorts the processes by the most cpu intensive. Let’s test it out.

vagrant@homestead:~$ top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

6374 vagrant 20 0 107692 2168 1176 S 0.3 0.1 0:00.39 sshd
1052 redis 20 0 35188 7404 968 S 0.7 0.4 1:14.62 redis-server
6374 vagrant 20 0 107692 2168 1176 S 0.3 0.1 0:00.41 sshd
6419 vagrant 20 0 23568 1576 1100 R 0.3 0.1 0:00.31 top
1 root 20 0 33640 3016 1492 S 0.0 0.1 0:01.08 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.33 ksoftirqd/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root 20 0 0 0 0 S 0.0 0.0 0:10.00 rcu_sched
8 root 20 0 0 0 0 R 0.0 0.0 0:47.15 rcuos/0
9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
11 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.65 watchdog/0
13 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper
14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.02 kworker/u3:0
20 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ata_sff
22 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khubd
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 devfreq_wq
25 root 20 0 0 0 0 S 0.0 0.0 0:25.50 kworker/0:1
26 root 20 0 0 0 0 S 0.0 0.0 0:00.02 khungtaskd
27 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kswapd0
28 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
29 root 39 19 0 0 0 S 0.0 0.0 0:00.32 khugepaged
30 root 20 0 0 0 0 S 0.0 0.0 0:00.00 fsnotify_mark
31 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ecryptfs-kthrea
32 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
44 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld
64 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 deferwq
65 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 charger_manager
106 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kpsmoused
107 root 20 0 0 0 0 S 0.0 0.0 0:00.30 scsi_eh_0
108 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u2:2
109 root 20 0 0 0 0 S 0.0 0.0 0:00.10 kworker/u2:3
154 root 20 0 0 0 0 S 0.0 0.0 0:00.04 jbd2/sda1-8
155 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 ext4-rsv-conver
395 root 20 0 19476 652 464 S 0.0 0.0 0:00.13 upstart-udev-br
397 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/u3:1
401 root 20 0 49800 1724 984 S 0.0 0.1 0:00.05 systemd-udevd
430 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 iprt
553 root 20 0 10224 2412 116 S 0.0 0.1 0:00.00 dhclient
662 root 20 0 23420 1092 800 S 0.0 0.1 0:00.10 rpcbind

As we said, by default, these processes are sorted by the most cpu intensive. It’s super easy to sort in other ways as well. Simply type the greater than symbol > to resort the processes by memory usage. When we do this, we can see that the mysql process is the most memory intensive of all the processes running.


PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1309 vagrant 20 0 668172 165992 101896 S 0.0 8.1 0:00.74 hhvm
1090 mysql 20 0 1135068 460192 8772 S 0.3 22.4 0:44.23 mysqld
1526 root 20 0 182348 34492 2780 S 0.0 1.7 0:01.19 puppet
2424 root 20 0 345040 18740 13244 S 0.0 0.9 0:02.95 php5-fpm
1111 postgres 20 0 249312 16788 15412 S 0.0 0.8 0:00.49 postgres
1350 root 20 0 60540 11284 1032 S 0.0 0.6 0:10.37 supervisord
1052 redis 20 0 35188 7404 968 S 0.0 0.4 1:15.08 redis-server
2426 vagrant 20 0 345040 6248 752 S 0.0 0.3 0:00.00 php5-fpm
1052 redis 20 0 35188 7404 968 S 0.3 0.4 1:15.10 redis-server
6321 root 20 0 107692 4256 3268 S 0.0 0.2 0:00.00 sshd
6375 vagrant 20 0 21612 4048 1756 S 0.0 0.2 0:00.11 bash
884 syslog 20 0 255844 3180 784 S 0.0 0.2 0:00.05 rsyslogd
1115 postgres 20 0 249420 3144 1708 S 0.0 0.2 0:00.01 postgres
1865 root 20 0 61364 3068 2396 S 0.0 0.1 0:00.00 sshd
1 root 20 0 33640 3016 1492 S 0.0 0.1 0:01.08 init
1118 postgres 20 0 249716 2744 1072 S 0.0 0.1 0:00.45 postgres
1116 postgres 20 0 249312 2688 1260 S 0.0 0.1 0:00.87 postgres
553 root 20 0 10224 2412 116 S 0.0 0.1 0:00.00 dhclient
6374 vagrant 20 0 107692 2168 1176 S 0.0 0.1 0:00.77 sshd
2310 vagrant 20 0 90764 1956 484 S 0.0 0.1 0:06.76 nginx
6374 vagrant 20 0 107692 2168 1176 S 0.0 0.1 0:00.78 sshd
2312 vagrant 20 0 90764 1956 484 S 0.0 0.1 0:00.00 nginx
2313 vagrant 20 0 90764 1956 484 S 0.0 0.1 0:05.73 nginx
1119 postgres 20 0 104588 1844 388 S 0.0 0.1 0:00.38 postgres
864 root 20 0 43452 1788 1436 S 0.0 0.1 0:00.01 systemd-logind
401 root 20 0 49800 1724 984 S 0.0 0.1 0:00.05 systemd-udevd
1117 postgres 20 0 249312 1668 292 S 0.0 0.1 0:00.66 postgres
6419 vagrant 20 0 23568 1576 1100 R 0.3 0.1 0:00.85 top
2309 root 20 0 90408 1528 232 S 0.0 0.1 0:00.00 nginx
6419 vagrant 20 0 23568 1576 1100 R 0.3 0.1 0:00.87 top
787 message+ 20 0 39216 1224 856 S 0.0 0.1 0:02.81 dbus-daemon
1316 memcache 20 0 325396 1184 844 S 0.0 0.1 0:02.67 memcached
662 root 20 0 23420 1092 800 S 0.0 0.1 0:00.10 rpcbind
1370 root 20 0 216612 1060 728 S 0.3 0.1 0:15.58 VBoxService
1042 root 20 0 23656 1052 800 S 0.0 0.1 0:00.11 cron
1370 root 20 0 216612 1060 728 S 0.0 0.1 0:15.58 VBoxService
978 root 20 0 14540 956 796 S 0.0 0.0 0:00.00 getty
981 root 20 0 14540 956 796 S 0.0 0.0 0:00.00 getty
990 root 20 0 14540 948 796 S 0.0 0.0 0:00.00 getty
991 root 20 0 14540 948 796 S 0.0 0.0 0:00.00 getty
994 root 20 0 14540 944 796 S 0.0 0.0 0:00.00 getty
715 root 20 0 15392 660 312 S 0.0 0.0 0:00.06 upstart-socket-
1041 root 20 0 4368 656 516 S 0.0 0.0 0:00.00 acpid
395 root 20 0 19476 652 464 S 0.0 0.0 0:00.13 upstart-udev-br
923 root 20 0 15404 640 356 S 0.0 0.0 0:00.03 upstart-file-br
1260 beansta+ 20 0 4472 620 524 S 0.0 0.0 0:00.00 beanstalkd
827 root 20 0 23480 416 208 S 0.0 0.0 0:00.00 rpc.idmapd
1043 daemon 20 0 19140 164 0 S 0.0 0.0 0:00.00 atd

Creating Monitoring and Killing Processes in Linux Summary

In this episode we took a look at how running processes is the bread and butter of the Linux operating system. We have processes that run in the foreground, as well as the background. We had a look at how to interact with these processes, as well as monitoring processes with the top command.