Xu Wang Xu Wang - 6 months ago 13
Linux Question

Test syntax of at command time argument

I would like to test an at time argument before running the at command. The use case is that I would like to run the at command on a different computer at a different time, so I want to first store the at argument, which means I want to check it when it is first entered.

Example is before:

$ atarg1="now+7mintes"
# I would like to check the above *before* running this
$ at $atarg1 <<< "echo hi there"
syntax error. Last token seen: t
Garbled time

Answer

You could always remove the job from the queue if it succeeds. This obviously has a race condition - in the unlikely event that the command executes from the queue between submitting and removing it, you will run a command needlessly. Running true when you didn't want to has very low impact, though.

if success=$(at "$atarg" <<<true); then
    sed 's/^job \([1-9][0-9]*\) at .*/\1/' <<<"$success" | xargs atrm
    exit 0
else
    exit $?
fi