15

I'd expected this to be easy to find on Google, but wasn't able to discover anything.

I know that the working directory of a launchd agent/demon can be changed via the workingdirectory key. However, this key is optional. If no working directory is specified, what does macOS use?

Wowfunhappy
  • 7,383

1 Answers1

23

TL;DR

The default value is /, as in

<key>WorkingDirectory</key>
<string>/</string>

Explanation

The WorkingDirectory in a launchd .plist is an optional key used to specify a directory to chdir(2) to before running the job. If this key is not present, then the root / directory is used.

I verified this by running a shell script as both a Launch Agent and a Launch Daemon that, when run, appended a directory listing to a text file. In all cases, without the WorkingDirectory key, the output was the root / directory of the startup disk.

Depending on whether or not it was run as root or the current user the output as root also contained the hidden directories even though that was not explicitly set to output by the script.

As a side note, the PATH passed to the shell script when run as both a Launch Agent and Launch Daemon was:

/usr/bin:/bin:/usr/sbin:/sbin

Note that this was tested under OS X 10.8.5.

coolaj86
  • 284
user3439894
  • 58,676
  • Note for anyone stumbling across this answer in 2019: / is now read-only on macOS Catalina, so it may be worth setting WorkingDirectory to something like /tmp now, depending on what you need. – jjj Sep 06 '19 at 13:19
  • Shouldn't it be the $HOME of the $USER running the job? – dardo82 Jul 05 '20 at 01:40
  • @ardo82, RE: "Shouldn't it be the $HOME of the $USER running the job?" -- Shouldn't what be? The default is / and if you want to change it, then set the WorkingDirectory key to what you want it to be. Also, you have to use the actual path, not variables. – user3439894 Jul 05 '20 at 02:39