How can I use $HOME or ~/ in my log paths of launchd plist to run as LaunchAgent

My plist has below keys

<key>StandardOutPath</key>

<string>$HOME/launchd.stdout.log</string>

<key>StandardErrorPath</key>

<string>SHOME/launchd.stderr.log</string>


But it doesnt identify $HOME, i want to load this plist in different user accounts, i cant hardcode my user name

Replies

I think the user home directory is the default working directory for launch agents. Try it and see.

The fact that launchd agent property lists have no good way to specify home-relative paths is a well-known limitation. There are two ways you can deal with this:

  • Not print to

    stdout
    and
    stderr
    — Daemons and agents are meant to be headless. We recommend that such programs log to the system logging facility, which is currently <os/log.h>.
  • Redirect

    stdout
    and
    stderr
    yourself — If you absolutely have to print to
    stdout
    and
    stderr
    , you can redirect them to a destination of your choice using
    dup2
    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"