Link pi-star to reflectors using systemd

Posted on Dec 22 2024

Most people managing a repeater or using a Pi-Star open-spot at home want to automate the connection to a specific reflector to ensure they don't miss a net. To automate that task, they look online, and the internet tells them to insert a line in the file /etc/crontab to tell the Pi-Star to link the repeater at a specific day and time. See the example below.

0   6  * * *  root    /usr/local/sbin/pistar-link ref084_a
Pi-Star hotspot
Pi-Star hotspot

This solution works okay, but there is a better approach. Instead of using cron, a more effective method is to utilize the Linux systemd service. Using systemd services, in conjunction with timers, offers several advantages. Systemd provides superior management and monitoring capabilities and allows for human-readable scheduling and timezones through the OnCalendar feature. Systemd also ensures that all dependencies, such as network connectivity, are satisfied before executing the task. You can easily start, stop, and check the status of a service or a timer.

To interact with systemd, Linux uses two commands: systemctl and journalctl. The systemctl command sends orders to systemd, and journalctl offers comprehensive debugging capabilities.

In summary, utilizing systemctl services and timers for tasks like reflector connections is more robust, flexible, and secure than traditional crontab methods.

Configuring systemd

Note

All the source files in this blog-post can be found on my github account 0x9900 under the name pistar_timers.

The service file

To configure the systemd service, we need to create two files. First, the .service file contains the rules and commands to run. Next, we need to create timers.

We will use a template for the service file since the command to connect to a reflector remains consistent. The only variation is the name of the reflector to which we are linking.

This is the .service file I installed on my pi-star. The character @ in the name is important. This file is a service template and can be called from several timers. The file is simple; it contains a description and the command to run. The %i is the instance name. It is the part of the unit name between the @ character and the file suffix.

# cat /etc/systemd/system/[email protected]
[Unit]
Description=Connect pistar to %i

[Service]
ExecStart=/usr/local/sbin/pistar-link %i

The timer file

Next, we need to create a .timer file per reflector. The timer file contains the unit section with the description, the timer section, and the dependencies.

The field OnCalendar contains the time of day in a human-readable fashion and, eventually, the timezone.

The field Unit contains the name of the service to call. In the service f le we created above, the %i will be replaced by the name of the reflector specified between the @ and the .service suffix.

Specifying the timezone is convenient. First, you don't have to do any timezone calculations. Second, if you travel, Pi-Star will connect to the reflector on time, no matter your time zone.

Note

The net starts at 3 pm in Toronto time in the following example.

# cat /etc/systemd/system/pistar-xlxvps.timer
[Unit]
Description=Connect to XLXVPS_C on Satruday at 15:55 Toronto time.

[Timer]
OnCalendar=Sat 15:55:00 America/Toronto
Unit=pistar-link@XLXVPS_C.service

[Install]
WantedBy=timers.target

Installation

Once you have created these files in the /usr/systemd/system directory, run the following commands to install them into the systemd service.

pi-star@dstar(rw):~$ sudo systemctl daemon-reload
pi-star@dstar(rw):~$ sudo systemctl enable pistar-xlxvps.timer
pi-star@dstar(rw):~$ sudo systemctl start pistar-xlxvps.timer

Now, 5 minutes before 3 pm every Saturday, your hotspot or repeater running pi-star will connect to the XLXVPS Charlie.

You can run the status command to verify the timer has been loaded correctly and that there are no errors. You will see the timer's name, when it was activated, and when it will be triggered. In our case:
Trigger: Sat 2024-12-28 12:55:00 PST; 5 days left

pi-star@dstar(rw):~$ sudo systemctl status pistar-xlxvps.timer
● pistar-xlxvps.timer - Connect to XLXVPS_C on Satruday at 15:55 Toronto time.
  Loaded: loaded (/etc/systemd/system/pistar-xlxvps.timer; enabled; vendor preset: enabled)
  Active: active (waiting) since Mon 2024-12-23 10:30:01 PST; 6s ago
 Trigger: Sat 2024-12-28 12:55:00 PST; 5 days left
Triggers: ● pistar-link@XLXVPS_C.service

Dec 23 10:30:01 dstar systemd[1]: Started Connect to XLXVPS_C on Satruday at 15:55 Toronto time.

Debugging

If you make an error in either of these files, the start command will refuse to run and display an error message. You can view all the errors using the command journalctl -xe. However, this command can be verbose, making finding the error challenging. I prefer the command journalctl -u <service-name>, which only shows messages related to your specific service. You can run journalctl -xu <service-name> for more detailed output about your service.

Example

pi-star@dstar(rw):~$ sudo systemctl start pistar-xlxvps.timer
Job failed. See "journalctl  -xe" for details.

pi-star@dstar(ro):~$ journalctl -u pistar-xlxvps.timer
Dec 23 10:37:44 dstar systemd[1]: pistar-xlxvps.timer: Refusing to start, unit pstar-link@XLXVPS_C.service to trigger not loaded.
Dec 23 10:37:44 dstar systemd[1]: Failed to start Connect to XLXVPS_C on Satruday at 15:55 Toronto time..

The first line shows the error: pistar-xlxvps.timer: Refusing to start, unit pstar-link@XLXVPS_C.service to trigger not loaded . We are getting this error because the pstar-link does not exist. The correct name of the service is pistar-link.

After fixing the error, we have:

pi-star@dstar(rw):~$ sudo systemctl daemon-reload
pi-star@dstar(rw):~$ sudo systemctl restart pistar-xlxvps.timer
pi-star@dstar(rw):~$ sudo systemctl status pistar-xlxvps.timer
● pistar-xlxvps.timer - Connect to XLXVPS_C on Satruday at 15:55 Toronto time.
  Loaded: loaded (/etc/systemd/system/pistar-xlxvps.timer; enabled; vendor preset: enabled)
  Active: active (waiting) since Mon 2024-12-23 10:53:33 PST; 7s ago
 Trigger: Sat 2024-12-28 12:55:00 PST; 5 days left
Triggers: ● pistar-link@XLXVPS_C.service

Dec 23 10:53:33 dstar systemd[1]: Started Connect to XLXVPS_C on Satruday at 15:55 Toronto time..

This shows that the timer has been loaded into systemd and is active.

List timers

An interesting command to use is systemctl list-timers. This command provides information about when a timer was last activated, whether it was successful, and when it will be triggered next. This makes it easier to identify any potential issues.

For example, you can see the exact date and time when the timer is scheduled to run next, such as "Sat 2024-12-28 12:55:00 PST," along with the remaining days and hours until the timer is activated.

pi-star@dstar(rw):~$ sudo systemctl list-timers  pistar-xlxvps.timer
NEXT                        LEFT        LAST PASSED UNIT                ACTIVATES
Sat 2024-12-28 12:55:00 PST 5 days left n/a  n/a    pistar-xlxvps.timer pistar-link@XLXVPS_C.service

1 timers listed.

Systemd: Reliable and Robust

While cron offers simplicity, using systemd for reflector connections provides greater reliability, easier debugging, and more robust service management. With a bit of setup, you can automate and stabilize your connections, leaving you free to focus on making contacts instead of troubleshooting.

 Ham Radio      Digital Radio      Pi-Star      D-Star