The UNIX and Linux Forums

The UNIX and Linux Forums (http://www.unix.com/index.php)
-   Shell Programming and Scripting (http://www.unix.com/shell-programming-and-scripting/)
-   -   Kill a PID using script (http://www.unix.com/shell-programming-and-scripting/170573-kill-pid-using-script.html)

rajkumarme_1 11-03-2011 11:10 PM

Kill a PID using script
 
Hi,

I wrote a script to kill a process id.

I am able to kill the PID only if I enter the root password in the middle of the execution because I did not run as root i.e after i run the script from the terminal, instead of killing directly, it is killing only after entering the pass when it prompts.

Can you help me out in addressing this issue?

Thanks in advance

ahamed101 11-03-2011 11:36 PM

Well, show the code snippet where you are killing the PID. If you are not the owner, normally it should say "Permission denied". I suppose you are using sudo or something?

You can kill a process only if you have started it. Only root can kill any process. You can have a look into sudoers if you need root privileges for killing any process.

--ahamed

rajkumarme_1 11-05-2011 02:00 AM

Hi Ahamed,

Below is the code snippet of the script that I am working on:

Code:

sudo su -
ps -ef | grep -i skype > tmp
cat tmp | grep -v grep | awk '{print $2}' > tmp.txt

kill -9 $(cat tmp.txt | head -1)


I am not sure if the sudo should be used in a different manner.

Thanks!

ahamed101 11-05-2011 02:44 AM

So, you are indeed using sudo. You need to configure the sudoers for avoiding the password.
Something like this in your /etc/sudoes file...
Code:

raj  ALL=NOPASSWD:/full/path/to/your/script

#remove "sudo -" from the first line and execute your script like this
sudo /your/script

BTW, you can try this for killing the skype process...
Code:

#Entry in sudoers
raj  ALL=NOPASSWD:/bin/kill

#Command
sudo kill - 9 $(pgrep skype)

HTH
--ahamed

rajkumarme_1 11-07-2011 12:55 PM

I wanted to extend the above functionality to check for a file size and depending on that I want to restart a process.

But there is an error thrown as shown below:
Code:

CheckDATfileANDKillEngineANDRestart.sh: line 18: [: -gt: unary operator expected
DAT File size lesser
 
CheckDATfileANDKillEngineANDRestart.sh: line 18: [: -gt: unary operator expected
DAT File size lesser
Segmentation fault

The code performing this task is below:
Code:

#!/bin/sh
 
 
engineEXECUTE()
{
 
sleep 2
echo "In the engine function"
killENGINE
 
}
 
checkFILESIZE()
{
FILESIZE=$(ls -al File.dat | awk '{print $5}')
echo $FILESIZE
 
if [ "$FILESIZE" -gt 60000 ]; then
echo "DAT File size greater"
engineEXECUTE
else
echo "DAT File size lesser"
checkFILESIZE
fi
}
 
killENGINE()
{
sleep 2
echo "Inside KILLENGINE"
 
 
ps -ef | grep -i "CONTROLFILE" > tmpPROCESS
cat tmpPROCESS | grep -v grep | awk '{print $2}' > tmpPID
 
kill -9 $(cat tmpPID | head -1)
OUT=$?
if [ $OUT -eq 0 ];then
echo " killed"
else
echo " not found"
#<start process>
exit 1
fi
 
kill -9 $(cat tmpPID | tail -1)
OUT=$?
if [ $OUT -eq 0 ];then
echo " killed"
else
echo " not found"
exit 1
fi
 
rm tmpPID
rm tmpPROCESS
 
checkFILESIZE
}
 
checkFILESIZE

Can you please let me know why is there that error and how can I fix it?

Thanks!

CarloM 11-07-2011 01:01 PM

Does File.dat exist? Otherwise FILESIZE will be empty string.

ahamed101 11-07-2011 01:02 PM

$FILESIZE might be empty here... Check if the file File.dat is present in the directory where this code is being executed
Code:

if [ "$FILESIZE" -gt 60000 ]; then
--ahamed


All times are GMT -4. The time now is 05:03 PM.

Linux and Unix Supported by: vBulletin
Search Engine Optimisation provided by DragonByte SEO v1.1.4 (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
The UNIX and Linux Forums Content Copyright ©1993-2013. All Rights Reserved.
Forum Operations by The UNIX and Linux Forums