How to run a script at a certain time on Linux?

187,196

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

Share:
187,196
Aaron Ullal
Author by

Aaron Ullal

Updated on June 20, 2020

Comments

  • Aaron Ullal
    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
    Aaron Ullal over 10 years
    so how would you trigger that command?
  • glenn jackman
    glenn jackman over 10 years
    with bash, you'd write $(< file)
  • tripleee
    tripleee over 9 years
    An at job will remain scheduled even if the machine is rebooted in the meantime.
  • HappyCoding
    HappyCoding about 7 years
    get an error Can't open /var/run/atd.pid to signal atd. No atd running? any clue?
  • Manoel Vilela
    Manoel Vilela almost 7 years
    You need the atd daemon running to use at. On Manjaro OpenRC, you can just install at-openrc and add the daemon atd service with: sudo rc-update add atd and start with sudo rc-service atd start. Usually the at package had already included a systemd (the default init/service system on various distro linux including Ubuntu) service which it can be started with sudo systemctl start atd and enable autostart on init with sudo systemctl enable atd.
  • Jewenile
    Jewenile almost 7 years
    In my case this prints out "job 6 at 2017-08-21 10:53" immediately...
  • Tom Russell
    Tom Russell almost 6 years
    BTW, 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
    Umesh .A Bhat about 5 years
    Crontab Guru (crontab.guru) is a helpful tool for crontab configuration.
  • Shrijit Basak
    Shrijit Basak almost 4 years
    Will this script run later if the system was powered off at that time?
  • Cedric
    Cedric over 3 years
    Note 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
    Prof.Chaos almost 3 years
    To 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?