Title: Capturing Results In Shell
mmrobinette - September 7, 2006 01:20 PM (GMT)
Hey crew ...
I need a little assistance on a Shell Script command.
Essentially we need to capture the results from the following command in a variable: "ps -ef | grep MSCM".
What we are trying to do is automate the killing of the MSCM PID in Unix. Any brainstorming or ideas is VERY welcome. What we want to do is capture JUST the pid in a variable. Then we can do a KILL command in the next line killing the process.
Please help. B)
trezaei - September 7, 2006 05:10 PM (GMT)
Ahhh the ever so useful and yet unpopular awk and sed conversation.
I suggest you look into these two commands: awk and sed. They're basically a language in their own right and they can be quite useful.
here's an example of how this could be used
| CODE |
| $size=`ls -al $_[0] | awk {\'print \$5\'}`; |
The above is perl code and it gets the size of a file ($_[0])
but go ahead and try this from the prompt:
| CODE |
| ls -al | awk {'print $5'} |
I think you get the idea. It gives you back the 5th column. You can do the same thing with ps so:
| CODE |
| ps -ef | grep MSCM | awk {'print $1'} |
The column number above is 1 but you can choose whatever column you want.
I am sure there is a better way and I am equally sure someone will post it soon. Hope that works.
T
3monkeys - September 8, 2006 08:35 PM (GMT)
Every night I have root run a script to stop and restart rmi, tomcat and apache. I found that tomcat wouldn't always stop so I added some extra scripting to do a kill on the PID. Below is a portion of that script:
| CODE |
tomcat stop >> $LOGFILE sleep 15 echo Is tomcat stopped? >> $LOGFILE if test `ps -ef |grep tomcat|grep -v grep|wc -l` -gt 0 then echo WARNING: Tomcat is still up! >> $LOGFILE echo `ps -ef|grep tomcat|grep -v grep` >> $LOGFILE tomcatid=`ps -ef|grep tomcat|grep -v grep|cut -c 10-15` echo Killing $tomcatid >> $LOGFILE kill $tomcatid echo >> $LOGFILE else echo Tomcat has stopped. >> $LOGFILE echo >> $LOGFILE fi |
The $LOGFILE was defined earlier in the script so I could go back and check on things at some later time if necessary. On the 8th line is where I get the PID and then I kill it on the 10th. Everything else really isn't needed, but it helps me keep track of what happened. Make sure you're getting the whole PID when you do the cut command though - account for larger PID's than the common 5 characters. I made the mistake of only getting columns 11-15 once. Once! Good luck.
-GW
Keith_G_Thompson - September 12, 2006 05:56 PM (GMT)
trezaei's suggest is right on the mark. Use his command, but substitute "{print $2}" at the end for the ID of the process you're trying to kill.
Phil Feller - September 13, 2006 03:58 AM (GMT)
You will note in 3monkeys example that putting the commands in back ticks (` `) performs command substitution, so that you can populate a variable with the command output. Korn shell also provides $( ) as a valid syntax for command substitution.
If you use Solaris, you have the pkill command available and can do what you want in only one command.