6

I'm looking for a Linux command to 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 at option I'm not aware of? Or another command?

Pat Myron
  • 129

2 Answers2

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.

slhck
  • 235,242
Journeyman Geek
  • 133,878
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.

gerrit
  • 1,648
  • 7
  • 21
  • 38