You can easily run as another user with sudo -u
like so:
#!/bin/sh
UZER=jsmith
sudo -u "$UZER" /path/to/program/you/want/to/run
exit 0
Just change 'jsmith' to the appropriate short user name, and then save that as a script somewhere, and call from /Library/LaunchDaemons
Remember that all files in /Library/LaunchDaemons must be owned by root to be run.
(I think this is what bmike was suggesting)
Another option
However, I wrote up a HOWTO for making auto-login more secure:
Terminally Geeky: use automatic login more securely
The executive summary is this:
- Turn on Automatic login
- Put this launchd plist in ~/Library/LaunchAgents
- Reboot
What it does:
As soon as you login, launchd will throw you back to the login screen using:
"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession" -suspend
Note that is all one long line.
Considering that someone with physical access to your computer has a lot of potential exploits, I consider this relatively safe, but of course everyone has to make their own determination. I run this in my small office, but my iMac is in my private office which I can lock when I'm not there.
Also note that automatic login doesn't work with FileVault 2.
I'm not a developer or a guru in all of the available keys with launch daemons, but that is kind of a tall order as by design, root can do anything. I'm thinking you would basically be creating a security "virus" that would have a helper mechanism reload the daemon if it gets unloaded.
Best Answer
First, Shadowsocks has an active GitHub site where you can ask this question directly to the people working on the project. They're likely to know a lot more about it than anyone here.
That being said, Shadowsocks (from our discussion, above) creates these launchd plist files programmatically, so there is no easy way — short of going to GitHub and becoming a project contributor — to stop it from doing so. I assume you want ss-local to be running while Shadowsocks is running, you just don't want it to be running the rest of the time. You can do this manually through terminal using the
launchctl
utility. First you need to get theLabel
of the launchd job, so open the plist files that Shadowsocks creates in a text editor and look for something like this:The bit between the
<string>
tags is the launchd job label, so if you run the following command in terminal (once for each plist file):It will remove the job from launchd, which shuts down whatever processes those jobs are running. I assume that Shadowsocks will restart the jobs when you launch it again, but if not, you can do that manually as well, using:
You could script this in a couple of different ways, I think, but I don't know what scripting languages you're familiar with (if any), and it would be better to go to GitHub and ask for a developer solution regardless.
EDIT:
Per comments... OSX used to have to have a system for creating logout hooks through the Window Server, but that was deprecated back in 10.4, and I don't know if it still works. So instead, I'm going to tell you how to create a background script app that should do what you want.
Here's the script, follow the instructions below:
Assuming that you have the launchd job names and the process name of Shadowsocks correct, this applet will sit and watch for Shadowsocks to quit, and whenever Shadowsocks quits, this app will remove the launchd jobs (shutting down the processes they are running) and delete the launch agent plist files. It will do the same thing when it quits, at logout, restart, or shutdown.
I've set the idle time to 15 minutes, which seems reasonable (on average, ss-local will only run for 7.5 minutes after you quit Shadowsocks)
Once you're sure it works the way you want, you can turn it into a proper background app. Quit the app and open its info.plist file - right-click on the app and choose "Show Package Contents"; info.plist will be down in the 'contents' folder — and add in the following key-value pair (being careful not to mess up the other key-value pairs):
This keeps the app from becoming visible (no dock icon, and it never becomes frontmost or takes over the menu). It can still display alerts (useful for error debugging) but that's it. You can quit it manually by double-clicking on the icon again; that's what the
reopen
handler is for.