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

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

Related

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..!

how to get process id of specific process?

Say there are 3 process with names abc, abcd and abcde.
I'm using the following command to find out the process id:
ps -ef | grep abc | grep -v grep
This gives the output for all the 3 processes with their corresponding pids:
user 6009 1 0 May 11 ? 0:23 ./abc
user 28047 1 0 Apr 24 ? 0:04 ./abcd
user 28548 1 0 Apr 27 ? 0:04 ./abcde
Now what I want is a grep thing that outputs the process id of just abc without returning abcd and abcde. I know using grep -v "processname" eliminates what i want but is there anything simple and specific?
ps -ef | grep -w abc | grep -v grep
^
`--- match whole words only
sure, there is:
pidof
Here is the man page: http://linux.die.net/man/8/pidof
For example write:
pidof abc
output:
6009
This is exactly what pgrep is for.
Specifically, for this you would use pgrep -x abc.
To avoid having to pipe into grep -v grep, do this:
ps -ef | grep '[a]bc\>'
the \> is an end-of-word boundary marker, so you won't match "abcd" or "abcde"
Putting one character into a bracket expression means that regex will match the string abc but it will not match the string grep [a]bc
I do this often enough that I wrote a function, psg
psg () {
local -a patterns=()
(( $# == 0 )) && set -- $USER
for arg do
patterns+=( "-e" "[${arg:0:1}]${arg:1}" )
done
ps -ef | grep "${patterns[#]}"
}
ps -ef | grep -w 'abc$' | grep -v grep
i.e. end of line ($) after abc

Last pid process 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 $!

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