How to run a script at a certain time on Linux?
Solution 1
Look at the following:
echo "ls -l" | at 07:00
This code line 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. You can read the specifications here:
http://manpages.ubuntu.com/manpages/precise/en/man1/at.1posix.html http://manpages.ubuntu.com/manpages/xenial/man1/at.1posix.html
Hope it helps!
Solution 2
The at
command exists specifically for this purpose (unlike cron
which is intended for scheduling recurring tasks).
at $(cat file) </path/to/script
Solution 3
Cron is good for something that will run periodically, like every Saturday at 4am. There's also anacron, which works around power shutdowns, sleeps, and whatnot. As well as at.
But for a one-off solution, that doesn't require root or anything, you can just use date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.
Solution 4
Usually in Linux you use crontab
for this kind of scduled tasks. But you have to specify the time when you "setup the timer" - so if you want it to be configurable in the file itself, you will have to create some mechanism to do that.
But in general, you would use for example:
30 1 * * 5 /path/to/script/script.sh
Would execute the script every Friday at 1:30 (AM) Here:
30
is minutes
1
is hour
next 2 *'s are day of month
and month
(in that order) and 5
is weekday
Aaron Ullal
Updated on June 20, 2020Comments
-
Aaron Ullal almost 4 years
I have a text file containing a specific date and time. I want to be able to run a script at the time specified in that file. How would you achieve that? Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file? Is there another way? The machine is a linux server , Debian wheezy. Thanks in advance
-
Aaron Ullal over 10 yearsso how would you trigger that command?
-
glenn jackman over 10 yearswith bash, you'd write
$(< file)
-
tripleee over 9 yearsAn
at
job will remain scheduled even if the machine is rebooted in the meantime. -
HappyCoding about 7 yearsget an error
Can't open /var/run/atd.pid to signal atd. No atd running?
any clue? -
Manoel Vilela almost 7 yearsYou need the
atd
daemon running to useat
. On Manjaro OpenRC, you can just installat-openrc
and add the daemon atd service with:sudo rc-update add atd
and start withsudo rc-service atd start
. Usually theat
package had already included asystemd
(the default init/service system on various distro linux including Ubuntu) service which it can be started withsudo systemctl start atd
and enable autostart on init withsudo systemctl enable atd
. -
Jewenile almost 7 yearsIn my case this prints out "job 6 at 2017-08-21 10:53" immediately...
-
Tom Russell almost 6 yearsBTW, my bash documentation describes a
at -c
usage. What's it for, if the way to execute a command is to pipe it into std in? -
Umesh .A Bhat about 5 yearsCrontab Guru (crontab.guru) is a helpful tool for crontab configuration.
-
Shrijit Basak almost 4 yearsWill this script run later if the system was powered off at that time?
-
Cedric over 3 yearsNote that this might require to install at first. Needed it on a raspi running raspbian which was as simple as
sudo apt install at
. -
Prof.Chaos almost 3 yearsTo me this was not helpful. At just claims that the respective command will be executed -- but it actually never does so, not matter what command I use (e.g. the invocation of another program), it just claims to do it, but nothing ever happens. Also following Manoel's advice (which actually sounds so elementary that it belongs into the solution itself) did not help. My ubuntu (21.04) claims that "at-openrc" doesn't exist and thus can't be installed. Can't this response be improved to get an actual running/working example?