3

My plist has below keys:

<key>StandardOutPath</key>
<string>$HOME/launchd.stdout.log</string>
<key>StandardErrorPath</key>
<string>$HOME/launchd.stderr.log</string>

But it doesn't identify $HOME. What I want is to load this plist in different user accounts as I cant hardcode my user name.

How can I achieve this ultimate aim?

3
  • Please read the answers to the two questions linked on top of your question, and enhance your plist according to the recommendations there. If it still doesn’t work, please update your question to show the revised plist.
    – nohillside
    Aug 1 '19 at 19:35
  • I want to add log path not program path.That is not answering my question . I cant follow that how will I get $HOME to give in log path Aug 1 '19 at 20:13
  • I don't think you can, this is a limitation of launchd.
    – TJ Luoma
    Aug 1 '19 at 21:50
5

You cannot use $HOME in these keys. You'll need to use the work-around shown in this answer:

how can I use $HOME, ~ or environment variable in plist file of LaunchDaemons

And then use ordinary shell scripting to redirect standard out and standard error to locations within $HOME. I.e. use a command similar to:

command >$HOME/launchd.stdout.log 2>$HOME/launchd.stderr.log
0
4

You can't use $HOME in the plist but you can configure it easily enough.

Once the plist is loaded on the proper computer (lets assume it is at ~/Library/LaunchAgents/local.testing.plist for the sake of discussion)

All you need to do is enter two commands at the command line

defaults write "$HOME/Library/LaunchAgents/local.testing.plist" \
StandardOutPath "$HOME/launchd.stdout.log"

defaults write "$HOME/Library/LaunchAgents/local.testing.plist" \
StandardErrorPath "$HOME/launchd.stderr.log"

That will add the two log definitions with the full path, specific and correct for each computer.

It will also replace any existing values, so you don't need to worry about duplication.

5
  • Launchagent main purpose is operating system will load them during login/reboot.How can I run these commands in other user accounts bcoz first time during manual loading it takes current home directory but when I logout and login to other user I cant run these commands and my plist load fails. Aug 3 '19 at 18:55
  • If you have root access, try a LoginHook instead of using launchd. It's technically deprecated but it still works fine.
    – TJ Luoma
    Aug 4 '19 at 1:01
  • But we Never know how long it will be supported and I cant use it in production Aug 4 '19 at 15:13
  • Without knowing more about what you are trying to do, it's hard to say, but I might try a LaunchDaemon that runs as root and then have it execute commands as the logged in user.
    – TJ Luoma
    Aug 5 '19 at 0:55
  • I fixed it by redirecting my script to that log path as shown below by jksoegaard answer. Aug 5 '19 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .