Skip to main content
1 of 5

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.