2

So sometimes my Mac loses its connection to the Internet.

When I'm in front of it physically I can use the terminal and do the following to repair it:

networksetup -setnetworkserviceenabled ethernet off

type my password then:

networksetup -setnetworkserviceenabled ethernet on

and type my password again.

The thing is the machine is hosting several servers and I need them to be running even when I'm not physically present. So I wanted to run a daily script that would do this when I'm not present and be sure the connection is reset at least once a day. But since I haven't figured out a way to include my password in the command line I can't do that.

So my question is: is it possible to do what I described?

  • 6
    Why does the computer use its connection though, wouldn't it be better to fix that problem? XY problem? meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Rob de Jonge Apr 28 '16 at 2:52
  • Can't you run this from launchd automatically – Mark Apr 28 '16 at 12:14
  • What I meant when i said the computer was hosting several servers is this: It runs 2 minecraft servers and a Plex server, I also use Teamviewer to access my computer when I'm away to check on my cat (yeah…). The thing is if the DLNA option is ticked in the Plex server, my computer might lose its connection at some point and I can't predict why or when this could happen. My problem seems to be related to the DLNA server itself since I had the same problem with another DLNA server (Firestream) but everything I found is already done so I have to be ready for X by solving Y… – NicolasPP Apr 28 '16 at 19:14
3

Ok, thank you Batman for suggesting to edit the sudoer file. Instead of crontab or launchd to schedule it once a day (which wasn't good enough anyway), I instead wrote that applescript to do it more regularly and so far it seems to be working:

repeat
    repeat
        try
            do shell script "ping -t 15 8.8.8.8"
            exit repeat
        on error
            display notification "Reconnection attempt"
            do shell script "sudo networksetup -setnetworkserviceenabled ethernet off"
            do shell script "sudo networksetup -setnetworkserviceenabled ethernet on"
            delay 9
            display notification "Connection should be established"
        end try
    end repeat
    delay 5
end repeat

I'm adding more details: I exported it as an .app then I edited the .plist of it with the lines:

<key>LSBackgroundOnly</key>
<string>1</string>

To make it invisible when it runs (no dock icon) and finally I made it run on login.

1

sudo allows you to run a command at an elevated permission level. You can edit the sudoers file by running

sudo visudo

One way to do this is to first add a line to your sudoers file which allows the listed commands to be run without a password. By adding the specific command to your sudoers file, you can allow the desired commands to be run without password. You can do this by entering the following 2 lines to your sudoers file (the file that opens when you run "sudo visudo"):

user ALL=(ALL) NOPASSWD: /usr/sbin/networksetup -setnetworkserviceenabled ethernet on user ALL=(ALL) NOPASSWD: /usr/sbin/networksetup -setnetworkserviceenabled ethernet off

with "user" replaced by your user name.

Then, as your user, to run something at a regular time, you can use crontab

crontab -e

and append the lines:

0 1 * * * sudo networksetup -setnetworkserviceenabled ethernet off && sudo networksetup -setnetworkserviceenabled ethernet on

which will apply this restart at 1 AM every day.

Another solution: Add the commands directly to root's crontab:

sudo -s

crontab -e

Then enter the line

0 1 * * * networksetup -setnetworkserviceenabled ethernet off && networksetup -setnetworkserviceenabled ethernet on

and save and exit.

You may want to put this in some sort of script that notifies you that the network has been successfully restarted (e.g. with an email or something) so that you're sure it worked.

Alternatively, you can use a launchd script.

  • That should be 2 answers - the sudo which does not meet the requirements and the cron which might do – Mark Apr 28 '16 at 12:15
  • There are two cron-based approaches listed: one uses sudo+ a non-priviliged user in a crontab, while the other uses crontab for root. I don't think it makes sense to split them into two answers, since the bulk of the idea is the same. – Batman Apr 28 '16 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .