Linux How to run a script at a specific time?

I have a text file containing a specific date and time. I want to be able to run the script at the time specified in this file. How would you achieve this? Create another script that runs in the background (look like a deamon) and checks every second if the current time matches the time in the file? Is there another way? The machine is a Linux server, Debian wheezy. thanks in advance

+49
linux scripting bash debian
Sep 22 '13 at 15:47
source share
4 answers

Take a look at the following:

echo "ls -l" | at 07:00 

This line of code executes "ls -l" at a specific time. This is an example of executing something (a command in my example) at a specific time. "at" is the command you were really looking for. Here you can read the specifications:

http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html

Hope this helps!

+98
Sep 22 '13 at 15:58
source share

The at command exists specifically for this purpose (unlike cron , which is designed to schedule repetitive tasks).

 at $(cat file) </path/to/script 
+10
Sep 22 '13 at 15:56
source share

Cron is good for running periodically, like every Saturday at 4 a.m. There's also an anacron that works around power outages, sleep, and much more. Just like in .

But for a one-time solution that doesn't need root or anything else, you can just use date to calculate the second-second of the target time as well as the current time, then use expr to find the difference, and sleep for many seconds .

+5
Sep 22 '13 at 16:02
source share

Typically on Linux you use crontab for such scduled tasks. But you must specify the time when you "set the timer" - therefore, if you want it to be configured in the file itself, you need to create some kind of mechanism for this.

But in general, you would use, for example:

 30 1 * * 5 /path/to/script/script.sh 

Executed a script every Friday at 1:30 (AM) Here:

30 minutes

1 - hour

next 2 * are day of month and month (in that order), and 5 is a weekday

+4
Sep 22 '13 at 15:52
source share



All Articles