Last pid process linux - linux

I'm trying to get the last pid of a process (for example sleep). I do this:
# sleep 10 &
[1] 14115
But when I use this command to get the last pid:
# ps aux --sort +start_time | tail -n 4 | awk 'NR==1{print $2 " " $11}'
14125 sleep
The pid is not the same, i don't understand what I'm doing wrong, because if I do the same but without the last part of the command, I get the same pid:
# sleep 10 &
[1] 15853
# ps aux --sort +start_time | tail -n 4
root 15853 0.0 0.1 3364 520 pts/2 S 16:45 0:00 sleep 10
www-data 15871 0.0 0.0 1864 452 ? S 16:45 0:00 sleep 1
root 15872 0.0 0.2 4344 1168 pts/2 R+ 16:45 0:00 ps aux --sort +start_time
root 15873 0.0 0.1 3756 676 pts/2 S+ 16:45 0:00 tail -n 4
Thanks in advance
[Solved]
# ps aux --sort +start_time | tail -n 5 | awk 'NR==1{print $2 " " $11}'
another solution for a concrete process
# pgrep -n 'name_of_the_process'

The awk process is started, and occupies another line in the output. Try tail -n 5, but it's not reliable, as another process could be started in between, anyway. See psgrep.

You could get the pid of the last process with:
echo $!

Related

How I can count how many suspended proceses there are in a shell script linux bash

I tried to write a shell script that shows and counts how many suspended processes there are.
But I succeeded only to show the suspended processes with:
#!/bin/bash
list_ps=`ps aux | awk '$8~/T/'`
echo "$list_ps"
I tried to count the suspended processes with:
nr=0
for i in $list_ps
do
nr=`expr $nr + 1`
done
Of course this didn't work because it counted every word there was even with the first row that had the USER PID STAT COMMAND.
Can you give me any suggestion on how I should do it?
Also here is the output for "ps aux | awk '$8~/T/" after I stopped some sleep processes.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
alexsan+ 6441 0.0 0.0 9008 736 pts/0 T 16:17 0:00 sleep 5000
alexsan+ 6511 0.0 0.0 9008 820 pts/0 T 16:18 0:00 sleep 5000
alexsan+ 7041 0.0 0.0 9008 760 pts/0 T 16:21 0:00 sleep 333
additional characters can be added to the state field (depending on the options you use), so this might be a safer approach:
ps aux | awk '$8~/T/'
to count how many processes you have with a header :
ps aux | awk '$8~/T/' | wc -l
to skip the header :
count=$(ps aux | awk '$8~/T/' | wc -l)
echo $((count -1))
one line version :
echo $(( $(ps aux | awk '$8~/T/' | wc -l)-1))
within single awk :
ps aux | awk 'NR>1 && $8~/T/' | wc -l
Using the option --no-header of the ps command, you will not need to substract 1 from the number of lines of output you get from wc -l, so you just have to issue:
ps --no-header aux | awk '$8 ~ /T/' | wc -l
And since the header will contain the 'T' of 'STAT' in field #8, it was necessary to filter this line out.
Within single awk could you please try following.
echo $(( $(ps aux | awk '$8~/T/{count++} END{print count-1}')))
Or to assign to a variable simply:
var=$(ps aux | awk '$8~/T/{count++} END{print count-1}')

Show particular column for command [duplicate]

This question already has answers here:
Using cut command to remove multiple columns
(4 answers)
Closed 4 years ago.
I want to show only specific columns for all the records in the command.
Example: docker ps shows me data in 10 columns. It can have space in between the column headers. My requirement is to get only 2-4 columns in some sequence.
Is there a direct way to do that in any of the commands that respond in a tabular way?
I am new to Linux and thinking if this can be feasible. Thanks.
For example:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED NAMES
123 image "ABC DEF" 10 hours ago image ABC
Here in the above scenario, CONTAINER ID is one header column but there is space, and in the row for NAMES columns can have space in between.
Using AWK, CUT etc it is trickier to write a common script for all the commands because they work on "space" logic.
You can use awk like that:
$ ps | awk '{print $2 " " $3 " " $4}'
TTY TIME CMD
pts/22 00:00:00 bash
pts/22 00:00:00 ps
pts/22 00:00:00 awk
Or together with column -t for more readable output:
$ ps | awk '{print $2 " " $3 " " $4}' | column -t
TTY TIME CMD
pts/22 00:00:00 bash
pts/22 00:00:00 ps
pts/22 00:00:00 awk
pts/22 00:00:00 column
As William Pursell noted in the comment below awk command can be simplified:
$ ps | awk '{print $2, $3, $4}' | column -t
TTY TIME CMD
pts/9 00:00:00 bash
pts/9 00:00:00 ps
pts/9 00:00:00 awk
pts/9 00:00:00 column
Piping the output to tr and cut:
docker ps | tr -s " " | cut -d " " -f 2-4
-s flag on tr squeezes characters and leaves only one " "
-d on cut tells to split field by " ".
Sample output using Perl. Perl has 0 based index, so you have to use 1..3 meaning from 2 to 4
$ ps
PID PPID PGID WINPID TTY UID STIME COMMAND
14556 11424 14556 6944 cons0 197609 22:10:27 /usr/bin/ps
11424 1 11424 11424 cons0 197609 22:41:21 /usr/bin/bash
$ ps | perl -F'\s+' -lane ' print "#F[1..3]" '
PID PPID PGID
11208 11424 11208
11424 1 11424
380 11424 11208
If you don't need headers.. then
$ ps | perl -F'\s+' -lane 'if($.>1) { print "#F[1..3]" } '
6072 11424 6072
11424 1 11424

