Scheduling command/script by specifying the exact second

I’m using bash and wondering how I can execute a command/script at a later specified time, with the accuracy of a second?

I read the man page for the at-command, but as far as I could understand it’s only possible specify minutes (and not seconds). Right now I first use at and then sleep to get it to execute the right second. To demonstrate, if I would like to run at 22.21.05 I would do:

echo "sleep 5;" | at 22.21

But it would be much nicer to have a command with it built in. Something like at 22.21.05.

I don’t believe that this is possible. cron is only granular down to the minute and the at utility hooks into it to do it’s work. I think you have the right solution–sleeping for x seconds before executing.

Answered By: gvkv

You could make your technique into a function:

atplus () { local sec=${1##*.} time=${1%.*}; shift; echo "sleep $sec; $@" | at $time; }

Try it out:

atplus 22.21.05

You should be aware, however, that precision is not guaranteed.

Answered By: Dennis Williamson

Actually at may support seconds

I think this is not always well supported (depending on the particular implementation), but in principle at may support seconds.

From the man page:

-t time run the job at time, given in the format [[CC]YY]MMDDhhmm[.ss]

Look at the [.ss] at the end.

This means that, using the -t option, you can type something like

echo "" | sudo at -t 04281549.45

i.e. the command should be executed at 15:49:45 (45 seconds).
(on April the 28th)

When I tried it, as expected the output from at was:

job 38 at Wed Apr 28 15:49:45 2021

Note: when I tried it, it was still late by some seconds, but it still was executed not before 15:49:45, meaning that the specified seconds are not ignored by at internally, they are actually considered. Therefore, specifying seconds can still be useful when you want to say something like "delay this command to run it not earlier than 15:49:45".

Answered By: Kubuntuer82
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.