LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-17-2008, 09:23 PM   #1
BillyGalbreath
Member
 
Registered: Nov 2005
Location: Houston Texas
Distribution: Debian Sid
Posts: 379

Rep: Reputation: 31
Run for N seconds then kill...


This is REALLY got me stumped..

I'm trying to run a simple bash command for N seconds then force it to kill/stop/halt.

The command is:

Code:
$ gpspipe -r > nmea.data
What this does is throw a BUNCH of NMEA strings from my GPS receiver into the nmea.data text file. The problem is I have to manually stop this command with ^C or it will literally run forever. I simply want this command to run for about 3 to 5 seconds then stop on its own.

How can I alter my command to make it do this? I'd prefer to have this as a 'one-liner', but if need be I can create a bash script.

Please help! Thanks!
 
Old 05-17-2008, 09:31 PM   #2
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
This should work:

Code:
$ gpspipe -r > nmea.data & sleep 3 ; kill $!
 
Old 05-17-2008, 09:41 PM   #3
BillyGalbreath
Member
 
Registered: Nov 2005
Location: Houston Texas
Distribution: Debian Sid
Posts: 379

Original Poster
Rep: Reputation: 31
Sweet beans! Works brilliantly!

Thanks a million, man!
 
Old 05-18-2008, 12:28 AM   #4
BillyGalbreath
Member
 
Registered: Nov 2005
Location: Houston Texas
Distribution: Debian Sid
Posts: 379

Original Poster
Rep: Reputation: 31
Ok - additional request:

Is there any way to daemonize this? I've tried adding another '&' to the very end (after $!) but this didn't do it.

The reason I ask is I'm making a script that uses this command, and the script 'hangs' for the duration of the command. I'd like to be able to run the command and have my script continue on without waiting for the command to finish.

Thanks!
 
Old 05-18-2008, 02:07 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by BillyGalbreath View Post
Is there any way to daemonize this? I've tried adding another '&' to the very end (after $!) but this didn't do it.
Strange, it works for me. Did you get any error? Anyway, you can take into account the option -n for gpspipe:
Quote:
-n [count] causes [count] sentences to be output. gpspipe will then exit gracefully.
guess how many sentences are sent in 3 seconds and specify the count parameter accordingly.
 
Old 05-18-2008, 02:30 AM   #6
BillyGalbreath
Member
 
Registered: Nov 2005
Location: Houston Texas
Distribution: Debian Sid
Posts: 379

Original Poster
Rep: Reputation: 31
You're a genius, colucix! The -n option did the trick! Too bad I didn't know about it earlier..

Anyways, this is the command I'm using now which works perfectly (ven daemonizes):

Code:
gpspipe -r -n 15 > nmea.data &
15 seems to last about 3 seconds.

 
Old 05-18-2008, 12:21 PM   #7
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
This would work too:

Code:
cat /dev/random >/dev/null & echo $! | (sleep 3 ; xargs kill) &
 
  


Reply



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
How to re-run a command every x seconds automatically? Akhran Linux - Newbie 4 08-22-2006 10:58 PM
how to run crontab job every 30 seconds houssamfarag Slackware 5 06-23-2006 02:45 AM
how to run crontab job every 20 seconds houssamfarag Mandriva 4 06-23-2006 02:45 AM
Howto run a program for 15 seconds and let it quit itself. bglnelissen Programming 3 03-16-2005 09:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 PM.

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
Open Source Consulting | Domain Registration