LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Run for N seconds then kill... (https://www.linuxquestions.org/questions/programming-9/run-for-n-seconds-then-kill-642955/)

BillyGalbreath 05-17-2008 09:23 PM

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!

sydney-troz 05-17-2008 09:31 PM

This should work:

Code:

$ gpspipe -r > nmea.data & sleep 3 ; kill $!

BillyGalbreath 05-17-2008 09:41 PM

Sweet beans! Works brilliantly!

Thanks a million, man! :D

BillyGalbreath 05-18-2008 12:28 AM

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!

colucix 05-18-2008 02:07 AM

Quote:

Originally Posted by BillyGalbreath (Post 3156696)
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.

BillyGalbreath 05-18-2008 02:30 AM

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.

:D

sydney-troz 05-18-2008 12:21 PM

This would work too:

Code:

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


All times are GMT -5. The time now is 11:53 AM.