Skip to main content
4 of 5
added 20 characters in body

How do I run Launchd as root?

I'm trying to block some websites via /etc/hosts
I can't seem to figure out how to run the LaunchAgent with the correct permissions.

$ ls -la ~/Library/LaunchAgents/hosts_Blocksites_AM.plist     
-rw-r--r--@ 1 root  staff  1854 Aug 25 11:55 /Users/bryanwheelock/Library/LaunchAgents/hosts_Blocksites_AM.plist    

I set a UserName key:

<key>UserName</key>  
<string>root</string>  

Here is the hosts_Blocksites_AM.plist:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
<plist version="1.0">  
<dict>  
    <key>Label</key>  
    <string>local.job</string>  
    <key>ProgramArguments</key>  
    <array>  
        <string>cp</string>  
        <string>/etc/hosts_BLOCKED_sites.txt</string>  
        <string>/etc/hosts</string>  
    </array>  
    <key>RunAtLoad</key>  
    <true/>  
    <key>UserName</key>  
    <string>root</string>  
    <key>StartCalendarInterval</key>  
    <array>  
        <dict>  
            <key>Hour</key>  
            <integer>8</integer>  
            <key>Minute</key>  
            <integer>15</integer>  
            <key>Weekday</key>  
            <integer>1</integer>  
        </dict>  
        <dict>  
            <key>Hour</key>  
            <integer>8</integer>  
            <key>Minute</key>  
            <integer>15</integer>  
            <key>Weekday</key>  
            <integer>2</integer>  
        </dict>  
        <dict>  
            <key>Hour</key>  
            <integer>8</integer>  
            <key>Minute</key>  
            <integer>15</integer>  
            <key>Weekday</key>  
            <integer>3</integer>  
        </dict>  
        <dict>  
            <key>Hour</key>  
            <integer>8</integer>  
            <key>Minute</key>  
            <integer>15</integer>  
            <key>Weekday</key>  
            <integer>4</integer>  
        </dict>  
        <dict>  
            <key>Hour</key>  
            <integer>8</integer>  
            <key>Minute</key>  
            <integer>15</integer>  
            <key>Weekday</key>  
            <integer>5</integer>  
        </dict>  
    </array>  
    <key>StandardErrorPath</key>                                                                                                                                                          
    <string>/tmp/mycommand.err</string>                                                                                                                                       
    <key>StandardOutPath</key>                                                                                                                                                                <string>/tmp/mycommand.out</string>  
</dict>  
</plist>  

Here are the errors:

$ cat /tmp/mycommand.err   
sudo: no tty present and no askpass program specified  
cp: /etc/hosts: Permission denied  

I had tried to add the sudo command to the program arguments, but I don't think that was correct.

EDITED:

Changed Label key:

<key>Label</key>  
<string>local.hosts.blockingAM</string>  

Removed UserName key.

Moved the plist and changed name:

$ ls -la /Library/LaunchDaemons/
-rw-r--r--@  1 root  wheel  1474 Aug 29 13:19 local.hosts.blockingAM.plist  

I still seem to be getting permission errors:

system log:

8/29/16 3:50:04.006 PM com.apple.xpc.launchd[1]: (local.hosts.blockingAM[7675]) Service exited with abnormal code: 1

$ cat /tmp/local.hosts.blocking.err   
cp: /etc/hosts: Permission denied