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
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
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.

Disable host-key checking while using ansible adhoc command

If you want to disable host key checking for ansible adhoc command, first run the the command below.

 [root@server ~]# export ANSIBLE_HOST_KEY_CHECKING=False

Now, once you have disabled host key checking run your ansible ad-hoc command.

 [root@server ~]#  ansible all -i inventory -m shell -a 'uptime'

Run Ansible adhoc command with Sudo

[root@server ~]# ansible all -i inventory.yml -m shell -a 'service crond restart' -K --become

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}'
4371

Eval Command in Linux

Eval is built in unix command – it’s used to execute arguments as shell commands. It’s useful when you have command stored in variable and you want to execute it.

[root@server Files]# mycommand="ls -ltr"

[root@server Files]# echo $mycommand
ls -ltr

[root@server Files]# eval $mycommand
total 0
-rw-r--r-- 1 root root 0 Nov  5 07:40 file1.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file3.txt
-rw-r--r-- 1 root root 0 Nov  5 07:40 file2.txt
[root@server Files]#

Look at the example above, I have stored the command ls – ltr in the variable mycommand so when I do echo it’s just prints what in the variable. But when I do eval followed with the variable – it will actually execute the command.

Eval is powerful command but it’s Evil – it has security issues as well.
You can find the details here