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