100

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

codeforester
  • 39,467
  • 16
  • 112
  • 140
Aaron Ullal
  • 4,855
  • 8
  • 35
  • 63
  • 6
    Any reason why cron won't work? – lreeder Sep 22 '13 at 15:49
  • 23
    Why is this topic closed?? It's a very reasonable question. Google search lead you to this as #1 And the answers are pretty nerdisch. So someone who can answer this is clear English will be blocked – Richard de Ree Aug 25 '17 at 11:52
  • 3
    @Richard I suspect the reason StackOverflow is closing so many questions like this is to encourage users to put questions that are off-topic here on the appropriate StackExchange site. There are quite a few new SE sites that previously didn't exist. For example, there are quite a few [very useful Vim questions](https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118) on SO that have been closed as off topic, and now that there's a Vi/Vim SE, those questions would be on topic there. Just musing. However, this still does seem like a programming question – mgarey Oct 19 '17 at 20:59
  • 2
    Tbh I forget how to use cron every time after I learn it, and I don't need anything advanced. – sudo May 11 '18 at 20:04
  • Agreed. Very useful topic. Anyone knows an alternative for 'at' ? I want to use it on a RHEL server and I don't want to create a cron job. Just for future reference, cause I need it now, so I guess I will just use the good old 'sleep' 'At' is convenient because you don't have to do the calculation yourself (which has led to miscalculations in the past.) – oneindelijk Sep 28 '22 at 15:48

4 Answers4

174

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

starball
  • 20,030
  • 7
  • 43
  • 238
Antoni
  • 2,071
  • 2
  • 24
  • 31
  • 2
    get an error ```Can't open /var/run/atd.pid to signal atd. No atd running?``` any clue? – HappyCoding Mar 22 '17 at 15:01
  • 3
    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`. – Manoel Vilela Aug 12 '17 at 10:13
  • 11
    In my case this prints out "job 6 at 2017-08-21 10:53" immediately... – Jewenile Aug 21 '17 at 08:53
  • 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? – Tom Russell Jun 01 '18 at 06:33
  • Will this script run later if the system was powered off at that time? – Shrijit Basak Jun 20 '20 at 07:38
  • 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`. – Cedric Sep 23 '20 at 10:51
  • 1
    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? – Prof.Chaos Aug 27 '21 at 02:45
  • -bash: at: command not found – Marcel Kopera Feb 17 '23 at 06:02
  • What works and what doesn't depends on your platform and local configuration. Out of the box, honest to `$dmr` U*x will have an `at` command installed and the supporting `atd` daemon running. Some Linux distros etc deviate from this for various reasons; then you'll need to figure out what package names and configuration changes are required for your particular distro. – tripleee Apr 28 '23 at 14:27
  • @Prof.Chaos How exactly do you establish that the commands don't run? This sounds to me like they ran silently, and you didn't know how to find the results. `at` obviously doesn't have access to your terminal (you could be logged out when it runs, or you could have closed the terminal where you ran it, but opened twenty others; it doesn't even try to solve this conundrum.) It will try to send any output by email, but if you haven't configured mail on your system, it obviously can't. Maybe look for a file named `dead.letter` in your home directory, or examine system logs to see what happened. – tripleee Apr 28 '23 at 14:32
22

The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).

at $(cat file) </path/to/script
tripleee
  • 175,061
  • 34
  • 275
  • 318
12

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.

Anony
  • 164
  • 2
8

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

julumme
  • 2,326
  • 2
  • 25
  • 38