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
-
5Any reason why cron won't work? – lreeder Sep 22 '13 at 15:49
-
20Why 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
-
2Tbh 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
4 Answers
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!
-
2get an error ```Can't open /var/run/atd.pid to signal atd. No atd running?``` any clue? – HappyCoding Mar 22 '17 at 15:01
-
2You 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
-
10In 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
-
1To 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
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.
- 164
- 2
-
10An `at` job will remain scheduled even if the machine is rebooted in the meantime. – tripleee Jan 10 '15 at 11:38
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
- 2,226
- 2
- 23
- 35
-
2
-
1Crontab Guru (https://crontab.guru) is a helpful tool for crontab configuration. – Umesh .A Bhat Mar 21 '19 at 18:46