6

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 my_script.sh at 22.21.05 I would do:

echo "sleep 5; my_script.sh" | at 22.21

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

7

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.

3

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 my_script.sh

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

1

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 "yourscript.sh" | 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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.