0

I'm trying to set up porkbun dynamic dns python script to run at reboot via Cron (later I'd like to run it hourly, but baby steps...) following porkbun's tutorial https://www.youtube.com/watch?v=B2y3vT35sSE

I can run the job manually outside of Cron, but I cannot for the life of me get this to work via Cron.

I've searched the topic and tried lines that have fixed similar (not related to this particular script) issues, but I've had no luck.

Below is the cronjob (note that [domain.name] is in place of my actual domain name for privacy, but not what's in the script):

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/home:/root

@reboot /usr/bin/python3 /usr/local/www/porkbun-dynamic-dns-python/porkbun-ddns.py /usr/local/www/porkbun-dynamic-dns-python/config.json [domain.name]
5
  • the babysteps should go the other way ... get it to run hourly first
    – jsotola
    Jul 30 at 23:15
  • How would I easily test if it's working then? It's the same line and I can easily reboot the pi to test the @reboot line instead of waiting an hour to see if it worked.
    – APEX
    Jul 30 at 23:17
  • This is an exercise in futility. Presumably your unlisted script relies on networking - which is unavailable on boot. The brute force approach is to include a delay. The solution is to develop a systemd service with dependencies.
    – Milliways
    Jul 30 at 23:21
  • instead of waiting an hour ... set cron to run the job every 2 minutes
    – jsotola
    Jul 30 at 23:56
  • @jstola so funny that after I read Milliways comment about this not working well at reboot (due to network not being ready) and re-thinking my response to your suggestion to get it to work hourly first I indeed set it to run every minute. I saved the crontab file and checked after a couple of minutes and the DNS updated as it should. I've changed it to run every 2 hours now instead of every minute. Sorry I was being dense on this one. I think if I'd thought of that hours ago I wouldn't have kept bashing away at this and it would have "just worked" (because networking would have been ready).
    – APEX
    Jul 31 at 0:01

2 Answers 2

0

Your dynamic DNS script most probably needs network to be up and running.

Traditionally (Sys V init) , cron would be started after the network is up (run level 2, S30 for the network and S75 for cron). However, with systemd, this is no longer guaranteed.

So, to run at boot, you could write a wrapper:

#!.bin/bash
while ! ping -c1 8.8.8.8 ; do
    sleep 1
done
/usr/bin/python3 /usr/local/www/porkbun-dynamic-dns-python/porkbun-ddns.py /usr/local/www/porkbun-dynamic-dns-python/config.json [domain.name]

Another option on Raspian is to use systemd for starting at boot. You would create a unit-file with something like this:

[Unit]
Description=test service
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=wrapperscript_above

[Install]
WantedBy=multi-user.target

But as said, if you want to execute it hourly anyway, execute it every hour in stead of messing with @reboot or unit-files.

0

I can run the job manually outside of Cron, but I cannot for the life of me get this to work via Cron.

This difference is usually about environment variables. You could use printenv in your cron job and compare that to what you see from printenv when run from a terminal.

In your case, it may be that your cron job is running too early during boot.

In any case, your cron job is likely generating an error message. You'll want to see that error message. In your @reboot line add >/tmp/foo 2>&1 to the end. Then after rebooting, see what's in /tmp/foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.