I have a script, let's call it on-event.sh
, that's going to be run at specific times, such as when a particular application launches or quits. The script itself will run as the current user (not root) since I'll be using BetterTouchTool to execute it.
However, on-event.sh
needs to somehow trigger the execution of another script that must run as root, which I'll call run-as-root.sh
. run-as-root.sh
will execute some commands to load or unload specific third party launch daemons (not my own), hence the need to run as root. Since it'll all be happening in the background, it needs to happen without requiring user intervention, so it can't use sudo
or AppleScript's with administrator privileges
. Because of the obvious security implications, I only want it to be possible to execute run-as-root.sh
as root and not any arbitrary script or terminal command.
One option I'm already aware of is I could create a launch daemon that opens a socket, waits for a connection, and then executes run-as-root.sh
, and have on-event.sh
connect to that socket. But that's a more heavy weight solution and I'm hoping for an easier way to do this.
How can I set it up so this is possible?
launchd
orcron
do out of the box. Regardless, when either of these services kick off a script, it runs within the context it was defined; this also applies to Windows. It's not going to allow you to escalate priviliges like that at runtime.sudo
to elevate privileges by adding an appropriate line to/etc/sudoers
, but this comes with its own set of risks. Can you share some details about whatrun-as-root.sh
needs to do, there might be other ways to accomplish this.