I'm having trouble with part of this bash script in Linux where I respawn a new instance of script and kill the old one to prevent forking (Yes, I know 'exec' will not fork but this needs to be interactive) When the old instance is kill it pops up "Terminated!" in the middle of the new instance that's running. I've seen a lot of suggestions to answer this here but either I'm not doing it right, or they're all wrong.
Here's the problematic part of the code:
control_c()
{
echo -e "Would you like to do something?\n"
echo -e "Enter y or n: "
read yn
if [ "$yn" == "y" ]; then
echo -e "\nDoing something...\n"
if [ -n $sumvar ]; then
echo -e "\nDoing Something else...\n"
bash what.sh
else
bash what.sh
fi
fi
else
exit 0
fi
}
trap control_c SIGINT
trap "rm -f /var/run/mine.pid" EXIT
pid=/var/run/mine.pid
if test -e $pid
then
kilt=$(awk 'NR==1' $pid)
exec kill -15 $kilt 2> /dev/null
else
echo $$ > $pid
fi
Well, I've written a very basic IDS. Sorry for not posting the code in its entirety, but its over 200 lines. It might be a lot to sort through and I really don't want to show it until its finished away. This is the last part I'm on.
Trying to get the "Terminated" message to not display. Even though the new instance of the script keeps running, a message will pop up saying "Terminated" (caused by killing the old instance's pid) and then a new command prompt. If you wait a second the new instance will give more output, but I don't want it to look to the user that the script has crashed. That's basically all.
Not whatever manner you feel hellbent on accomplishing it with.
I only mean what, you are trying, to do. What function this script is supposed to accomplish. WHY you are killing processes in such a manner. Why you think this "prevents forking".
Until you explain that, your script isn't going to make much sense. I'm not sure it ever will -- it may need replacing, not minor fixes.
Don't know how explain it much better than the title of this thread. That's what I was trying to do. Make the message stop happening. With that the script would work and look fine. Sorry, I thought my explanations would make things clearer.
Because when it forks it kills one of the 2 current processes. If you know a better way than this or using exec, by all means show me.
Sorry, I could see I was having trouble explaining this to you. I thought sending you the entire code would let you see what I was talking about. It won't happen again.