I have a machine that reboots nightly, but when it does it ends up connecting to the wrong router somehow, and always gets the wrong IP. I don't want it to use a static IP, I have my router issue it a reserved address.

Anyway, it's easily fixed by renewing the DHCP lease (Ethernet, not wifi), but I want this to be automated after the reboot. I can't get this to work from cron for some reason... I've tried both of these:

* * * * * echo "add State:/Network/Interface/en0/RefreshConfiguration temporary" | sudo scutil
* * * * * sudo ipconfig set en0 DHCP

Neither does anything from Cron, but works fine from the CLI manually.

How can I get this to work via cron?

EDIT: I have tried using the root user as suggested in the first answer, but this also does nothing. No change, nothing output to a log, just nothing.

  • Why sudo, and where should the password come from when run from cron?
    – nohillside
    Commented Jul 17, 2022 at 21:42
  • I just used code snippets I found on articles explaining how to renew the IP from cron. I just used them as-is, though I assumed sudo would be needed since it's going to run unmonitored, but maybe not? I'll try without it, though I run a nightly shutdown command with sudo and no password so, I thought it would just work.
    – JVC
    Commented Jul 17, 2022 at 22:40
  • @nohillside no change without sudo.
    – JVC
    Commented Jul 17, 2022 at 22:50
  • 1
    Perhaps only one router should be serving DHCP? What's your network topology? How many networks, devices, etc?
    – IconDaemon
    Commented Jul 18, 2022 at 0:07
  • DHCP is completely disabled on all but my primary router, the cable modem. In particular, the router (Access Point actually) that this computer keeps connecting to, is absolutely positively set to DHCP disabled because it says so very clearly. Netgear Nighthawk in AP mode. This is the only computer exhibiting this issue, I have no real idea how it can be connecting to a router that is not issuing IP addresses. It's a very odd problem.
    – JVC
    Commented Jul 18, 2022 at 3:40

1 Answer 1


Important first step

Make sure cron is working. You need to grant /usr/sbin/cron full disk access. Here's a good guide. You should be able to have the current date and time appear in a file

* * * * * /bin/date > /Users/yourusername/cronout.txt

Root crontab

Adding sudo to a crontab entry won't work because sudo is requires you to enter your password when the the command runs and cron is not interactive so you cannot enter your password.

There are several ways to make this work. The easiest, and reasonable one for this use case, is to have this job run as root. Log in as root and create the crontab there.

% sudo -i # Opens an interactive shell as root 
root# EDITOR=nano crontab -e # EDITOR is probably not configured for root  

That'll open the nano editor. Feel free to use vi. In the editor add one of your commands, without the sudo

* * * * * ipconfig set en0 DHCP

Save. I like to test my cron entries with crontab -l. Once that's done, exit the shell logged in as root with exit or ^D.

If you need to troubleshoot this problem further, add some logging information to cron. Change the line to

* * * * * ipconfig set en0 DHCP &>> /Users/JVC/cron.log

That will append the output of cron to a log file that you can read and discover why the command didn't work.

An alternate means to this end would be to modify your sudoers file via visudo to permit this command to run without requiring a password 1.

That leaves open the questions of why 1) you need to reboot every night and 2) why this machine connects to the wrong router, but this should solve you immediate problem.

  • Hmmm ok this is slightly over my head at the moment but I'm sure I can figure it out. I reboot nightly just because when I don't, I find the machine bogs down over time. It's a dedicated media server running a variety of stuff like Plex, Sonarr, etc. and there seem to be memory leaks, so a nightly reboot keeps everything humming perfectly. But this router thing is new and I have no idea why it happens. Thanks, will see if I can get this to work.
    – JVC
    Commented Jul 18, 2022 at 12:48
  • One question. Why does this need to run as root, when sudo /sbin/shutdown -h now runs just fine from my normal admin user?
    – JVC
    Commented Jul 18, 2022 at 17:31
  • OK I got it going, but it still does nothing. The IP lease is never renewed. I tried both code snippets I listed in my OP, neither does anything when run via crontab by the root user. Also no log file is created when I add that.
    – JVC
    Commented Jul 19, 2022 at 19:52
  • @JVC when you issue the command with sudo, you are running whatever follows as root.
    – Ray Baxter
    Commented Jul 20, 2022 at 0:55
  • Right, which is exactly why I don't understand why I would need to actually log in as root in order to set this up. I'm already running the command as root, but it doesn't work. Only now I've confirmed that it doesn't work as the root user either.
    – JVC
    Commented Jul 20, 2022 at 2:05

You must log in to answer this question.

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