Getting PID of process in Shell Script - linux

I am writing one shell script and I want to get PID of one process with name as "ABCD". What i did was :
process_id=`/bin/ps -fu $USER|grep "ABCD"|awk '{print $2}'`
This gets PID of two processes i.e. of process ABCD and the GREP command itself what if I don't want to get PID of GREP executed and I want PID only of ABCD process?
Please suggest.

Just grep away grep itself!
process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print $2}'`

Have you tried to use pidof ABCD ?

It's very straight forward. ABCD should be replaced by your process name.
#!/bin/bash
processId=$(ps -ef | grep 'ABCD' | grep -v 'grep' | awk '{ printf $2 }')
echo $processId
Sometimes you need to replace ABCD by software name. Example - if you run a java program like java -jar TestJar.jar & then you need to replace ABCD by TestJar.jar.

ps has an option for that:
process_id=`/bin/ps -C ABCD -o pid=`

You can also do away with grep and use only awk.
Use awk's expression matching to match the process name but not itself.
/bin/ps -fu $USER | awk '/ABCD/ && !/awk/ {print $2}'

You can use this command to grep the pid of a particular process & echo $b to print pid of any running process:
b=`ps -ef | grep [A]BCD | awk '{ printf $2 }'`
echo $b

ps | pgrep ABCD
You can try the above command to return the process id of the ABCD process.

I found a better way to do this.
top -n 1 | grep "##" | grep -Eo '^[^ ]+'

Related

How does this line of code work in shell?

I got confused about this line of code, could anyone explain how this code work? I can understand that its using a pipeline, but codes in the middle confuses me.
for pid in $(ps -e -f | grep $1 | grep -v $0 | awk '{print $2}')"
Check below details to understand the command, full command will be like this-
cat script.sh
for pid in $(ps -e -f | grep $1 |grep -v $0 | awk '{print $2}')
do
kill -9 $pid
done
You need to execute it like below -
./script.sh vipin
step1 : ps -e -f (will print all the processes running in the server)
step2 : ps -e -f|grep $1 (considering $1 is variable for current user,
in my case is vipin(user) which i will pass with script,so step2
will filter all the process for that user.
step3 : And $0 is the script name (script.sh), which you don't want to kill
that is why you are using grep -v(to exclude)
step4 : awk '{print $2}' to fetch only the process number.

Linux Script to selectively kill processes

I'm looking at a way to automate the following:
Run ps -ef to list all processes.
Filter out those rows containing java in the CMD column.
Filter out those rows containing root in the UID column.
For each of the filtered rows, get the PID column and run pargs <PID>.
If the output of pargs <PID> contains a particular string XYZ, the issue a kill -9 <PID> command.
To filter out rows based on specific column values, is there a better way than grep? I can use
ps -ef | awk '{print $1}' | grep <UID>
but then I lose info from all other columns. The closest thing I have right now is:
ps -ef | grep java | grep root | grep -v grep | xargs pargs | ?????
EDIT
I was able to solve the problem by using a using the following script:
ps -ef | awk '/[j]ava/ && /root/ {print $2}' | while read PID; do
pargs "$PID" | grep "Args" > /dev/null && kill -9 $PID && echo "$PID : Java process killed!"
done
both anubhava's and kojiro's answers helped me reach there. But since I can only accept one answer, I tagged kojiro's answer as the correct one since it helped me a bit more.
Consider pgrep:
pgrep -U 0 java | while read pid; do
pargs "$pid" | grep -qF XYZ && kill "$pid"
done
pgrep and pkill are available on many Linux systems and as part of the "proctools" packages for *BSDs and OS X.
You can reduce all grep by using awk:
ps -ef | awk '/[j]ava/ && /root/ {print $1}' | xargs pargs
Searching for pattern /[j]ava/ will skip this awk process from output of ps.
You can also use pkill if it is available on your system.

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)

Resources