MacOS launchd WorkingDirectory is set to current directory when scheduling
I was looking at moving the launchd logfiles out of my $HOME directory on MacoOS.
These are set in the launchd plist file as:
<key>StandardErrorPath</key>
<string>local.resticprofile.default.backup.log</string>
<key>StandardOutPath</key>
<string>local.resticprofile.default.backup.log</string>
<key>WorkingDirectory</key>
<string>/Users/MYUSER</string>
I tried setting base-dir in the config but that didn't do anything:
# {{ $logdir := print .Env.HOME "/Library/Logs/resticprofile" }}
version: "1"
schedule-defaults:
ignore-on-battery: true
prevent-sleep: true
permission: user_logged_on
default:
initialize: false
base-dir: "{{ $logdir }}"
The only workaround I found was manually switching to the log directory before running resticprofile schedule.
I would've assumed that base-dir is also used for the WorkingDirectory in the launchd daemon. If it isn't by design, what is the correct way to move the default launchd logfiles to another location?
Hey!
Sorry for the late answer.
I realise this is not explained in the documentation (note to self: add it to the million items list of things to fix in the documentation).
The working directory is the current directory you were in at the time you scheduled the profile. The reason is that the arguments you're specifying on the command line will be relative to this current directory. Things like configuration file or other parameters added on the command line.
base-dir is a bit misleading and I'll need to fix the ambiguity at some point 😵💫
As a workaround I use an alias now that changes to the log dir before calling resticprofile.
alias resticprofile='_cwd "$RESTICPROFILE_LOGS" resticprofile'
Maybe it would be better to have the ability to specify a path to the log files then in the launchd config instead of just the filenames? But either way, it's working for me now.
You can use the schedule-log flag, with a full path to a log file.
Annoyingly, launchd will still create these default log files even though nothing will ever be written in them 🤷🏻