You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

launchd and non-root user scheduling

I ran into a problem using launchd to schedule a recurring non-root user script (High Sierra 10.13.6) I want myscript to run, at specific times, whenever my Mac is running. After I ran into this issue, I reread the details of how launchd works, and concluded my workaround may be the only viable solution.

The issue: apparently, scheduled non-root user scripts cannot be run, until after that user logs in to the Mac. I want the script to run regardless of whether I've logged in or not, since last startup.

My workaround: instead of directly scheduling my own script, I schedule a root script, at the desired times, that essentially does this:

su myuserid -c "/path/to/myscript"


I welcome advice on this.

Mac Pro (Mid 2010), macOS High Sierra (10.13.2)

Posted on Aug 6, 2018 7:06 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 7, 2018 5:36 AM

The old method of doing this which Apple discourage but is still possible is to create a cron job for a specific user.


However it is supposed to be possible to create a LaunchDaemon which runs as a specific user. The default is to run as root which most of the time is actually what you want.


Try this -


You put the plist into /Library/LaunchDaemons but edit it to say which user the script will run as

The key you need to add is UserName.

<key>UserName</key>

<string>yourusernamehere</string>

Other approaches which will not do what you want are -


  • A LaunchAgent - only runs after any user has logged in and still as default runs as root
  • A user LaunchAgent - only runs after specific user has logged in but does run as that user
  • A Login Item - only runs after specific user has logged in but does run as that user

Similar questions

12 replies
Question marked as Top-ranking reply

Aug 7, 2018 5:36 AM in response to jonkreisler

The old method of doing this which Apple discourage but is still possible is to create a cron job for a specific user.


However it is supposed to be possible to create a LaunchDaemon which runs as a specific user. The default is to run as root which most of the time is actually what you want.


Try this -


You put the plist into /Library/LaunchDaemons but edit it to say which user the script will run as

The key you need to add is UserName.

<key>UserName</key>

<string>yourusernamehere</string>

Other approaches which will not do what you want are -


  • A LaunchAgent - only runs after any user has logged in and still as default runs as root
  • A user LaunchAgent - only runs after specific user has logged in but does run as that user
  • A Login Item - only runs after specific user has logged in but does run as that user

Aug 8, 2018 4:13 AM in response to Barney-15E

The "problem" is the userid of the running process. The actual script owner doesn't seem to affect anything.

The launchd daemon uses the location of the scheduling plist to determine the process userid. If the plist is in the /Library or /System/Library tree then the process is started as root. If in ~/Library tree, then it is the owner of that directory used as the userid.

launchd and non-root user scheduling

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.