Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
systemd shutdown script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
theblackraven
n00b
n00b


Joined: 12 Oct 2005
Posts: 17

PostPosted: Sun Mar 15, 2015 4:02 pm    Post subject: systemd shutdown script Reply with quote

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
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6749

PostPosted: Sun Mar 15, 2015 4:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
theblackraven
n00b
n00b


Joined: 12 Oct 2005
Posts: 17

PostPosted: Sun Mar 22, 2015 11:38 am    Post subject: Reply with quote

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
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6749

PostPosted: Sun Mar 22, 2015 12:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
theblackraven
n00b
n00b


Joined: 12 Oct 2005
Posts: 17

PostPosted: Sun Mar 22, 2015 3:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6749

PostPosted: Sun Mar 22, 2015 8:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum