Why `systemctl stop service` can't invoke the service?
This is explained in detail in the systemd service documentation, but you pretty much need to read all of it to understand what’s going on. The most pertinent part in this case is example 3; from that, the reader can gather that a oneshot service as you’ve declared it never becomes active, so its stop action will be run once its start action completes.
To achieve what you’re after, you need a oneshot service which nevertheless becomes active:
[Unit]
Description=delete file
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=/bin/bash /home/mytest.sh
[Install]
WantedBy=multi-user.target
Related videos on Youtube
Comments
-
showkey over 1 year
vim /home/mytest.sh rm -f /home/mytest/*
I want to write a service to execute the remove action.
My expections:
sudo systemctl stop mytest
can delete files in /home/mytest
sudo systemctl start mytest
do nothing.Edit my service file.
sudo vim /etc/systemd/system/mytest.service [Unit] Description=delete file [Service] Type=oneshot ExecStart=/bin/true ExecStop=/bin/bash /home/mytest.sh [Install] WantedBy=multi-user.target
Enable it.
sudo systemctl enable mytest
Now i found a strange action for mytest service.
sudo systemctl start mytest
can delete files in /home/mytest
sudo systemctl stop mytest
do nothing.Why? Pleas give a explanation in detail.