2

I would like to have a service which behaves differently on first run and on restart of service. Is this possible with systemd? (I use systemd in my embedded os).

I tried with ExecReload and ExecStart, but ExecReload is run only when I use command "systemctl restart". On the other hand ExecStart is run after service Restart ( I have Restart=on-failure and RestartSec=5).

5

You could use systemctl set-environment to push some values into future runs of the service. For example, with a unit:

[Unit]
Description=testing
[Service]
Type=oneshot
ExecStart=/my/command myarg1 ${MYDONE}
ExecStart=/usr/bin/systemctl set-environment MYDONE=1
[Install]

On the first systemctl start <unit> the last arg passed to /my/command will be '' and MYDONE will not be in the environment. On later starts, the last arg will be 1 and MYDONE=1 will be in the environment.

  • This is good. :) By the way I didn't know that i can specify more than one ExecStart in a .service file. – s.paszko Aug 2 '16 at 12:00
  • 1
    You are only allowed multiple ExecStart if Type=oneshot. But for any type there is also ExecStartPre and ExecStartPost. – meuh Aug 2 '16 at 12:14
1

You could define the first run in a separate .service file, using

[Service]
Type=oneshot

Then create another .service file that depends on that one, which will be the one that runs regularly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.