Hi,
I am writing a script which will run a process in a while loop, i.e., user will executesh test.sh
and the script will run a set of processes,
I want to know how to kill the script which is having a infinite while loop in its contents.
Killing a process (say test.out) is done bypkill -9 test.out
and a script can be killed usingpkill -9 test.sh
But this is possible only when the user has started the script using./test.sh
But if the user runs like thissh test.sh
Then it cannot be killed usingpkill -9 test.sh
Since both options to run the script should be given to the user, in what way can i kill the script in case the user starts the script assh test.sh
I am using fedora 16.
Below is a sample code of what i am trying to do. You can create a test.out executable with following content
#include <stdio.h>
#include <iostream>
int main ()
{
int i=1;
while (i)
{
printf ("%d ",i);
i++;
sleep (1);
}
return (0);
}
the contents of script file test.sh is as follows
#!/bin/sh
# Run using ./test.sh start
# Stop using sh test.sh stop or ./test.sh stop
start () {
while [ 1 ]
do
echo "Starting.."
./test.out
#./test2.out
sleep 3
done
}
stop () {
echo "Stopping run.sh script"
pkill -9 run.sh
#** The above line works only if user has run with ./test.sh start
# but not if the user has used sh test.sh start **
exit
}
case "$1" in
start)
start
fi
;;
stop)
stop
;;
*)
echo "Invalid argument"
esac