Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2134 :: Shutdown Sequence Systemd

Klaatu demonstrates how to sequence systemd shutdown processes

<< First, < Previous, , Latest >>

Hosted by Klaatu on Thursday, 2016-10-06 is flagged as Explicit and is released under a CC-BY-SA license.
systemd, service. 2.
The show is available on the Internet Archive at: https://archive.org/details/hpr2134

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:14:48

general.

Set up a service to trigger FIRST (this would be the shutdown service):

# cat /lib/systemd/system/fakehalt.service

[Unit]
Description=Fake-Halt Service
After=fakevm.service
Requires=fakevm.service

[Service]
Type=simple
ExecStart=/usr/local/bin/fakehalt.sh #this will fail until fakevm succeeds
ExecReload=/usr/local/bin/fakehalt.sh

And then set up the one that you want to run and complete BEFORE shutdown is permitted:

# cat /lib/systemd/system/fakevm.service
[Unit]
Description=Fake Service
Before=fakehalt.service

[Service]
Type=simple
ExecStart=/usr/local/bin/fake.sh

Create a script to represent the VM shutdown (or any process that you cannot anticipate the duration of)

# cat /usr/local/bin/fake.sh
#!/bin/sh

test="1"
sleep 21
if [ X"$test" = "X1" ]; then
    echo "vm has shut down" > /tmp/fake.test
    exit 0
else 
    exit 1
fi

And a script to pass for a shutdown signal:

# cat /usr/local/bin/fakehalt.sh
#!/bin/sh

sleep 3
cat /tmp/vmfake.test > /tmp/haltfake.test

Start the service you want to happen AFTER the first one:

# systemctl start fakehalt

What "should" happen is that fakehalt will fail to find a file called /tmp/fake.test to cat from, and so everything should go horribly wrong.

What actually happens is that systemd places fakehalt service on hold until it gets an exit 0 signal from the fake service. So if you wait 21 seconds and cat /tmp/fakehalt.test, you see that the cat from a file that did not exist when fakehalt was started - actually succeeded.


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2016-12-18 20:43:23 by CPrompt^

Great explanation!

Great explanation of how systemd works with two services like this.

I started working with systemd services and went back to this show to get some info. Good stuff!

Just wanted to point out that in the show notes, there is a little bit of a typo.

Under the "fakehalt.service" you have listed in the Unit section :
After=fakevm.service
Requires=fakevm.service

However, right below that you call the service fake.service

You say it correct in the audio but the show notes have the typo.

Thanks!
C:\

Comment #2 posted on 2018-10-07 16:59:58 by david pellecchia

systemd service

Top Man!
Many thanks for posting your information regarding systemd services. I've been pulling my hair out trying to work out why my pre-shutdown script would not fire. Then I found your post.
A very big thank you to you.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?