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
3 replies

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

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.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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 Account.