display all the ssh pids where the users are connected to in one line [duplicate] - linux

This question already has answers here:
How to join multiple lines of filenames into one with custom delimiter
(22 answers)
Closed 7 years ago.
How can I display all the ssh pids where the users are connected to in one line separated by comma ?
This command displays the output in multiple lines, I would like to have the output in one line.
ps aux | grep -i "ssh" | awk '{print $2}'
from
1325
3255
2323
5321
3252
To
1325, 3255, 2323, 5321, 3252
Thank You!

You may want to use pgrep to get the processes IDs directly:
$ pgrep ssh
1217
5305
This way, you avoid calling ps aux and parsing its output, which will always contain the grep itself.
To join them on a ,-separated list, use paste on a -serial mode:
$ pgrep ssh | paste -s -d,
1217,5305

You could use sed utility in another pipe, so the command would be:
ps aux | grep -i "ssh" | awk '{print $2}' | sed ':a;{N;s/\n/, /};ba'
Where you are in fact replacing new lines (except the final one) by commas.

Related

Capture pid of a process started from a terminal with its unique command line

I'm trying to get the process id of multiple processes run from a multiple tmux windows. Each processes started have their unique command line. I think only the command line is unique as there can be multiple processes with same name. what i did currently is
process_pid=$(ps --no-headers aux | grep "${process_cmd_line}" | grep -v grep | awk '{print $2}' | tr '\n' '')
This works for me. But i want to know if this is the correct approach to do this. I know there are output format specifiers. Any example to do the same with the format specifier or an improvement over the code above?

grepping output of ps, exclude the word grep [duplicate]

This question already has answers here:
More elegant "ps aux | grep -v grep"
(9 answers)
Closed 3 years ago.
I'm using ps to find the pid of a process created to execute the command "sleep 1234 &"
I grep the result to match only "sleep 1234".
ps -A -f | grep "sleep 1234"
however, this matches also the command "grep sleep 1234" itself, returning two lines instead of one. How do I write a pattern for grep to exclude the word 'grep' itself?
Thanks
This is a pretty common problem and the easiest solution is to just surround a character in the grep'ed pattern with square brackets:
ps -A -f | grep "[s]leep 1234"
This will now match sleep 1234, but not [s]leep 1234 (because of the literal ] between s and l), and the grep line no longer matches.
The reason that the grep is in the process list is that pipelines are executed from right to left, so the grep is actually executed prior to the ps.

I'm trying to understand how to use special characters in grep and wc [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
Can you explain in detail what happened here on this command?
grep -nw '^root' /etc/passwd
What is the use of ^ and '? Please give me examples and be detailed cuz all I'm hearing is that it's the beginning of the line. I don't understand what each of those special characters means.
How would I go about using wc, grep and ^ to find out the number of non-root processes running on my system?
grep -nw '^root' /etc/passwd
It reads the /etc/passwd file line-by-line and filters out all those lines that ^=begin with the -w=complete word "root". So, for example the line
root:x:0:0:root:/root:/bin/bash
To see all processes on a system, you could use ps aux. and it will show lines like this
root 22866 0.0 [...] 0:00 [kworker/1:0]
As you can see, the lines start with a username. If you pipe the ps aux output through grep, you can use the same RegEx from above, to filter out all lines that do not start with "root".
Use -v to invert pattern matching, so that grep -vw '^root' finds all lines that don't begin with a complete word "root".
ps aux | grep -vw '^root' | wc -l
Finally, wc -l counts the number of lines it receives. So that is the number of all lines from ps aux that do not begin with "root".

grep a variable containing special characters

i'm new to bash scripting and i have to determine if a process is running in a linux environment.
Actually i use the follow command to do the job:
#ps -ef | awk '{print substr($0, index($0,$8))}' | grep -v grep | grep -w -F $PROCESSNAME
where
awk '{print substr($0, index($0,$8))}'
allow me to ignore UID PID PPID C STIME TTY TIME fields and
grep -v grep
allow me to ignore the row that contains the command itself. So at this point i have a list of all processes running on the system.
Finally:
grep -w -F $PROCESSNAME
read a variable which contains the name of the process that i want to check.
For what i understand the full command should return only the row that has the exact value of $PROCESSNAME
Actually this doesn't works correctly for processes that follow the pattern "[processname]", and probably also for other patterns.
For example to simplify, if i have a running process named "[vmmemctl]" and i run:
#ps -ef | grep -v grep | grep -w -F "vmmemctl]"
it actually returns a result:
#root 615 2 0 Feb26 ? 00:01:00 [vmmemctl]
but the actual process name in the command is different from the process name in the result.
What is the correct command that doesn't have this behavior?
Thank you
awk to the rescue!
ps -ef | awk '$8=="[command]"{NF=8;print}'
or
ps -ef | awk -v c="vmmemctl]" '$8==c{NF=8;print}'
note that this is for an exact match not pattern.
since this is an exact match the command can have spaces and other special chars in it (it's not pattern match but string equality). Using your variable name it will look like this
ps -ef | awk -v c="$PROCESSNAME" '$8==c{NF=8;print}'

How to capture the output of a bash command into a variable when using pipes and apostrophe? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I am not sure how to save the output of a command via bash into a variable:
PID = 'ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}''
Do I have to use a special character for the apostrophe or for the pipes?
Thanks!
To capture the output of a command in shell, use command substitution: $(...). Thus:
pid=$(ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}')
Notes
When making an assignment in shell, there must be no spaces around the equal sign.
When defining shell variables for local use, it is best practice to use lower case or mixed case. Variables that are important to the system are defined in upper case and you don't want to accidentally overwrite one of them.
Simplification
If the goal is to get the PID of the raspivid process, then the grep and awk can be combined into a single process:
pid=$(ps -ef | awk '/[r]aspivid/{print $2}')
Note the simple trick that excludes the current process from the output: instead of searching for raspivid we search for [r]aspivid. The string [r]aspivid does not match the regular expression [r]aspivid. Hence the current process is removed from the output.
The Flexibility of awk
For the purpose of showing how awk can replace multiple calls to grep, consider this scenario: suppose that we want to find lines that contain raspivid but that do not contain color=auto. With awk, both conditions can be combined logically:
pid=$(ps -ef | awk '/raspivid/ && !/color=auto/{print $2}')
Here, /raspivid/ requires a match with raspivid. The && symbol means logical "and". The ! before the regex /color=auto/ means logical "not". Thus, /raspivid/ && !/color=auto/ matches only on lines that contain raspivid but not color=auto.
A more straightforward approach:
pid=$(pgrep raspivid)
... or a little different
echo pgrep [t]eleport

Resources