[systemd] How to use a parameter in ExecStart command line?

I try to convert a SysVintit script used on Debian (and derivatives distros such as LinuxMint and Ubuntu & Co.) to a systemd service to be used on Fedora or ArchLinux (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

suso 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 maner 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 trully 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.

TIA for your time and advice.

This question is related to systemd sysv

The answer is


Although systemd indeed does not provide way to pass command-line arguments for unit files, there are possibilities to write instances: http://0pointer.de/blog/projects/instances.html

For example: /lib/systemd/system/[email protected] looks something like this:

[Unit]
Description=Serial Getty on %I
BindTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service

[Service]
ExecStart=-/sbin/agetty -s %I 115200,38400,9600
Restart=always
RestartSec=0

So, you may start it like:

$ systemctl start [email protected]
$ systemctl start [email protected]

For systemd it will different instances:

$ systemctl status [email protected]
[email protected] - Getty on ttyUSB0
      Loaded: loaded (/lib/systemd/system/[email protected]; static)
      Active: active (running) since Mon, 26 Sep 2011 04:20:44 +0200; 2s ago
    Main PID: 5443 (agetty)
      CGroup: name=systemd:/system/[email protected]/ttyUSB0
          + 5443 /sbin/agetty -s ttyUSB0 115200,38400,9600

It also mean great possibility enable and disable it separately.

Off course it lack much power of command line parsing, but in common way it is used as some sort of config files selection. For example you may look at Fedora [email protected]: http://pkgs.fedoraproject.org/cgit/openvpn.git/tree/[email protected]


To attempt command line arguments directly is not possible.

One alternative might be environment variables (https://superuser.com/questions/728951/systemd-giving-my-service-multiple-arguments).

This is where I found the answer: http://www.freedesktop.org/software/systemd/man/systemctl.html

so sudo systemctl restart myprog -v -- systemctl will think you're trying to set one of its flags, not myprog's flag.

sudo systemctl restart myprog someotheroption -- systemctl will restart myprog and the someotheroption service, if it exists.