2

I'm trying to execute my test daemon by launchd with logon user privilege. I saved my plist file at /Library/LaunchAgents/myplist and it was executed well with logon user account at Mavericks. My command was like this.

$launchctl load /Library/LaunchAgents/myplist

But it was executed as root account when I tried it at Yosemite. As I know, it should be executed as logon user account, but it wasn't at Yosemite. I also moved myplist file to ~/Library/LauchAgents/myplist, but the result was same. What happend to Launchd at Yosemite and how can I execute launchd with logon user account?

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
jay
  • 21
  • 1
  • 1
    Your daemon was launched as `root` when you put your plist into the `~/Library/LauchAgents/myplist`? Is your daemon `setuid root` by any chance? – DarkDust Aug 10 '15 at 13:33
  • Yes. It was executed with root when I put my plist into the ~/Library/LaunchAgents/myplist. My daemon never setuid, it's just a simple nohup command like "nohup mydaemon > /tmp/mydaemon.log 2> /tmp/mydaemon.log &". – jay Aug 11 '15 at 01:37

1 Answers1

0

The user can be specified in the daemon's plist with the UserName key. For example: -

<key>UserName</key>
<string>_daemonUser</string>

Where _daemonUser is the name of the user the daemon should run as, assuming that a user with that name exists.

If you want to use the name of the current logged-in user, you'll have to specify that when calling launchctl load. In a bash script, run with sudo, you can do something like this: -

SESSION_USER="$USER"
echo Starting MyDaemon as user $SESSION_USER
su $SESSION_USER -c 'launchctl load /Library/LaunchAgents/myplist.plist' 

You may need to consider other users too, if user switching is applicable and they're already logged-in. This is more complicated, but there's a solution here.

Community
  • 1
  • 1
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • I've already tried UserName key, but it didn't work. Still executed with root account. As I mentioned, It's just happend at Yosemite only, it's executed with logon user account at Mavericks. Any idea about this? – jay Aug 11 '15 at 01:42
  • Sorry @jay, is launchd running as root? As the [Apple Docs state](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html) for UserName *This optional key specifies the user to run the job as. This key is only applicable when launchd is running as root.* – TheDarkKnight Aug 11 '15 at 08:18