Linux use grep command - linux

I know use ps -ef | grep test| grep -v grep |wc -l can list the num of process test,and now i plan to list the test processes belong to user :forme.is this right as below :
ps -ef | grep test|grep -x forme| grep -v grep |wc -l

For a start, grep test| grep -v grep can be replaced with grep '[t]est'. See here for an explanation.
Secondly, if you want to limit the processes to a single user, that's what the -u option to ps is for:
ps -fu forme | grep '[t]est' | wc -l
And, finally, grep already has a -c option to count lines, so you can ditch the wc part of the pipeline:
ps -fu forme | grep -c '[t]est'

Related

alias in .cshrc to kill processes

I have a bunch of 'abcd' processes that I want to kill and restart frequently. I do this to kill them
kill -9 `ps -ef | grep abcd | grep -v grep | awk '{print $2}'`
Because I do it so frequently, I want to create an alias in the .cshrc file.
alias killabcd 'kill -9 `ps -ef | grep abcd | grep -v grep | awk '{print $2}'`'
But it seems like $2 of awk is getting expanded and I get this error when I open a terminal
Missing }.
What am I doing wrong? How can I create an alias for this in my cshrc?
Simply with pkill command:
alias killabcd 'pkill abcd'
Nevermind! I figured it out!
This is one of the ways to do it:
alias killabcd 'kill -9 `ps -ef | grep abcd | grep -v grep | awk \{print\ \$2\}`'

How to pass parts of a command as variable bash?

a="grep ssh | grep -v grep"
ps -ef | $a | awk '{print $2}'
How can I make the above work? I have a section where I need to not just pass the grep term, but possible pass more than one grep term, meaning I need to pass "term1 | grep term2" as a variable.
Edit:
another.anon.coward answer below worked perfectly for me. Thank you sir!
Create a function instead:
a() {
grep ssh | grep -v grep
}
ps -ef | a | awk '{print $2}'
The other solution is to use eval but it's unsafe unless properly sanitized, and is not recommended.
a="grep ssh | grep -v grep"
ps -ef | eval "$a" | awk '{print $2}'
If you want just the pid of a process, then use pgrep.
pgrep ssh
You can put this in a bash like the following (a.bash) :
#!/bin/bash
pname=$1
pgrep "$pname"
or if you want ps -ef for other purposes as you've written, following inside a script might work:
pname=$1
ps -ef | grep "$pname" | grep -v grep | awk '{print $2}' # I would personally prefer this
OR
ps -ef | eval "$pname" | awk '{print $2}' # here $pname can be "grep ssh | grep -v grep"
change the permission to execute :
chmod a+x a.bash
./a.bash ssh

How to continue executing the next other commands after using "kill" in shell?

I want to merge two different script files into one script file which could do what the two different files do. And the script files is:
script file A:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
script file B:
nohup ./temp_tool &
the merged script file:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
kill -9 ${pid}
nohup ./temp_tool &
The whole merged script file would stop after executing kill command, and I have to modify it to be:
pid=`ps -ef | grep temp_tool | grep -v grep | awk '{print $2}'`
out=`kill -9 ${pid}`
nohup ./temp_tool &
and it works well now, but I don't know why? Is there any difference?
I would say $pid also contains the pid of your script. You can filter it out:
script_pid=$$
pid=$(ps -ef | grep temp_tool | grep -Ev "grep|$script_pid" | awk '{print $2}')
Though if you want the pids of the command temp_tool I would suggest this:
ps -C temp_tool -o pid
Instead of the ps -ef | grep ...

Killing a process

