How to use Shell script to kill an on going program?
Hi, I'm still newbie in linux and i'm trying to run a shell script to listen to a GPS receiver port.
So in my script, I have
cat /dev/ttyS2 > outputFile.txt
which will write the all the data to outputFile.txt.
This is like executing a while(1) loop,it will always listen and write to outputFile forever.
However, i want to stop this after it runs for 1 or 2 minute.
manually, i can use ctrl-c to stop it,
but now i have to make it automatic stopped.
So i was thinking to call another script (script2) to get the PID of this program and then use "kill $PID" to stop it.
but the problem is, once i execute this $cat /dev/ttyS2, the CPU will just stuck there.
it wont release the CPU to run the other script.
and I cant run script2 first because when this cat command have not been started, there is no PID for me to get.
Is there anyone knows how to solve this problem ? i try to test and simulate this using this script:
echo "_______________Shell_1 file testing_____________"
gcc -o whileLoop c_test.c
echo "starting while loop:"
this is the code for the c_test.c:
//this loop act as a port listener.
printf("i : %d \n", i);
so what happen now is: I want to stop this while loop after it runs for 1 or 2 minute.
i know i can use for loop or a counter to solve this simple while loop, but that is not what i'm currently working at.
my program is listening to an active port.
is there anyone can advice me on how to solve this problem? thanks.