Only USERID, PID, TIME, and COMMAND for specific user Shell Script - linux

I have to get Only USERID, PID, TIME, and COMMAND for a specific user. I have tried
ps -u user1
But that display
PID TTY TIME CMD, but I dont want the Field TTY.
Can any one help me to find the USERID, PID, TIME, and COMMAND of a specific user

Use the ps option -o to specify the output with uid,pid,time,cmd
ps -u user1 -o uid,pid,time,cmd

You can use this command to get the required details
# ps -e -o pid,time,cmd,user | sed 1q; ps -e -o pid,time,cmd,user | grep '[u]ser_name'
replace username with your user. For example, alen would be '[a]len'

Related

ps axo : display full username based on PID number

I am trying to display full username by PID number
The first Approach i tried :
PID=12345
PID_USER=`ps -u -p ${PID}| awk '{print $1}' | tail -1`
echo $PID_USER
this will display as systemU+, i want to display as SystemUser_Farid
i have tried a second approach using ps axo , script used as
ps axo user:20,pid , however , this is printing the whole list of processes , using ps axo -p ${PID} user:20,pid doesnt show any result.
Use -o format for a user-defined format and only print the user name:
ps -o user= -p "$PID"
The = disables printing the header line.

linux ps command to count number of running processes

does anyone know how I can pipe the results of ps -ef | grep ^$USER to wc -1
I already used ps -ef | grep ^$USER but know i want to pipe the command
As per my understanding of your question
you want all the running process from a particular user and pipe it to wc ( note its wc -l not -1)
so i used this
ps aux | grep ^$USER|wc -l
-a : Information for all processes associated with terminals.
-u : Information for processes in userlist.
-x : username (user running this command)
or this can also work
ps -u $USER|wc -l
for any commands if you want to know the details try man command in terminal for example man ps

How to grab usernames of ssh connections?

I need a script which cuts every single username with the following command
"ps -ef | grep '[s]shd' | grep -v ^root"
After that, I want to put each user in the following group with that command
"gpasswd -a $user nginx"
Operating system: CentOs 8.2
Cutting the username out of the ps -ef output is possible but tricky and potentially fragile. You should look at the options to ps to get exactly the information you want without anything extraneous.
e.g. ps -C sshd -o user= looks to be close to what you want.

Bash - Killing all process in a single command

I have the following bash command which works quite good:
ps aux | grep mpg321 | grep -v "grep mpg321" | awk '{print $2}' | xargs kill
The only problem is, when there is no mpg321 being executed it returns an awful message which I would not like to display to the user.
Is there a "quiet" parameter of a way to put an if in this statement to avoid returning the message?
Usage:
kill [options] <pid> [...]
Options:
<pid> [...] send signal to every <pid> listed
-<signal>, -s, --signal <signal>
specify the <signal> to be sent
-l, --list=[<signal>] list all signal names, or convert one to a name
-L, --table list all signal names in a nice table
-h, --help display this help and exit
-V, --version output version information and exit
For more details see kill(1).
Thank you!
How about killall?
killall mpg321
You can also use pkill as an alternative to killall
By the way xargs has a --no-run-if-empty option if you don't want to execute the command when there are no parameters to add.

How to find a PID of a process whose name I don't know exactly?

I can get the PID of a specific process name by
pidof$(ps -C netns)
but what if I don't know the name of the process exactly?
I can't type something like
pidof$(ps -C net*)
so is there any wildcard character, or is there another solution?
Use the -A (all processes) option and filter the result through grep:
pidof $(ps -A | grep "net*")
Just use pgrep -l, eg:
$ pgrep -l sh
1821 sshd
2590 ssh-agent
2658 sh
2677 bash
3025 gvfsd-trash
14785 ksh93
17723 ksh93
try the following and see if you can discover the process as such
This will give you all processes for all users, in a full-format listing
ps auxf
where :
axu = To see every process on the system using BSD syntax
f = fullformat
if the list is too long you can filter if you have an idea of the process name
For example the command below will show you the pids for chrome.
ps auxf | grep chrome
you can use grep and pip :
pidof$(ps -c |grep yor_pattern)

Resources