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]
2 Answers 2
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.
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
instead of waiting an hour
... set cron to run the job every 2 minutes