Contact Form

Name

Email *

Message *

OS X - Schedule tasks with the “at" command

Like my UNIX/Linux professor used to say, we should have our computer work for you, so I decided to find new ways to automate tedious tasks by using my Mac.

Sounds cool, right?

Problem 

Sadly, the default solution provided by OS X for creating scheduled tasks (LaunchD) isn't very user-friendly (here for more details).

There are third-party tools around, mostly paid, providing an easier GUI approach to this functionality (e.g. LaunchD Task Scheduler).

However, a task can be also created by leveraging OS X system resources.


Solution

OS X includes a command called at, normally disabled "due to power management concerns" (here for more details).

at allows to schedule tasks for a later time in a user-friendlier way than launchd.

It can be enabled by launching the following command: 

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist

As a simple example of the possibilities offered by this tool, I created a small Python script allowing the computer to say a sentence.


I wanted to take a nap for a while, so I decided to use the at command for me to setup an alarm, which would run the above script after 30 minutes:


The above command is broken down as follows:
  1. at 7:45 pm today: Defines the date and time when to run the application, or the file, specified in the second part. After entering this part of the command, press down ENTER.
  2. python /Users/mattia/alarm.py: Specifies what file/application(s) to run at the date and time indicated above. In this case, my Mac has to run our small Python sample script at 7:45 PM. Press ENTER, after inserting this portion of the command.
  3. Press CTRL/D. If the previous two steps were correctly performed, you'll notice a job will be created and scheduled to run by the time indicated in the first part of the command (in this case, you can see "job 4 at Sat Jul 23 19:45:00 2016", in the last line shown in the above figure).
Check the embedded video for what happened at 7:45 PM, when the task was scheduled to run.

Wrap-up

Of course, this is a very simple example, but possibilities are endless.

Using at command can greatly help you be more productive with a Mac system.

The command syntax is much more user-friendly than launchd, as you can schedule a task by using plain English

NOTE: If you want to re-disable at command, you simply need to launch the following command: 

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.atrun.plist

I ended up creating two scripts for launching these commands: at_enable.bash to enable at (which uses the "load" version of the command) and at_disable.bash (which runs the "unload" version of the command) to disable it when I don't need it. I'm lazy and I hate useless typing.

Comments

Related Posts Plugin for WordPress, Blogger...