View previous topic :: View next topic |
Author |
Message |
theblackraven n00b
Joined: 12 Oct 2005 Posts: 17
|
Posted: Sun Mar 15, 2015 4:02 pm Post subject: systemd shutdown script |
|
|
Hi,
i tried to write a shutdown service for my virtual-machines, but it won't work.
i created a win7.service:
Code: |
[Unit]
Description=Start an Shutdown Virtual Machines
ConditionPathExists=/usr/bin/shutdown-vm
After=gdm.service
Before=shutdown.target reboot.target halt.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/bin/start-win7
ExecStop=/usr/bin/shutdown-vm
[Install]
WantedBy=multi-user.target
|
and this is the shutdown-vm script:
Code: |
#!/bin/bash
vmrunning=`virsh list |grep running`
i=1
while [ "x$vmrunning" != "x" ]
do
echo "$vmrunning"
echo "$i"
virsh shutdown win7
virsh shutdown ubuntu12.04
sleep 5
vmrunning=`virsh list |grep running`
let i=$i+1
if [ $i == 20 ]
then
break
fi
done
|
if i run "systemctl stop win7" the script works fine.
but if i enable this service or start this service manually the script won't work or isn't execute by systemd.
What i have to change in the win7.service that this scripts runs properly?
Btw:
the script doesn't start the virtual machines on startup, but works when i logged in gnome. how can i change that service, that it will execute start-win7 after i logged in gnome? |
|
Back to top |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6749
|
Posted: Sun Mar 15, 2015 4:25 pm Post subject: |
|
|
Remove the "Before=" line. If you need to stop in a particular phase, use "Conflicts=", but this should normally not be necessary. |
|
Back to top |
|
|
theblackraven n00b
Joined: 12 Oct 2005 Posts: 17
|
Posted: Sun Mar 22, 2015 11:38 am Post subject: |
|
|
sorry it still doesn't work..
Code: |
[Unit]
Description=Shutdown Virtual Machines
[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/bin/shutdown-vm
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
|
|
|
Back to top |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6749
|
Posted: Sun Mar 22, 2015 12:05 pm Post subject: |
|
|
Now you removed also the "After=". This way you have no control when the start/shutdown will happen: It can start and stop anytime, even before/after basic things like local mounting or network connection are established/finished. Before, you had there gdm.service which presumably depends on a lot of things. A minimal thing which you should put in "After=" is probably local-fs.target and any services which "virsh" needs to be properly working (e.g. network connection?). |
|
Back to top |
|
|
theblackraven n00b
Joined: 12 Oct 2005 Posts: 17
|
Posted: Sun Mar 22, 2015 3:59 pm Post subject: |
|
|
Ok,
i only want a unit file like /etc/conf.d/local.stop.
So the ExecStart=/bin/true is only a "dummy"
But the ExecStop script should run first, when shutting down or rebooting the system. |
|
Back to top |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6749
|
Posted: Sun Mar 22, 2015 8:09 pm Post subject: |
|
|
man systemd.unit wrote: | Before=, After=
[...] Note that when two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied. i.e. if a unit is configured with After= on another unit, the former is stopped before the latter if both are shut down. |
|
|
Back to top |
|
|
|