I've got a unit that I'll just call eth-service
that requires another unit that I'll call eth-server
. In the unit file of eth-service
I have
Requires=eth-server.service
After=eth-server.service
Now, when I run systemctl stop eth-server
, then eth-service
stops as well, as it should. However, later when I run systemctl start eth-server
, unfortunately eth-service
remains inactive, so I have to remember to re-start it.
I'm trying to make my unit file setup as idiot-proof as possible (especially against myself) – is it possible to set up my unit files so that when the server starts up, the service does too? If I were to put into my eth-server
unit file:
Requires=eth-service.service
Before=eth-service.service
so that they mutually require each other, that would cause the service to start up when the server does, but it also means that when I run systemctl stop eth-service
then eth-server
stops too, since it now requires the service, which is not desirable.
Best Answer
Let me see if I understood the use-case:
eth-service
should start botheth-server
andeth-service
.eth-server
should start botheth-server
andeth-service
.eth-server
should stop botheth-server
andeth-service
.eth-service
should ONLY stopeth-service
and leaveeth-server
running.You can achieve it by adding
WantedBy=eth-server.service
to the [Install] section of theeth-service.service
file, orWants=eth-service.service
to the [Unit] section of theeth-server.service
.According to the systemd documentation
Wants
is a weaker version of theRequires
, and it seems like ifWanted
unit is stopped, the unit that requested it is not affected. But there might be an undesirable side-effect in this setup whereeth-server
will start no matter ifeth-service
fails to start or not.Unit file examples
Following sample unit files can be saved in the home directory (
~/.config/systemd/user
) and used withsystemctl --user
parameter.Services described by those unit files don't do anything, but the
RemainAfterExit=yes
forces systemd to assume that the service is still running, even though the/bin/true
command does not hang around after it is executed.etc-service.service
eth-server.service
Enable the
eth-service.service
module withsystemctl --user enable eth-service.service
.Following is the output of tests with starting/stopping services:
Initial State
System is returned into initial state before commands in following two sections are executed.
systemctl --user start eth-service.service
Both
server
andservice
are started (ignore theexited
lines: both server and service are executions of /bin/true, which exits once it is finished, but the unit files are written so that systemd will think services are running until they are stopped)systemctl --user start eth-server.service
Both services are started:
Commands in following two sections are executed where both services are running:
systemctl --user stop eth-server.service
You can see both services were stopped
systemctl --user stop eth-service
You can see that
eth-service
has been stopped (Active: inactive (dead)
) but theeth-server
is still "running" (Active: active
)The status command for
etc-service
produced more output compared to other cases, I'm assuming because the "transaction" hasn't finished yet (etc-server
is still running). The extra lines fromjournalctl
show that as far as systemd is concerned one of the services has been stopped, and the other is still running.PS according to the systemd homepage it is spelled systemd, not SystemD (just being pedantic)