Bash Script to Get Specific User(S) Id and Processes Count

Bash script to get specific user(s) id and processes count

#!/bin/bash

users=$@
args=()
if [ $# -eq 0 ]; then
# all processes
args+=(ax)
else
# user processes, comma-separated list of users
args+=(-u${users// /,})
fi

# print the user field without header
args+=(-ouser=)

ps "${args[@]}" | awk '
{ tot[$1]++ }
END{ for(id in tot){ printf "%s\t%4d\n", id, tot[id]; cntu++ }
printf "%4d users has total processes:%4d\n", cntu, NR}'

The ps arguments are stored in array args and list either all processes with ax or user processes in the form -uuser1,user2
and -ouser= only lists the user field without header.

In the awk script I only removed the NR>1 test and variable cntp which can be replaced by NR.

Possible invocations:

./myScript.sh
./myScript.sh root daemon
./myScript.sh root,daemon

Displaying userIDs for system processes in bash

You can do this in one line:

ps -eo uid= | tr -d ' ' | grep -vx 0 | sort | uniq

The steps are:

  • Print UIDs of all running processes
  • Trim whitespace (since ps right-justifies the numbers)
  • Remove any lines that say 0 (e.g. the root user)
  • Sort the remaining lines so that duplicates are grouped together
  • Remove adjacent duplicates

If you want to show user names instead of UID numbers:

ps -eo user= | grep -vx root | sort | uniq

As mentioned in a comment below, you can specify ruid or ruser instead of uid or user, if you want the processes' "real" (rather than "effective") UIDs or usernames. (If you run a setuid program, the "effective" user is the owner of the program, but the "real" user is still you.)

Also, you can add -c to the uniq command to get a count of how many processes each user owns. You can even sort again by number of processes:

ps -eo user= | grep -vx root | sort | uniq -c | sort -nr

Get the username and the process ID of a process in bash

This can happen if the username is longer than 8 characters (OR) id has no name. But, If you want the username in the ps output then try this,

ps -eo uname:20,pid,pcpu,pmem,sz,tty,stat,time,cmd | grep '[b]ash'

The number of processes a user is running using bash

Give this a try:

ps -u "$(echo $(w -h | cut -d ' ' -f1 | sort -u))" o user= | sort | uniq -c | sort -rn

In order to properly handle usernames that may be longer than eight characters, use users instead of w. The latter truncates usernames.

ps -u "$(echo $(printf '%s\n' $(users) | sort -u))" o user= | sort | uniq -c | sort -rn

Bash Shell script to check how many processess are running and issue warning if exceeds 20?


number=$(ps -ef | grep icinga | wc -l)
if ((number >= 20 && number <= 40)); then
# your code for warning
elif ((number > 40 && number <= 70)); then
# your code for critical warning
fi

Id argue you wouldnt need the less than 70 part , as i imagine if it was 71 youd be really worried and the code wouldnt do anything :)

Shell Script:How to get all the Process Id which are using a file-system/directory

If you only want id instead of id(user) then don't use the -u option. Documentation of fuser -u:

-u, --user

Append the user name of the process owner to each PID.

For me, fuser -c / has a different format than your sample. Each id is followed by letters denoting the type of access. The letters are printed to stderr, therefore I will use 2>&- to hide them.

$ fuser -c /
/: 1717rce 1754rce 1765rce 1785rce ...
$ fuser -c / 2>&-
1717 1754 1765 1785 ...

You can use grep to print one id per line:

$ fuser -c / 2>&- | grep -o '[0-9]*'
1717
1754
1765
1785
...

However, to run a loop you don't need one id per line. Ids separated by spaces work as well:

for id in $(fuser -c / 2>&-); do
echo "id = $id"
done

Shell script to capture Process ID and kill it if exist

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

Hope it helps.



Related Topics



Leave a reply



Submit