brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: at runs a job once, where cron would repeat it

Cron
~/tips/tip-cron-at-one-off-job

echo 'systemctl restart myapp' | at 03:00 tomorrow

Schedules a single command for later without adding a cron line you have to remember to delete.

A cron line for something that should happen only once, like a restart tonight at 3, is easy to forget. Then it happens again next month. at is made for one-off jobs.

echo 'systemctl restart myapp' | at 03:00 tomorrow

at reads the commands from standard input, or from a file with -f, and runs them once at the time you give. The time format is friendly: at now + 2 hours, at 16:30, at 1am tomorrow and at 4pm + 3 days all work.

To see and manage what’s waiting:

atq        # list pending jobs with their number
at -c 7    # show exactly what job 7 will run
atrm 7     # cancel job 7

One nice difference with cron: at saves your current working directory and most of your environment when you create the job, and restores them when it runs. So a command that works in your shell now is much more likely to work later than in a cron line.

at isn’t always installed, and on Debian and Ubuntu you need the at package and its atd service. Output is mailed to you, like with cron, so without a mail setup it disappears; redirect it to a file. I’d also check atq after a reboot, to see that the job is still waiting.

Further reading: at(1) manual page

Going deeper: Guide: find out why a Linux service failed, with journalctl

More Cron tips 55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /usr/local/bin/month-end.sh EDITOR=nano crontab -e 30 6 * * mon /usr/local/bin/weekly-report.sh @daily /usr/local/bin/backup.sh sudo ls -l /var/spool/cron/crontabs All Cron tips (32) All topics

Link