What are the Linux kill command

So we have been covering diffrent Linux command as previosuly we saw echo command in Linux now today we will see what are the Linux kill command and Enlist all the Linux kill commands with its function

The Linux Kill commands are:

  1. Killall
  2. Pkill
  3. xkill
  • Killall: Killall command is used to kill all the processes with a particular name.
  • Pkill: This command is a lot like killall, except it kills processes with partial names.
  • Xkill: xkill allows users to kill command by clicking on the window

Difference between kill and kill -9

The normal kill command sends a signal to a process. This can terminate a processes. However if some program is hung or not responding you may have to kill that program using -9 option which will kill a program no matter what.
kill -9 you may have to think twice while using kill -9 as it will bypass any normal shutdown process and any unsaved data will be lost.

Killall Command in Linux

Unlike kill command killall command can terminate all processes by it’s name. For example, if you want to kill all processes used by firefox so instead of finding all process ID and killing them you can just do killall firefox

  • -e. Find an exact match for the process name.
  • -I. Ignore case when trying to find the process name.
  • -i. Ask for additional confirmation when killing the process.
  • -u. Only kill processes owned by a specific user.
  • -v. Report back on whether the process has been successfully killed.

pkill Command

You term pkill command as search and kill. It will kill processes based on name but when other paramerters match.

pkill options pattern

pkill options include:

  • -n. Only kill the newest of the processes that are discovered.
  • -o. Only kill the oldest of the processes that are discovered.
  • -u. Only kill the processes that are owned by the selected user.
  • -x. Only kill the processes that match the pattern exactly.
  • -signal. Send a specific signal to the process, rather than SIGTERM.

Seq Command in Linux

Seq Command in Linux: Linux seq command generates a list of numbers – you can use this in scripts and many other functions. Let’s see the examples.

Basic usage of command
Just typing seq 5 will create 5 numbers

[root@server ~]# seq 10
[root@server ~]#

If you type two numbers, first number will be start and other one will be end number.

[root@server ~]# seq 21 25
[root@server ~]#

You can add a third number in between which will work as a step.
for example, if you type seq 5 5 50 it will generate a number from 5 to 50 at the interval of 5 – see the example below

[root@server ~]# seq 5 5 50
[root@server ~]#

You can generate numbers in descending order as well by adding the middle number in -ve

[root@server ~]# seq 10 -1 1

Practical Usage of seq command
First, let’s see how to create files using the seq command. You will need to use format strings functions i.e. -f

[root@server seq]# touch $(seq -f "file-%g.txt" 1 10)

See the output of the command above.

You can use seq command in your scripting for example While Loops or For Loops.
We will see an example for loop.

[root@server seq]# for i in $(seq 1 10); do echo "Hello $i"; done
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Here is Man page you can use it to learn something more about it which may be covered in this page.

PS Command Examples

Below are PS Command Examples I will try to add.

Command to the process using highest memory

You can use the below PS command with the sort option which shows the process using highest %MEM usage at the bottom.

 [root@server ~]# ps aux --sort pmem
apache   29798  1.5  6.2 570424 31116 ?        S    05:11   3:32 /usr/sbin/httpd -DFOREGROUND
apache   29831  1.5  6.4 567676 32136 ?        S    05:12   3:29 /usr/sbin/httpd -DFOREGROUND
apache   18426  0.3  6.4 562656 32404 ?        S    08:57   0:01 /usr/sbin/httpd -DFOREGROUND
apache   18364  0.5  6.5 565692 32860 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29809  1.5  6.8 567452 34336 ?        S    05:11   3:31 /usr/sbin/httpd -DFOREGROUND
apache   18356  0.4  6.9 567512 34572 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29792  1.5  7.2 569516 36144 ?        S    05:11   3:33 /usr/sbin/httpd -DFOREGROUND
apache   29825  1.4  7.4 569532 37372 ?        S    05:11   3:27 /usr/sbin/httpd -DFOREGROUND
apache   18014  0.3  8.0 570868 40232 ?        S    08:51   0:02 /usr/sbin/httpd -DFOREGROUND
apache   29791  1.5  8.2 572836 41040 ?        S    05:11   3:36 /usr/sbin/httpd -DFOREGROUND

From the example above we can see that the PID 29791 is using the highest memory. You can note the PID of the process which is using the high memory and then you can see the details using ps.

[root@server ~]# ps ev --pid=18426
18426 ?        S      0:03   3847   483 570652 41760  8.3 /usr/sbin/httpd -DFOREGROUND LANG=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin NOTIFY_SOCKET=/run/systemd/notify
[root@server ~]#

Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.

Sed Command Examples

Insert a newline in front of pattern

Example your text in the file is as below

Updated number of BobUpdated number of ChrisUpdated number of Peter

If you want format the text as below
Updated number of Bob
Updated number of Chris
Updated number of Peter

Need to use the command as below.

 [root@server ~]# sed 's/Update/\n&/g'  test.txt

Sum of the Values of Column Using AWK.

For example, I have a file called apples.txt with the following content and I want to total them up.

Apples in Box 1 = 100
Apples in Box 2 = 564
Apples in Box 3 = 214
Apples in Box 4 = 124
Apples in Box 5 = 785
Apples in Box 6 = 398
Apples in Box 7 = 125
Apples in Box 8 = 753
Apples in Box 9 = 357
Apples in Box 10 = 951

So just cat the file and use AWK command awk ‘{SUM+=$6}END{print SUM}’ to total them up

make sure to use the right column in awk command, like in our example I want to SUM the column 6 so I used ‘{SUM+=$6}

[root@server ~]# cat  example.txt | awk '{SUM+=$6}END{print SUM}'