I'm not an expert on systemd, but I google better than the average bear. See https://wiki.archlinux.org/index.php/Power_management:
There is just one thing systemd cannot do (as of systemd-204): power management depending on whether the system is running on AC or battery. To fill this gap, you can create a single udev rule that runs a script when the AC adapter is plugged and unplugged:
Further instructions follow.
My guess is you want to do something like create a fake service called acpluggedin.service
which is started and stopped by the udev rule, and then have the mongo and redis services Requires
it. Or something.
I've come upon an answer to my own question.
I read in the systemd.service man page about the "Type=
" clause, and I see that when I add "Type=oneshot
" to the [Service]
section of my *.service
files, systemd does what I wanted.
I expect I could use other Type=
settings and get results to my liking, and I also expect that I probably don't actually need to make second.service
oneshot to get what I want; first.service
is the one I need to see finish. But I have a path to success now.
So, for the record, here are the working *.service files and the journalctl output to prove it.
Onward!
====
First.service:
[Unit]
Description=First of two services
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/first.sh
Type=oneshot
[Install]
WantedBy=multi-user.target
Second.service:
[Unit]
Description=Second of two services
After=first.service
[Service]
ExecStart=/home/steve/play/systemd/oneAfterTheOther/second.sh
Type=oneshot
[Install]
WantedBy=multi-user.target
Journalctl output:
$ journalctl -u first -u second
-- Logs begin at Wed 2018-09-05 11:46:04 CDT, end at Wed 2018-09-05 11:51:54 CDT
Sep 05 11:46:21 sk-xenial-vm systemd[1]: Starting First of two services...
Sep 05 11:46:22 sk-xenial-vm first.sh[868]: sleep for 10 seconds
Sep 05 11:46:32 sk-xenial-vm first.sh[868]: ...and we're out
Sep 05 11:46:32 sk-xenial-vm systemd[1]: Started First of two services.
Sep 05 11:46:32 sk-xenial-vm systemd[1]: Starting Second of two services...
Sep 05 11:46:32 sk-xenial-vm second.sh[1104]: sleep for 2 seconds
Sep 05 11:46:34 sk-xenial-vm second.sh[1104]: ...and we're out
Sep 05 11:46:34 sk-xenial-vm systemd[1]: Started Second of two services.
Best Answer
I think the command you are looking for is:
From
man systemctl
: