LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to run a program for a specific amount of time starting at a specific time? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-run-a-program-for-a-specific-amount-of-time-starting-at-a-specific-time-808594/)

thiemster 05-18-2010 09:18 AM

How to run a program for a specific amount of time starting at a specific time?
 
I want to record an internet radio station starting at 2:00am tomorrow morning. The specific program on the radio station lasts until 6:00am.

The command I need to run to record the station is:
Code:

mplayer http://wjcu.jcu.edu:8001/listen.pls -ao pcm:file=indie_heat_of_the_night.wav -vc dummy -vo null
I'd use cron, but 1. I'm not sure how to and 2. it seems unnecessarily complicated for something that I only want to run once. If cron is the only/easiest solution, I guess I'll just have to resort to that, but I'd rather not.

Thanks in advance for any answers.

AlucardZero 05-18-2010 09:23 AM

well, "at" is good for starting oneoffs. Then you can "at" a "killall mplayer" or similar for 6AM.

thiemster 05-18-2010 09:27 AM

I was actually thinking of doing that but there's one problem: I tried to run the command using "at" last night, but it never ran. A few minutes ago, I tried running a simple "echo hello world" with "at". It was supposed to run it 5 minutes from when I set it, but it never did, but rather just removed "echo hello world" from the queue list at the specified time without running the command. Do you know what could be wrong? There weren't any error messages or any indication of what could have gone wrong...

pixellany 05-18-2010 09:33 AM

If you run a script from some kind of startup script, or a background function such as "at" or "cron", I think you would need to specify where its output would go.

In the case of your example, maybe just do this:

echo "hello world" > demofile

Then just check and see that demofile got created and has "hello world" in it.

thiemster 05-18-2010 09:36 AM

Quote:

Originally Posted by pixellany (Post 3972773)
If you run a script from some kind of startup script, or a background function such as "at" or "cron", I think you would need to specify where its output would go.

In the case of your example, maybe just do this:

echo "hello world" > demofile

Then just check and see that demofile got created and has "hello world" in it.

How would I be able to run the original command with this though? (as it does not need any output file, or at least I don't think it does...)

thiemster 05-18-2010 09:38 AM

Thanks pixellany. It worked. But how would I use this to run the "mplayer...." command?

i92guboj 05-18-2010 09:42 AM

Quote:

Originally Posted by thiemster (Post 3972770)
I was actually thinking of doing that but there's one problem: I tried to run the command using "at" last night, but it never ran. A few minutes ago, I tried running a simple "echo hello world" with "at". It was supposed to run it 5 minutes from when I set it, but it never did, but rather just removed "echo hello world" from the queue list at the specified time without running the command. Do you know what could be wrong? There weren't any error messages or any indication of what could have gone wrong...

The simple question first: is the atd daemon running at all? If it's not then at will not work.

The second thing now: commands programmed to be run using at are not attached to any particular terminal device, which means that using "echo" to test "at" will not bring you any good result at all.

As a test you could rather do this:

Code:

echo foo > /tmp/testfile.txt | at -m HH:MM
Just set "HH:MM" to the next minute to test.

i92guboj 05-18-2010 09:45 AM

About how to stop the thing, you could very well do what they've told you above, and use killall. If you need more control you could just fit all the pieces together in a single script, then launch it using at, then just forget about it. Something like this:

Code:

#!/bin/bash

mytask & # tune this
PID_mytask=$!
sleep 6h # sleeps 6 hours, tune this
kill -9 $PID_mytask


thiemster 05-18-2010 09:46 AM

Quote:

Originally Posted by i92guboj (Post 3972786)
The simple question first: is the atd daemon running at all? If it's not then at will not work.

Yes, the atd daemon is running.

Quote:

Originally Posted by i92guboj (Post 3972786)
The second thing now: commands programmed to be run using at are not attached to any particular terminal device, which means that using "echo" to test "at" will not bring you any good result at all.

As a test you could rather do this:

Code:

echo foo > /tmp/testfile.txt | at -m HH:MM
Just set "HH:MM" to the next minute to test.

that does work with echo, but how would I fit that to allow mplayer to run? I tried "mplayer ....blah...blah...blah > test.txt" but it did not successfully record. Instead, it immediately exited.

i92guboj 05-18-2010 09:52 AM

Quote:

Originally Posted by thiemster (Post 3972792)
Yes, the atd daemon is running.



that does work with echo, but how would I fit that to allow mplayer to run? I tried "mplayer ....blah...blah...blah > test.txt" but it did not successfully record. Instead, it immediately exited.

That's just a test, and it will not work with mplayer (at most, it would save the text output from mplayer to a plain text file which is not what you want).

The first thing you need to do is to make sure that your mplayer command works when ran directly from the command line.

Once you've done that, integrate that command in the script I wrote above, and run it using at -f

thiemster 05-18-2010 09:57 AM

Quote:

Originally Posted by i92guboj (Post 3972796)
That's just a test, and it will not work with mplayer (at most, it would save the text output from mplayer to a plain text file which is not what you want).

The first thing you need to do is to make sure that your mplayer command works when ran directly from the command line.

Once you've done that, integrate that command in the script I wrote above, and run it using at -f

Thanks. The mplayer command does work (I've used it before), so I'll try to fit it into the script and hopefully it'll work this time.

i92guboj 05-18-2010 10:03 AM

It should, just make sure you use '&' after the command, so it starts into the background and the $PID can be captured. Then just launch your script using at -f myscript.sh -m HH:MM. Remember that more than probably you won't see a thing on your screen, since as I said the processes ran this way are not attached to any particular console.

thiemster 05-18-2010 10:04 AM

Thank you very much everyone who offered solutions (and especially i92guboj). I tried the script with "sleep 3m" and it recorded the radio station for 3 minutes and 6 seconds and then stopped. The 6 seconds are unimportant, so I consider this a complete success!

If anyone is curious, my final script to record for 4 hours is:
Code:

#!/bin/bash

mplayer http://wjcu.jcu.edu:8001/listen.pls -ao pcm:file=indie_heat_of_the_night.wav -vc dummy -vo null &
PID_mytask=$!
sleep 4h
kill -9 $PID_mytask

and my "at" command is:
Code:

at -f record_radio.sh 2:00am tomorrow


All times are GMT -5. The time now is 07:35 PM.