Skip to content

Automation

rnjmur edited this page Jan 12, 2024 · 12 revisions

Linux

There are two ways I would recommend to automate running cf-dyn-ip-updater on Linux. I will describe each one below.

Running in crontab:

You can run the program in the crontab of any user with execute permissions to the file and write permissions to the directory.
I would recommend running this in the root crontab to prevent issues but if you are experienced in Linux and wish to use another user by all means do it! I will be using the root crontab here.

We first need to create a simple shell script named cf-dyn-ip-updater.sh in the directory where you installed cf-dyn-ip-updater:
I use nano but feel free to use what ever text editor you like.

#!/bin/sh
cd /opt/cf-dyn-updater # Use whatever directory you have the cf-dyn-ip-updater files located in
python3 cf-dyn-ip-updater.py # You may need to change this depending on how you have python3 setup

Next we edit our crontab:
sudo crontab -e

Add the following line:
0 */2 * * * /opt/cf-dyn-updater/cf-dyn-ip-updater.sh >/dev/null 2>&1

I will explain this line for those not familiar with crontab and you can modify it as you see fit. You can run man crontab or do a google search to find out more about the values that can be configured.
0 = at 0 minutes - Script runs at the 0 minute mark
*/2 = at every hour divided by two which equals every 30 minutes
* = every day of the month
* = every month
* = every day of the week
/opt/cf-dyn-updater/cf-dyn-ip-updater.sh = the command we are executing
>/dev/null 2>&1 = redirect output into a "null" device, so basically discard it

When finished exit crontab and be sure you save it and Viola!, cron should now run the cf-dyn-ip-updater every 30 minutes.

I will answer a quick question that you may think of - why not use /bin/python3 /opt/cf-dyn-updater/cf-dyn-ip-updater.py instead of /opt/cf-updater/cf-dyn-ip-updater.sh? I cannot give a full techincal explaination on the reasons but I have messed around enough that I find running python scripts directly from crontab causes all kinds of issues, from $PATH issues to import issues. Suffice it to say, I typically just create a shell script to run python from crontab and I almost never have issues. Feel free to play around with the crontab config; don't let my bad experiences persuade you from experimenting!

Running as a daemon:

Running the program as a daemon has some advantages over cron. First, the daemon system (systemctl) handles the program starting, stopping, and monitoring. Second, it is easier to control the user running the program. Third, if there are issues the systemd logs can provide additional information about errors.

First we create the service file named cf-dyn-ip-updater.service in the /etc/systemd/system directory:

[Unit]
Description=Cloudflare DNS Dynamic IP Updater

[Service]
User=root
Group=root
ExecStart=/bin/python3 /opt/cf-dyn-updater/cf-dyn-ip-updater.py -s
ExecStop=/bin/pkill cf-dyn-ip-updater
KillMode=none
Restart=always
RestartSec=120
Type=simple

[Install]
WantedBy=multi-user.target

We then want to reload systemctl by running sudo systemctl daemon-reload.
After reloading the services you need to enable the service to run at startup by doing sudo systemctl enable cf-dyn-ip-updater.
Lastly run sudo systemctl start cf-dyn-ip-updater to start the process.
You can run sudo systemctl status cf-dyn-ip-updater to ensure the process is running and see any messages generated by the service.

Windows

If using Windows I would advise you to use the standalone windows executable in the Releases section.
To create a task on Windows you just need to create a Task in the Task Scheduler.
You can go to search and type in Task Scheduler.
In Task scheduler select Create Task.
Name the task whatever you want, I use Cloud Flare DNS Updater.
You can optionally enter a Description.
Click the Change User or Group button, enter SYSTEM in the input box and click Check Names then click OK.
Click the Triggers tab and change the Begin the task: setting to At startup then select the Repeat task every: checkbox and set your interval. I set mine to 30 minutes then change the for a duration of: setting to Indefinitely. Ensure the Enabled checkbox is checked.
Next go to the Actions tab and click the New... button.
In the New Action pop up set the Action: to Start a Program then click the Browse button then find your cf-dyn-ip-updater program and click Open then click the OK button. If when you installed Python3 you did not set to execute .py files automatically or do not have Python3 in your PATH you may need to set the program to start the Python3 executable then in the Edit Action window add the program name cf-dyn-ip-updater.py into the Add arguments (optional): section if you are using the python source instead of the windows executable.

It should also be possible to set your Trigger settings and un-check the Repeat task every: setting and make sure all the checkboxes are un-checked except the Enabled checkbox.
In the Action settings you can add -s in the Add arguments (optional): field to have the program run in a loop using the time wait settings in the config file.
I have not tested running the program as a Task in Windows using the -s switch so I am not sure if it will behave properly.

Clone this wiki locally