I have a for loop to get the list of PID's and kill each PID. I want to display the entire line of PS output and write it to the /tmp/outfile . But from each line of PS output each field(PID,PPID,...) is written along with a new line in the /tmp/outfile. So if PS output has three lines as output i want to log these three lines into /tmp/outfile but it's breaking each field in the line and adding a new line. how can i do it.
for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep`
do
echo "$list" >> $CUSTOM_TMP/test5566
PID=`echo $list | awk '{print $2}'`
kill -TERM "$list"
done
Your for loop does not iterate the lines but each individual field.
Also your kill command was slightly wrong.
Just change your code to something like:
ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | while read list
do
echo "$list" >> $CUSTOM_TMP/test5566
PID=`echo $list | awk '{print $2}'`
kill -TERM "$PID"
done
Isn't it easier to use the killall command for what you are trying to do?
No need for a loop at all. And this uses tee to write your temp file.
list=$(ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | tee $CUSTOM_TMP/test5566 | awk '{printf "%s ", $2')
kill -TERM $list
You want to run ps before looping:
ps -ef | grep $"{process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep > $CUSTOM_TMP/test5566 2>/dev/null
for PID in `cat $CUSTOM_TMP/test5566 | awk '{print $2}'`; do
kill -TERM $PID
done
rm -f $CUSTOM_TMP/test5566
I would also insert some sanity, possibly using wc to make sure the file actually got some data from ps.
Just move the awk part to the top line, otherwise your code is fine.
for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | awk '{print $2}`
do
echo "$list" >> $CUSTOM_TMP/test5566
PID=`echo $list`
kill -TERM "$list"
done
For a one liner - if your system has pgrep --
pgrep -d ' ' ${process_name} > kill.log && kill -TERM $(< kill.log)

Finding process count in Linux via command line

I was looking for the best way to find the number of running processes with the same name via the command line in Linux. For example if I wanted to find the number of bash processes running and get "5". Currently I have a script that does a 'pidof ' and then does a count on the tokenized string. This works fine but I was wondering if there was a better way that can be done entirely via the command line. Thanks in advance for your help.
On systems that have pgrep available, the -c option returns a count of the number of processes that match the given name
pgrep -c command_name
Note that this is a grep-style match, not an exact match, so e.g. pgrep sh will also match bash processes. If you want an exact match, also use the -x option.
If pgrep is not available, you can use ps and wc.
ps -C command_name --no-headers | wc -l
The -C option to ps takes command_name as an argument, and the program prints a table of information about processes whose executable name matches the given command name. This is an exact match, not grep-style. The --no-headers option suppresses the headers of the table, which are normally printed as the first line. With --no-headers, you get one line per process matched. Then wc -l counts and prints the number of lines in its input.
result=`ps -Al | grep command-name | wc -l`
echo $result
ps -Al | grep -c bash
You can try :
ps -ef | grep -cw [p]rocess_name
OR
ps aux | grep -cw [p]rocess_name
For e.g.,:
ps -ef | grep -cw [i]nit
Some of the above didn't work for me, but they helped me on my way to this.
ps aux | grep [j]ava -c
For newbies to Linux:
ps aux prints all the currently running processes, grep searches for all processes that match the word java, the [] brackets remove the process you just ran so it wont include that as a running process and finally the -c option stands for count.
List all process names, sort and count
ps --no-headers -A -o comm | sort | uniq -c
You also can list process attached to a tty
ps --no-headers a -o comm | sort | uniq -c
You may filter with:
ps --no-headers -A -o comm | awk '{ list[$1] ++ } END { for (i in list) { if (list[i] > 10) printf ("%20s: %s\n", i, list[i]) } }'
Following bash script can be run as a cron job and you can possibly get email if any process forks itself too much.
for i in `ps -A -o comm= --sort=+comm | uniq`;
do
if (( `ps -C $i --no-headers | wc -l` > 10 )); then
echo `hostname` $i `ps -C $i --no-headers | wc -l` ;
fi
done
Replace 10 with your number of concern.
TODO: "10" could be passed as command line parameter as well. Also, few system processes can be put into exception list.
You can use ps(will show snapshot of processes) with wc(will count number of words, wc -l option will count lines i.e. newline characters).
Which is very easy and simple to remember.
ps -e | grep processName | wc -l
This simple command will print number of processes running on current server.
If you want to find the number of process running on current server for current user then use -U option of ps.
ps -U root | grep processName | wc -l
change root with username.
But as mentioned in lot of other answers you can also use ps -e | grep -c process_name which is more elegant way.
ps aux | wc -l
This command shows number of processes running on the system by all the users.
For a specific user you can use the following command:
ps -u <username> | wc -l
replace with the actual username before running :)
ps -awef | grep CAP | wc -l
Here "CAP" is the word which is in the my Process_Names.
This command output = Number of Processes + 1
This is why When we are running this command , our system read thats "ps -awef | grep CAP | wc -l " is also a process.
So yes our real answer is (Number of Processes) = Command Output - 1
Note : These processes are only those processes who include the name of "CAP"

Resources