15

I am working on a CentOS server and schedule a task with command at

# echo "touch a_long_file_name_file.txt" | at now + 1 minute
job 2 at Wed Oct 31 13:52:00 2018

One minute later,

# ls | grep a_long_file_name_file.tx
a_long_file_name_file.txt

the file was successful created.

However, if I run it locally on my macOS,

$ echo "touch a_long_file_name_file.txt" | at now + 1 minute
job 31 at Wed Oct 31 13:58:00 2018

Minutes later, if it failed to make such a file.

I checked the version of at on the CentOS server

AUTHOR:  
At was mostly written by Thomas Koenig, [email protected].  
2009-11-14                                             

In contrast, the macOS version

AUTHORS
     At was mostly written by Thomas Koenig <[email protected]>.  The time parsing routines are
     by
     David Parsons <[email protected]>, with minor enhancements by
     Joe Halpin <[email protected]>.

BSD                            January 13, 2002                            

I found that at, atq, atrm are not of GNU coreutils.

$ ls /usr/local/opt/coreutils/libexec/gnubin/ | grep at
cat
date
pathchk
realpath
stat
truncate

How could I install the latest version of at on macOS and make it work?

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
AbstProcDo
  • 2,453
  • 4
  • 23
  • 55
  • 1
    Possible duplicate of [Install the latest version of GNU at?](https://unix.stackexchange.com/questions/478633/install-the-latest-version-of-gnu-at) – muru Oct 31 '18 at 07:53
  • 1
    Possible duplicate of [How do I use \`at\` to schedule a script to execute on macOS?](https://unix.stackexchange.com/questions/407181/how-do-i-use-at-to-schedule-a-script-to-execute-on-macos) – Toby Speight Oct 31 '18 at 13:42
  • `at`, `cron`, and `periodic` are "deprecated": https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html – Christopher Aug 27 '19 at 13:53
  • @Christopher: that Apple page indeed says that `at` is "deprectaed", but does not explain how to run a job once "in x minutes". – mivk Feb 28 '20 at 10:16
  • @mivk Use a LaunchAgent or LaunchDaemon. – Christopher Feb 29 '20 at 11:54

2 Answers2

38

Instead of updating at and the associated tools on macOS, lets try to make the default at on macOS work.

The at manual on macOS says (my emphasis):

IMPLEMENTATION NOTES

Note that at is implemented through the launchd(8) daemon periodically invoking atrun(8), which is disabled by default. See atrun(8) for information about enabling atrun.

Checking the atrun manual:

DESCRIPTION

The atrun utility runs commands queued by at(1). It is invoked periodically by launchd(8) as specified in the com.apple.atrun.plist property list. By default the property list contains the Disabled key set to true, so atrun is never invoked.

Execute the following command as root to enable atrun:

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

What I think may be happening here, and what is prompting your other at-related questions, is that you just haven't enabled atrun on your macOS installation.

On macOS Mojave, in addition to running the above launchctl command (with sudo), you will also have to add /usr/libexec/atrun to the list of commands/applications that have "Full Disk Access" in the "Security & Privacy" preferences on the system. Note that I don't know the security implications of doing this. Personally, I have also added /usr/sbin/cron there to get cron jobs to work (not shown in the screenshot below as this is from another computer).

enter image description here

To add a command from the /usr path (which won't show up in the file selection dialog on macOS), press Cmd+Shift+G when the file selection dialog is open (after pressing the plus-icon/button in the bottom of the window).

You do not need to reboot the machine after these changes. I have tested this on macOS Mojave 14.10.1.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • Seems to work in 11.0 / Big Sur also, should others be looking in the future. – flip Jan 05 '21 at 19:34
  • Excellent instructions. Worked for me on 10.15.7 / Catalina. The "Full Disk Access" was the gotcha for me. – James May 04 '21 at 11:58
  • That security setting is a real gotcha! Especially since older answers to the same question do not include it (as, presumably, it was not needed on older OSXes). – corwin.amber Jun 19 '21 at 15:03
  • After doing all this, I had to output the result of my command to a file to see the results, in that file. I couldn't see the results in the standard output for some reasons. – Akhil Raj May 14 '22 at 20:54
  • 1
    In Big Sur you will find it under `/usr/libexec/atrun` – DimiDak Aug 23 '22 at 13:53
  • @AkhilRaj: This is expected. From the at(1) man page: "The user will be mailed standard error and standard output ..." – nishanthshanmugham Feb 01 '23 at 04:14
2

To add to Kusalananda correct answer, Adding terminal application to the list of applications that have "Full disk access" will cause the standard output of at jobs to be mailed to the user.

I'm sorry I couldn't comment on his answer. I registered to the site just to add the information above and I don't have permission to comment

mosh
  • 21
  • 1