Hi everyone!
I'm currently developing a custom image on a Mediatek Ralink MIPS MT76x8 (with LinkIt Smart 7688 target profile).
The system hosts two services, one enabled by default and the other disabled by default (let's call them A and B respectively). A is started at every boot and do some checks. If a certain condition is met, then A launches B and just exits, otherwise it does other things and reboot the system, repeating the loop.
Previously, this setup was tested with the Yocto framework using systemd
, and the two services were the following.
A:
[Unit]
Description=Process A
Conflicts=B.service
[Service]
Type=simple
ExecStart=/usr/bin/process-a.sh
Restart=on-failure
RestartSec=2min
[Install]
WantedBy=multi-user.target
B:
[Unit]
Description=Process B
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/process-b <arguments>
Restart=always
[Install]
WantedBy=multi-user.target
process-a.sh
checks whether the aformentioned conditions are met, and if so it has a systemctl start B.service
.
Now I'm trying to replicate this behaviour in OpenWRT with procd
, but it seems that there's no option for respawing a program based on its exit code. The wiki (https://openwrt.org/docs/guide-developer/procd-init-scripts?s=procd_set_param%20respawn) shows only time based options. Here are the two units I've wrote for A and B:
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=95
STOP=01
PROG=/usr/bin/newborn.sh
ARGS=production
start_service() {
procd_open_instance
procd_set_param command $PROG $ARGS
# Send stdout/stderr output to the system log
# Use command logread to see the log
procd_set_param stderr 1
procd_set_param stdout 1
# Set respawn
procd_set_param respawn ${respawn_threshold:-0} ${respawn_timeout:-120} ${respawn_retry:-0}
procd_close_instance
}
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=99
STOP=01
PROG=/usr/bin/modbus-gateway
ARGS="/etc/secrets/gateway.json /etc/secrets/cert.crt /etc/secrets/private_key.pem /dev/ttyS0"
start_service() {
procd_open_instance
procd_set_param command $PROG $ARGS
# Send stdout/stderr output to the system log
# Use command logread to see the log
procd_set_param stderr 1
procd_set_param stdout 1
# Set respawn
procd_set_param respawn ${respawn_threshold:-0} ${respawn_timeout:-10} ${respawn_retry:-0}
procd_close_instance
}
The problem is that even if the program exits gracefully with exit 0
, the procd daemon tries to restart the process. I thought of making the A script stop itself after launching B, but I would like to know if there's a more elegant solution.
There's also the problem that B is configured to be launched automatically when generating the image from source, is it possible to package a startup script to be disabled by default?
Thank you
Matteo