Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Run shell script as root on boot

Hi!
I have the need to run a shell script on boot up as root.
It will not run as any other user because the software that the shell script runs requires root permissions, and I don't use my computer as root. I can't run it at login and use sudo because it would ask for a password and hang.
How can I have the shell script auto-run at boot (or login) as root?
Thanks
Ross

MacBook Pro 15" 2.66 Dual Core 4GB RAM, Mac OS X (10.6.2)

Posted on Jun 12, 2010 7:58 PM

Reply
Question marked as Best reply

Posted on Jun 12, 2010 9:18 PM

Look at launchd. If you do a Google search, there are lots of tutorials around on the Internet. And be sure to check out the man page ( man launchd.plist), as it has a good listing of the various keys and parameters you can use in your plist.

If you can't get one of tutorials to work for you, post back and I'll try to help.

charlie
3 replies
Question marked as Best reply

Jun 12, 2010 9:18 PM in response to RossIV14

Look at launchd. If you do a Google search, there are lots of tutorials around on the Internet. And be sure to check out the man page ( man launchd.plist), as it has a good listing of the various keys and parameters you can use in your plist.

If you can't get one of tutorials to work for you, post back and I'll try to help.

charlie

Jun 13, 2010 5:35 AM in response to RossIV14

You can run your script at login as root, using sudo -- but first you have to edit the file /etc/sudoers such that sudo will not ask for a password to run that script as root.

To edit that file, use the command (in Terminal) "sudo visudo"
Add a line (assuming your username is ross, and the script is /usr/local/bin/myscript):
ross ALL = NOPASSWD: /usr/local/bin/myscript


The last step is to make a script that simply calls:
sudo /usr/local/bin/myscript

And setup that script as a login item.

Jun 13, 2010 5:39 AM in response to RossIV14

Hi,

first of all you can store your shell script anywhere on your system, I prefere the location /usr/local/scripts (this doesn't exist by default).

Second you have to create a LaunchDeamon script in /Library/LaunchDeamons which execute your shell script at boot as root.

An example:

-------------------------snip------------------------
<?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>YOUR IDENTIFIERNAME</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/scripts/YOUR_SCRIPT.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>ROOT</string>
<key>GroupName</key>
<string>WHEEL</string>
</dict>
</plist>
-------------------------snap------------------------

http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemS tartup/Articles/LaunchOnDemandDaemons.html

To run a script as root during a user login you can configure a LoginHook. To create a LoginHook login as the relating user and type the following commad:

sudo defaults write com.apple.loginwindow LoginHook /Path/To/Your/Script

http://support.apple.com/kb/HT2420

Hope that helps

Bye Tom

Run shell script as root on boot

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