Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PS command help 2

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
529
US
Is there a way to have the ps command only output the pids ? What I am doing is writing a script to kill off all the processes with in from this command:

ps aux | grep /reports

I just need to modify this so it will only out put the pids.
 
Look at the [tt]pgrep[/tt] command. It will do the [tt]ps[/tt], and the [tt]grep[/tt] in one command, and output the PIDS.
Code:
pgrep command
man pgrep
It looks for the command being run though, not just a string in the command line (/reports).


 
well thats just it. What I have going on is a remote smb volume mounted called "reports" and sometimes I have a communication failure and I have to remount the volume but to umount the volume I have to kill all the processes that are using it.
 
There's a command called [tt]fuser[/tt] that will list all of the PIDs that have a file open. You can even just specify the mount point and it will tell you anyone that has anything open on the mounted system.
Code:
fuser -c /reports
Taking a little further, it can also kill any of those processes too.
Code:
fuser -c -s KILL /reports
See the man page for more info.

If you don't have [tt]fuser[/tt], then something like [tt]lsof[/tt] could be used.

If you don't have that, you could use [tt]ps[/tt] as per your original question. Look at the man page for selecting output fields. I think it's the "-o" option. Output only the PID and full command line. Grep on the command line for your "/reports", then use sed, awk, or cut to grab the PID. Something like that should work.


 
Oops, forgot this was Linux. I gave the Solaris version of [tt]fuser[/tt]. Try this instead...
Code:
fuser -km /reports
That should kill all processes that have anything open on the mount point /reports in any way.


 
Just a thought, newer linux unmount supports the -f (force) mode. I believe that sends kill signal to all processes that have the mount open (this is my assumption if it works like solaris force unmount).
 
well no it doesn't work. I have tried the force option and it doesn't work. What happens is that new processes are started my users when it doesn't work and they probably start after the force starts and it doesn't catch them or something I don't know, all I know is it is hard to manually kill off all the processes. Well I will try that fuser command out and see how it works, Thanks.
 
If you use "ps aux | grep /reports" and ps only outputs PID then you will never find /reports :)

Try something like this:

ps aux |grep /reports |awk {'print $2'} |grep -v PID
 
GunnarD
well that is something that I was trying to do. Can you give a quick run down of how that works?

I noticed that I recieve the same output without the | grep -v PID as I do with it.

So if I do it your way would this work as a bash file?
Code:
#!/bin/bash
for i in $(ps aux | grep /reports | awk {'print$2'})
do 
    kill -9 $i
done
The command that SamBones gave me might be my solution if it works but this is a learning thing for me as well.
 
You can put more that one PID on the command line for [tt]kill[/tt], so it could be something like...
Code:
kill -9 $(ps aux | grep /reports | awk '{print $2}')
This is dangerous though since you are only keying off of "/reports" being in the command line somewhere. You could still miss processes (those with /reports as their current working directory), or include processes you don't want (anything with "/reports" in the command line (line the "grep")).

Read the man page for [tt]fuser[/tt]. The "-km" options are specifically made for killing processes that have anything open on a specific mount point. This is exactly what you're trying to do.


 
well you have a point next time I have an issue i will try it out.
 
Sweet the fuser command seemed to work thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top