How to execute commands during shutdown on Linux

0
256

Linux and Unix frameworks have long made it entirely simple to run a charge on boot. Simply add your order to /etc/rc.local and away you go. In any case, things being what they are,running a summon on shutdown is somewhat more muddled.

When do you need to run a summon as the PC close down? Maybe you need to de-enroll a machine or service from a database. Possibly you need to duplicate information from an unstable volatile storage framework to a permanent storage area.  You may just need to your PC to post “#Good Bye, I am shutting down!” on its Twitter account before it close down?

What we looking as a solution is to make the process simple and alleged “one-shot” changes rather than halting a daemon. It’s enticing to consider them the same way, however. In case you’re comfortable with SysVinit you may think, “Goodness, I’ll simply make a killing script content.” For instance, /etc/rc.d/rc3.d/K99runmycommandatshutdown should to get called when your PC exits runlevel 3. All things considered, that is the ticket the contents in /etc/init.d/ get ceased.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<span class="co0">#!/bin/sh</span>
<span class="co0"># chkconfig: 2345 20 80</span>
<span class="co0"># description: An example init script to run a command at shutdown</span>

<span class="co0"># runmycommandatshutdown runs a command at shutdown. Very creative.</span>

<span class="re2">LOCKFILE</span>=<span class="sy0">/</span>var<span class="sy0">/</span>lock<span class="sy0">/</span>subsys<span class="sy0">/</span>
start<span class="br0">(</span><span class="br0">)</span><span class="br0">{</span>
<span class="co0"># Touch our lock file so that stopping will work correctly</span>
<span class="kw2">touch</span> <span class="co1">${LOCKFILE}</span>
<span class="br0">}</span>

stop<span class="br0">(</span><span class="br0">)</span><span class="br0">{</span>
<span class="co0"># Remove our lock file</span>
<span class="kw2">rm</span> <span class="co1">${LOCKFILE}</span>
<span class="co0"># Run that command that we wanted to run</span>
mycommand
<span class="br0">}</span>

<span class="kw1">case</span> <span class="st0">"$1"</span> <span class="kw1">in</span>
start<span class="br0">)</span> start<span class="sy0">;;</span>
stop<span class="br0">)</span> stop<span class="sy0">;;</span>
<span class="sy0">*</span><span class="br0">)</span>
<span class="kw3">echo</span> $<span class="st0">"Usage: $0 {start|stop}"</span>
<span class="kw3">exit</span> <span class="nu0">1</span>
<span class="kw1">esac</span>
<span class="kw3">exit</span> <span class="nu0">0</span>

systemd

All of that is great, but what if you’re working on a Linux distribution which uses systemd and there is no SysVinit? Let’s see the point now. But it is much simpler with systemd. All you have to do is put your code or script in

1
/usr/lib/systemd/system-shutdown/

, which is handled by systemd-halt.service. Of course, if you need to manage dependencies in a particular order (e.g., you can’t post a tweet if the network stack is down), then you can write a systemd service unit file. For example:

[Unit]
Description=Run mycommand at shutdown
Requires=network.target
DefaultDependencies=no
Before=shutdown.target reboot.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=mycommand

[Install]
WantedBy=multi-user.target

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here