I try to convert a SysVinit script used on Debian (and derivative distros such as Linux Mint and Ubuntu & Co.) to a systemd service to be used on Fedora or Arch Linux (and derivative distros such as Bridge or Manjaro), but even if the systemd start system is more performant and versatile than the previous, I don't understand how to make simple stuff such as using an "optional" argument on a command line like ExecStart=
or ExecRestart=
!
Here is what I do with SysVinit:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myprog
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: myprog init script
# Descripton: this script manages myprog
### END INIT INFO
# exit if myprog isn't installed
[ -x "/opt/mystuff/myrpog" ] || exit 0
case "$1" in
start)
cd /opt/mystuff
./myprog -r
echo
;;
stop)
cd /opt/mystuff
./myprog -x
;;
restart)
cd /opt/mystuff
./myprog -x && ./myprog -r
;;
version)
cd /opt/mystuff
./myprog -v
;;
try)
cd /opt/mystuff
./myprog
;;
*)
echo "Usage: sudo service myprog {start|stop|version|try}" >&2
exit 3
;;
esac
:
So the script above allows to use different arguments including an empty one that will display the message "Usage: ..." when using the following command lines:
sudo service myprog start # Start myprog with the -r argument
sudo service myprog stop # Stop myprog with the -x argument
sudo service myprog version # Display the release of myprog in the console
sudo service myprog try # Start myprog without any argument
sudo service myprog restart # Stop then start myprog with the -r argument
sudo service myprog # Display the "Usage:..." message in the console
Now, with systemd, the script should look like this :
[Unit]
Description=This script manages myprog
ConditionFileExecutable=/opt/mystuff/myprog
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/mystuf/myprog -r
ExecStop=/opt/mystuff/myprog -x
ExecRestart=/opt/mystuff/myprog -x : /opt/mystuff/myprog -r
[Install]
After=NetworkManager.service
Here starts my problems (and my lack of systemd knowledge):
Obviously, systemd doesn't provide a command such as ExecCustom01=
, ExecCustom02
, etc. that would allow me to create commands for "version" and "try" (and other if needed).
So, I may use ExecRestart
in a different manner if I could use an argument to start both the "version" or the "try" command (being said that the "real" restart may be done by starting successively the stop and the start commands).
These "customized" ExecRestart=
command could then look like this:
sudo systemctl restart myprog # Without argument for the "try" command
and
sudo systemctl restart myprog -v # For the "version" command
The systemd script could then look like this:
[Unit]
Description=This script manages myprog
ConditionFileExecutable=/opt/mystuff/myprog
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/mystuf/myprog -r
ExecStop=/opt/mystuff/myprog -x
ExecRestart=/opt/mystuff/myprog ????? // This is where I need to use an argument
[Install]
After=NetworkManager.service
But I don't know if it's possible, and if yes, what is the syntax to use?
Any help would be truly appreciated since even after spending a bunch of hours in the multiple systemd man pages, I couldn't find any explicit sample about how to do that.
Thanks in advance for your time and advice.
/opt/mystuff/myprog
is a binary executable then just so it is called. Otherwise:ExecStart=/bin/sh -c '/opt/mystuf/myprog -r'
– Dioptricsversion
ortry
? – Medawar[Unit]
'sDescription=...
option so callingsystemctl status myservice
would returnThis script manages myprog: v.0.90.a
or whatever version string you have. – Medawarservice
. (And if they do, they've been using a very poorly designed operating system or otherwise learned some really bad habits.) They expect to run something likemyprog --version
ormyprog -V
directly. So there's no need for this at all. And thetry
verb doesn't make any sense at all. Do or do not; there is no try. There are ways to preserve these old verbs if really necessary, but in this case they don't appear to be necessary. – Fervor