How to write a Mac OS X script with sudo that will load whenever user logs in?

The Yosemite 10.10.3 MacBook Pro 13 is suffering from the  discoveryd service problems, which prevent Safari from connecting to the Internet.  There is a temporary fix until 10.10.4 that requires typing in two terminal commands:

sudo launchctl unload -w /System/Library/LaunchDaemons/
sudo launchctl load -w /System/Library/LaunchDaemons/

Can these two commands be combined into a script that can be run as a login item, with root privileges?

If not, what's the method for creating a little application that the user can click on?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You might configure sudoers to allow all users to run those specific commands,  and put them in   /etc/profile  or try a suid script

grep admin /etc/sudoers

tail -1 tail -1 ~/.bash_profile
sudo launchctl load -w /System/Library/LaunchDaemons/

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I would suggest that you don't do it quite that way.  You've just allowed every admin user to sudo without any password restriction to any command and bypassed a security protocol that's meant to slow down trojan attacks.

You should restrict it a bit more with a different group and only allow the launchctl command to run.  The wheel group already exists, so you could use that by adding this line to /etc/sudoers
%wheel      ALL=(root) NOPASSWD: /bin/launchtcl

Then you should add all admins to the group, since it currently only contains root as a member.
dscl . append /Groups/wheel GroupMembership admin

You could create another group to do it instead of using wheel.  I randomly chose the group ID 400, but you should make sure that it isn't already taken.
dscl . create /Groups/launchctl
dscl . create /Groups/launchctl RealName “Local Admins”
dscl . create /Groups/launchctl passwd “*”
dscl . create /Groups/launchctl gid 400
dscl . create /Groups/launchctl GroupMembership admin

Then you add the following to /etc/sudoers
%launchctl      ALL=(root) NOPASSWD: /bin/launchtcl
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.