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

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

1 Like

Hi Ahamed,

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

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!

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

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...

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

#Command
sudo kill - 9 $(pgrep skype)

HTH
--ahamed

1 Like

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:

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:

#!/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!

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

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

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

--ahamed

Yes, File.dat is existing in the current directory where this script is executed. The error is occuring when it is executed, runs for few minutes without any issues and then throws that error.

May be the file is getting deleted or moved by someother process when the size threshold is reached... thats why you are getting the error after sometime...
I took the liberty to clean up your code and made a few changes...

#!/bin/sh
file="File.dat"
size=60000
engineEXECUTE()
{
   sleep 2
   echo "In the engine function..."
   killENGINE
}
checkFILESIZE()
{
   sleep 2
   test -f $file || echo "$file not found" && exit 1
   FILESIZE=$(stat -c %s $file)
   if [ $FILESIZE -gt $size ]
   then
      echo "$file size is greater than $size..."
      engineEXECUTE
   else
      echo "$file size is less than $size..."
      checkFILESIZE
   fi
}
killENGINE()
{
   sleep 2
   echo "Inside KILLENGINE..."
 
   kill -9 $(pgrep CONTROLFILE)
   if [ $? -eq 0 ]
   then
      echo "CONTROLFILE process killed..."
   else
      echo "CONTROLFILE process not running... Exiting..."
      exit 1
   fi

   checkFILESIZE
}

checkFILESIZE

It is not tested though... And I am assuming "CONTROLFILE" here is a process.

HTH
--ahamed

1 Like

Appreciate your help ahamed. But I tested this in Ubuntu and it worked without any issue. When i moved the code code to a development linux environment, then this issue is occuring.

Is it that there is an issue with the if statement in my code?

Or if I use your if statement block, it might fix?

My code checks for the file and then goes forward... I suggest you try this code and keep an eye on the file... I strongly believe that File.dat is being manipulated by some other process... Check for the message "File.dat not found" on the screen, that should tell you whats happening...

--ahamed

1 Like

The code you provided above probably has a typo in testing for the file existence. So, I removed one pipe symbol and then used the code you mentioned. It did not log any message on to the screen and just exited when I ran the script. But the DAT file was present at that time.

Just an FYI, the dat file is created every time the process runs.

So, I am still getting the error.

Any other change that I can test?

Where was the typo?... You mean in the test statement?... Please paste the error...

--ahamed