5

I'm looking for some Linux command which may launch another command at a specific time.

I know about the at command, but it gives me only minutes precision, and I need seconds precision. Is there an option with at command that I'm not aware of? Or is there any other command I should use?

Any orientation will be useful.

5
  • 2
    Why? If a scheduler checked every second then it would eat up tons of processor. Why isn't minute precision good enough?
    – beatgammit
    Jun 25, 2011 at 5:03
  • You'll have to suffer with a little lack of precision - my results to time sleep 5 give me an extra .003 seconds. Try that command on your machine to see how accurate it will be.
    – new123456
    Jun 25, 2011 at 16:52
  • @tjameson , because I need to schedule something for 5 seconds or so, and the at command doesn't works for me. I wasn't going for the sleep command, but I guess I'll have to do with it. Thanks Jun 25, 2011 at 22:21
  • @beatgammit There are plenty of applications where second-level precision may be relevant. See real-time computing for some examples.
    – gerrit
    Jul 5, 2022 at 16:13
  • Related: stackoverflow.com/q/9619362/974555
    – gerrit
    Jul 6, 2022 at 16:00

2 Answers 2

7

Use cron to run a script that calls the sleep command for the sub-miniute precision bit of it? So

sleep 10 ; foo.sh 

should run foo.sh 10 seconds after the command is called.

5
  • Do you know anything about its precision? Or the precision of at in executing something at exactly xx:xx:00:000? If the user requires this kind of precision, it's conceivable that at and sleep won't cut it.
    – Daniel Beck
    Jun 25, 2011 at 9:36
  • sleep on ubuntu and as such debian accepts floating point values. "Unlike most implemenatations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number." However, if he needs smaller than whatever precision float supports... i have no clue
    – Journeyman Geek
    Jun 25, 2011 at 9:41
  • I meant more along the lines of whether his script actually will start exactly at :10 when he specifies sleep 10, or maybe more like :11 or :12...
    – Daniel Beck
    Jun 25, 2011 at 10:20
  • no idea, actually. i've mostly seen wait used as a 'lets give things a while to settle down' way, rather than as a precision timing instrument.
    – Journeyman Geek
    Jun 25, 2011 at 10:24
  • 2
    Bog standard linux is not a real time system and offers no absolute timing guarantees. Get it heavily loaded and thrashing and things can become very delayed, indeed. // too much experience with systems that have nearly enough memory Jun 25, 2011 at 13:27
0

You can schedule your job at the nearest minute, then use alarm to get second-level precision. For an example in Python:

from signal import signal, setitimer, ITIMER_REAL, SIGALRM, alarm
from datetime import datetime, time
from sys import argv, exit
from time import sleep

at = datetime.combine(datetime.now(), time.fromisoformat(argv[1]))

class Done(Exception):
    pass

def _handler(signum, frame):
    print("The time is", datetime.now())
    raise Done()

setitimer(ITIMER_REAL, (at - datetime.now()).total_seconds())
signal(SIGALRM, _handler)

try:
    while True:
        sleep(1)
except Done:
    pass

Running:

$ python at-second.py 13:36:28
The time is 2022-07-06 13:36:28.000238

A limitation for this method:

only one alarm can be scheduled at any time

So if you need multiple events in a minute, you'd need to schedule the second one only after the first one is finished.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .