Command Line Scheduling – Simple Way to Schedule a Single Future Event

command linescheduling

There are often times that I want my computer to do a single task, but not right now. For example, I could have it notify me in 30 minutes that it is time to leave work. Or maybe I want it to run a complicated test 2 hours from now when I'm sure most everyone else will be gone from the office.

I know I could create a cron job to run at a specific time of day, but that seems like a lot of work when all I want is something simple like "Run this script in 10 minutes", besides I'd have to figure out what time it will actually be X minutes/hours/days from now, and then delete the cron job once it finished.

Of course I could just write this script and run it in the background:

sleep X
do_task

But that just seems so clunky: I either need a new script for each task, or I need to write and maintain a script generic enough to do what I want, not to mention I have to figure out how many seconds are in the minutes, hours, or days I want.

Is there not an already established solution to this problem?

Best Answer

I use a simple script with at:

#!/bin/bash
# email reminder notes using at(1)...

read -p "Time of message? [HH:MM] " time
read -p "Date of message? [dd.mm.yy] " date
read -p "Message body? " message

at "$time" "$date" <<EOF
echo "$message" | mailx -s "REMINDER" me@gmail.com
EOF

You could just as easily pipe the $message to notify-send or dzen if you wanted a desktop notification instead of an email.

Related Question