Get only command as output in ps aux AIX

With the below command:
# ps aux
USER PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root 17760468 0.0 0.0 1180 1220 - A 18:15:00 0:00 iostat -Dsal 6
root 16777310 0.0 0.0 3172 408 - A 18:15:00 0:00 /usr/bin/perl
here i don't want iostst -Dsal 6 under COMMAND, instead it should show only iostat. I mean only command is needed not with its all options and parameters.
Reason for this query:
I am trying to use this below command on my AIX box -
ps aux | tr -s " " | head -1 | awk '{print $1,$2,$3,$4,$11}' ; ps aux | sort -rn +2 | tr -s " " | head -10 | awk '{print $1,$2,$3,$4,$NF}'
Since i have used NF in awk command, it will display only the last column of the line. In my case it will display "6" instead of iostst -Dsal 6 under COMMAND field.
Please help me here..!

Monitor Linux process by user or name

i need to get stats from my Centos 6.7 with Cpanel and send to my external monitor server. What I would like to get is an average cpu load per user or per process name in the last 3 minutes. After many research and test not found any praticable solutions apart bash run top with
top -d 180 -b -n 2 > /top.log
second iteration looks like...
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
38017 mysql 20 0 760m 265m 6324 S 1.4 14.2 244:27.08 mysqld
39501 nobody 20 0 1047m 93m 7068 S 0.1 5.0 0:06.80 httpd
54877 johnd 20 0 32728 3612 2364 S 0.0 0.2 0:00.09 imap
51530 johnd 20 0 353m 5372 1928 S 0.0 0.3 0:04.17 php-fpm
39500 nobody 20 0 1046m 79m 3656 S 0.0 4.3 0:02.57 httpd
7 root 20 0 0 0 0 S 0.0 0.0 27:47.61 events/0
39497 nobody 20 0 1046m 84m 7784 S 0.0 4.5 0:02.77 httpd
etc...
then grep (only on the second iteration output) with COMMAND or USER, sum and divide by 100 to get value like cpu-load
echo "$PRTGTOP" | grep johnd | awk '{ sum += $9; } END { print sum/100; }'
I should probably also try to count the process times etc ?, maybe there is a simpler way to achieve the same result, maybe with third-party software to generate stats?
Thanks.
top gets its info from /proc/*/stat. Each numerical directory under /proc is a process number for a currently running process.
It may be easier for you to collect data directly from those directories. The data format is well defined and can be found in man proc under the subsection called "/proc/[pid]/stat".
You can try the pidstat tool (part of the sysstat package):
pidstat -C httpd -U johnd -h -u 180 1 | awk '{ sum += $7; } END { print sum/100;}'
This will return the percentage CPU usage of all processes matching the httpd command string and the johnd user over a 180-second interval.
ok, pidstat is better, thanks!, but if USER pid is run for only few seconds no cpu use is reported. i found best result with:
#run pidstat with 10 iterations for 18 times
pidstat -U -u 10 18 > /pidstat.log
then
#sum all cpu usage and divide by 18
cat /pidstat.log | grep -v Average | grep johnd | awk '{ sum += $8; } END { print sum/100/18;}' OFMT="%3.3f"
cat /pidstat.log | grep -v Average | grep httpd | awk '{ sum += $8; } END { print sum/100/18;}' OFMT="%3.3f"
with this i get best cpu usage stat per USER even if process is run only for few seconds but with high cpu usage

Limit grep's output

I'm trying to make a server manager, but I need to grab the process IDs and Commands of some processes.
For example:
ps ax | grep ./skulltag
4760 pts/2 Tl 0:02 ./skulltag-server
4793 pts/2 Tl 0:01 ./skulltag-server
4956 pts/2 Tl 0:01 ./skulltag-server -port 13000
4958 pts/2 Tl 0:26 ./skulltag-server -port 13001
How would I get it to only return the process, only return the command (./skulltag-server) or both? Thanks.
You can pipe to awk to select which field to output
E.g. ps ax | grep ./skulltag | awk '{ print $1 }' will print the first column (pid)
Note that you may also want to look into using the -o option to ps to modify its output
I think you should use awk
ps ax | grep ./skulltag | awk '{print $1}' # Or $5, or $1 and $5
This will give you the list of the process ids.
For getting process ids you can also use non-standard but handy pgrep.
ps ax | grep ./skulltag | awk '{ print $1 }'
Is roughly equivalent to:
pgrep skulltag
You could parse the results of ps ax using awk to extract the columns you want:
aix#aix:~/tmp$ ps ax | grep bash
1906 pts/5 Ss+ 0:00 bash
13749 pts/31 Ss 0:00 bash
27315 ? SN 0:00 /bin/bash /etc/cron.daily/backup
27648 pts/31 S+ 0:00 grep --color=auto bash
aix#aix:~/tmp$ ps ax | grep bash | awk '{print $1}'
1906
13749
27315
27652
aix#aix:~/tmp$ ps ax | grep bash | awk '{print $5}'
bash
bash
/bin/bash
grep

Resources