systemd restart service on resume

Greg Woods woods at ucar.edu
Sun Dec 8 17:14:25 UTC 2013


I am trying to find a way to get a service to restart when the system is
resumed from suspend or hibernation. I can get something to run when the
system is put to sleep by using sleep.target (for example, I have an
"ssh-suspend" service that kills all the ssh sessions because they are
always hung after a resume and I have to close the windows manually
anyway), but I haven't figured out the exact incantation to get a
service to restart on resume. Saying "After=sleep.target" has not
worked, and there doesn't seem to be anything like a resume.target or
wakeup.target . Here's an example service file:

[Unit]
Description=Restart IPSEC tunnel on resume
After=sleep.target NetworkManager-wait-online.service

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart racoon
RemainAfterExit=yes

[Install]
WantedBy=sleep.target

(I have tried it both with and without the Install section). 

This service never runs:

# systemctl status racoon-resume.service
racoon-resume.service - Restart IPSEC tunnel on resume
   Loaded: loaded (/etc/systemd/system/racoon-resume.service; enabled)
   Active: active (exited) since Sat 2013-11-23 18:29:48 MST; 2 weeks 0
days ago
 Main PID: 2605 (code=exited, status=0/SUCCESS)
   CGroup: name=systemd:/system/racoon-resume.service

Nov 23 18:29:48 cobweb.gregandeva.net systemd[1]: Starting Restart IPSEC
tunnel on resume...
Nov 23 18:29:48 cobweb.gregandeva.net systemd[1]: Started Restart IPSEC
tunnel on resume.

That was from when I tested it manually. So how to I program it to
restart a service upon resume?

The real problem is that the racoon daemon (which negotiates IKE keys
for an IPSEC tunnel) stops working after a suspend/resume, and needs to
be restarted on resume in order for the tunnel to work again.

I have this service file that starts and stops the tunnel at
boot/shutdown time, and this works fine:

[Unit]
Description=IPSEC Tunnel Service
After=syslog.target
After=network.target

[Service]
Type=forking
ExecStart=/local/etc/racoon.rc
ExecStop=/usr/bin/pkill racoon
ExecStopPost=/usr/sbin/setkey -FP
PIDFile=/var/run/racoon.pid

[Install]
WantedBy=multi-user.target
Alias=racoon.service


--Greg



More information about the users mailing list