Relative Path in launchd plist
Asked Answered
C

2

6

I am currently using a plist to run a shell script.

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
                <key>Label</key>
                <string>com.name.set</string>
                <key>Program</key>
                <string>/Users/username_here/Desktop/simple.sh</string>
                <key>RunAtLoad</key>
                <true/>
                <key>StartInterval</key>
                <integer>5</integer>
                <key>StandardErrorPath</key>
                <string>/tmp/com.name.example.stderr</string>
                <key>StandardOutPath</key>
                <string>/tmp/com.name.example.stdout</string>
        </dict>
        </plist>

This works! But when I change the program name to be

<string>/Desktop/simple.sh</string>

it doesn't run the script. also ~/Desktop/simple.sh does not work.

Is there a way to run the script without knowing the username and using an absolute path?

I am also getting this error message when I tail launchd.

com.apple.xpc.launchd[1] (com.name.example[8178]): Service could not initialize: 14F27: xpcproxy + 13421 [1402][AD0301C4-D364-31CE-8BA7-B5DBECE64D0A]: 0x2

Thanks!

Collywobbles answered 2/2, 2017 at 20:36 Comment(0)
D
7

Using a shell as arg0 and giving it a path relative to the user's home folder works for me:

<key>ProgramArguments</key>
<array>
    <string>zsh</string>
    <string>-c</string>
    <string>~/CLI/scripts/list_open_jira_tickets --skip=5297 &gt; ~/CLI/tmp/open_jira_tickets.txt</string>
</array>
Daisie answered 18/11, 2018 at 15:35 Comment(0)
C
0

If daemon is run as a per-user agent (installed in ~/Library/LaunchAgent) you can use a period get the relative path. Which would be the home folder (~/)

So you can do this:

<key>Program</key>
<string>./Desktop/simple.sh</string>

It might be nicer to save your program next to your plist:

./Library/LaunchAgents/simple.sh
Cajuput answered 24/4, 2017 at 7:49 Comment(4)
. in this situation translates to /, not ~.Pharisee
Did you try it? My cron jobs work like this. If your script is a USER agent (and thus located in the library of the Home folder), launchd’s current working directory is the Home folder.Cajuput
I did try it, on my user’s LaunchAgents directory on macOS 10.12 (Sierra), and it translated to /.Pharisee
It seems like a poor idea to save the script next to the plists. Those directories are scanned at startup, so you wouldn’t necessarily want things that don’t belong.Pharisee

© 2022 - 2024 — McMap. All rights reserved.