[SOLVED] How to run a program for a specific amount of time starting at a specific time?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
Distribution: Arch Linux w/ Gnome, Ubuntu 9.10, Windows Vista
Posts: 24
Original Poster
Rep:
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...
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.
Distribution: Arch Linux w/ Gnome, Ubuntu 9.10, Windows Vista
Posts: 24
Original Poster
Rep:
Quote:
Originally Posted by pixellany
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...)
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.
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
Distribution: Arch Linux w/ Gnome, Ubuntu 9.10, Windows Vista
Posts: 24
Original Poster
Rep:
Quote:
Originally Posted by i92guboj
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
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.
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
Distribution: Arch Linux w/ Gnome, Ubuntu 9.10, Windows Vista
Posts: 24
Original Poster
Rep:
Quote:
Originally Posted by i92guboj
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.
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.
Distribution: Arch Linux w/ Gnome, Ubuntu 9.10, Windows Vista
Posts: 24
Original Poster
Rep:
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.