LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-03-2009, 12:28 PM   #1
cruiz_bugs
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
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:

#!/bin/bash

echo "_______________Shell_1 file testing_____________"
gcc -o whileLoop c_test.c
echo "starting while loop:"
./whileLoop


this is the code for the c_test.c:
#include <stdio.h>

int main(){
int i=0;

while(1){
//this loop act as a port listener.
printf("i : %d \n", i);
sleep(1);
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.
 
Old 08-03-2009, 12:48 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Some tips, based on the Bash man page:

1) putting an & symbol after your cat command will cause it to fork into the background, returning control to the shell.

2) After doing that, the bash builtin variable $! will contain the PID of the last process (the cat process) which you can use for whatever means you decide to use to kill the cat process after your desired delay.

Cheers,

Sasha


PS - Welcome to LQ!
 
Old 08-04-2009, 02:38 AM   #3
cruiz_bugs
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
hi, thanks for the reply

anyway, i still dont quite get what you mean.

how to call the built in variable here?
is there any sample code that i can refers to?

thanks.
Regards,
christine
 
Old 08-04-2009, 03:28 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Hello Christine

I understand you want to create a bash script that will run an executable for a set time. Please correct me if I am wrong.

You want something like this (not tested), based on GrapefruiTgirl's suggestion
Code:
#!/bin/bash
./whileLoop &
sleep 120
kill -9 $!
It's easier to read your code when you put it in [ code ]<stuff>[ /code ] tags (without the spaces).

Best

Charles
 
Old 08-04-2009, 04:30 AM   #5
cruiz_bugs
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Ok.
that works.
thank you so much ^___^

regards,
Christine.
 
Old 08-04-2009, 09:41 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply

Tags
script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to automatically kill user on a server vathsan AIX 3 01-06-2009 04:22 AM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 12:29 PM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 10:09 PM
shell script to kill all processes on specified port varunbihani Linux - General 1 04-19-2005 06:39 AM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 03:09 PM


All times are GMT -5. The time now is 03:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration