[Solved] Run a script at shutdown

Forum rules
Before you post please read how to get help
Post Reply
User avatar
cabbagetreemo
Level 1
Level 1
Posts: 49
Joined: Fri Mar 31, 2017 2:36 am
Location: Spain

[Solved] Run a script at shutdown

Post by cabbagetreemo » Fri Jun 15, 2018 8:42 am

I want to run a fairly simple script at shutdown. I have a script running at startup which opens Thunderbird in Kdocker so that my Google calendar gets synced. Sometimes systemd is starting a 90 second count down when I shutdown and I suspect it is because either kdocker or thunderbird is not getting closed properly (since I always close all windows before shutting down) so the script I want to run is

Code: Select all

#!/bin/bash
#Kills thunderbird to prevent 90 s timer from systemd at shutdown
killall kdocker
killall thunderbird
I have made the script executable with chmod u=rwx /home/craig/bin/killTB and I have tried two methods to get it to run at shutdown.

First I simply tried making a symbolic link to /etc/rc0.d/ but that didn't work and I think that might be the pre systemd method

So I tried creating a filed called /etc/systemd/system/shutdownTB.service

Where the contents is

Code: Select all

[unit]
Description=Kill Thunderbird on shutdown

[service]
Type=ones
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/craig/bin/killTB
user=nobody

[Install]
WantedBy=multi-user.target
And I used systemctl enable shutdownTB to try to make it 'work' but when I type systemctl status shutdownTB I get

Code: Select all

● shutdownTB.service
   Loaded: error (Reason: Invalid argument)
   Active: inactive (dead)

Jun 16 00:37:40 craig-ThinkPad-T530 systemd[1]: [/etc/systemd/system/shutdownTB.service:1] Unknown section 'unit'. Ignoring.
Jun 16 00:37:40 craig-ThinkPad-T530 systemd[1]: [/etc/systemd/system/shutdownTB.service:4] Unknown section 'service'. Ignoring.
Jun 16 00:37:40 craig-ThinkPad-T530 systemd[1]: shutdownTB.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
I don't know what that means since I have both ExecStart and ExecStart in my file.

Here is the output of inxi -Fxz

Code: Select all

System:    Host: craig-ThinkPad-T530 Kernel: 4.13.0-41-generic x86_64 (64 bit gcc: 5.4.0)
           Desktop: Cinnamon 3.4.6 (Gtk 3.18.9-1ubuntu3.3) Distro: Linux Mint 18.2 Sonya
Machine:   System: LENOVO (portable) product: 2359CTO v: ThinkPad T530
           Mobo: LENOVO model: 2359CTO Bios: LENOVO v: G4ET97WW (2.57 ) date: 10/17/2013
CPU:       Dual core Intel Core i5-3230M (-HT-MCP-) cache: 3072 KB
           flags: (lm nx sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx) bmips: 10375
           clock speeds: max: 3200 MHz 1: 2864 MHz 2: 2860 MHz 3: 2619 MHz 4: 2877 MHz
Graphics:  Card: Intel 3rd Gen Core processor Graphics Controller bus-ID: 00:02.0
           Display Server: X.Org 1.18.4 drivers: intel (unloaded: fbdev,vesa) Resolution: 1366x768@60.00hz
           GLX Renderer: Mesa DRI Intel Ivybridge Mobile GLX Version: 3.0 Mesa 12.0.6 Direct Rendering: Yes
Audio:     Card Intel 7 Series/C210 Series Family High Definition Audio Controller
           driver: snd_hda_intel bus-ID: 00:1b.0
           Sound: Advanced Linux Sound Architecture v: k4.13.0-41-generic
Network:   Card-1: Intel 82579LM Gigabit Network Connection driver: e1000e v: 3.2.6-k port: 6080 bus-ID: 00:19.0
           IF: enp0s25 state: down mac: <filter>
           Card-2: Realtek RTL8188CE 802.11b/g/n WiFi Adapter driver: rtl8192ce port: 4000 bus-ID: 03:00.0
           IF: wlp3s0 state: up mac: <filter>
Drives:    HDD Total Size: 500.1GB (6.2% used) ID-1: /dev/sda model: CT500MX500SSD1 size: 500.1GB
Partition: ID-1: / size: 89G used: 22G (26%) fs: ext4 dev: /dev/sda5
           ID-2: swap-1 size: 8.27GB used: 0.00GB (0%) fs: swap dev: /dev/sda6
RAID:      No RAID devices: /proc/mdstat, md_mod kernel module present
Sensors:   System Temperatures: cpu: 50.0C mobo: N/A
           Fan Speeds (in rpm): cpu: 1852
Info:      Processes: 201 Uptime: 14 min Memory: 1282.0/7677.8MB Init: systemd runlevel: 5 Gcc sys: 5.4.0
           Client: Shell (bash 4.3.481) inxi: 2.2.35 
Last edited by cabbagetreemo on Sun Jun 17, 2018 5:17 am, edited 1 time in total.

User avatar
trytip
Level 7
Level 7
Posts: 1844
Joined: Tue Jul 05, 2016 1:20 pm

Re: Run a script at shutdown

Post by trytip » Fri Jun 15, 2018 9:32 am

why not add the shut down command at the end of the script, and instead of shutting down and worrying about script starting you can just run the script which will kill whatever you want and then shut down.
Image

User avatar
thx-1138
Level 6
Level 6
Posts: 1050
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: Run a script at shutdown

Post by thx-1138 » Fri Jun 15, 2018 9:55 am

...note that there is at least one typo above, it should read Type=oneshot (not 'ones').

User avatar
Flemur
Level 15
Level 15
Posts: 5919
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Run a script at shutdown

Post by Flemur » Fri Jun 15, 2018 10:35 am

cabbagetreemo wrote:
Fri Jun 15, 2018 8:42 am
Desktop: Cinnamon 3.4.6 (Gtk 3.18.9-1ubuntu3.3) Distro: Linux Mint 18.2 Sonya
+1 for trytips' idea - I'm not sure if you can replace the regular shutdown in the Cinnamon GUI; in fluxbox it's easy, I just have it run my "~/bin/Shutdown" script, which has the actual system shutdown at the end of it.

If you can't change the regular "shutdown" in the Cinn GUI (a shame if true), to shut down run your script with "shutdown now" at the end of it (or "/bin/systemctl poweroff -i"), you could just make another desktop-icon/menu entry with your custom shutdown.

You might have to make it possible for non-root to shutdown:

Code: Select all

sudo  chmod u+s /sbin/shutdown
The "recommended" method is more complicated, of course:
http://how-to.wikia.com/wiki/How_to_all ... r_in_Linux
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

ugly
Level 3
Level 3
Posts: 156
Joined: Thu Nov 24, 2016 9:17 pm

Re: Run a script at shutdown

Post by ugly » Fri Jun 15, 2018 5:56 pm

In the systemd service files that I have working, I have

[Unit]
[Service]
[Install]

Note that they are all capitalized. In yours, [Install] is capitalized, but you have [unit] and [service] in all lowercase. Perhaps that's why you are getting unknown section errors for unit and service but not install.

User avatar
cabbagetreemo
Level 1
Level 1
Posts: 49
Joined: Fri Mar 31, 2017 2:36 am
Location: Spain

Re: Run a script at shutdown

Post by cabbagetreemo » Sat Jun 16, 2018 5:48 am

@ trytip and @ Flemur, that's probably a good idea but I like to make things more 'foolproof' i.e. I don't want to use the regular shutdown command instead of running my script one day out of habit and end up with a 90 s delay - plus doing it with systemd seems like an opportunity to learn something.

@ thx-1138 Good spot, but in my actual file it does say oneshot, I must have accidentally deleted part of the line after copy pasting

@ ugly that helped but now I have a different error after running systemctl daemon-reload
systemctl status shutdownTB gives

Code: Select all

● shutdownTB.service - Kill Thunderbird on shutdown
   Loaded: loaded (/etc/systemd/system/shutdownTB.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

Jun 16 21:36:10 craig-ThinkPad-T530 systemd[1]: [/etc/systemd/system/shutdownTB.service:9] Unknown lvalue 'user' in section 'Service'
I tried making a capital U on user and changing from nobody to my username but that made no difference. Google tells me that the user field is definitely supposed to be in the [Service] section...

User avatar
cabbagetreemo
Level 1
Level 1
Posts: 49
Joined: Fri Mar 31, 2017 2:36 am
Location: Spain

Re: Run a script at shutdown

Post by cabbagetreemo » Sat Jun 16, 2018 5:54 am

Actually, after running systemctl start shutdownTB I get

Code: Select all

● shutdownTB.service - Kill Thunderbird on shutdown
   Loaded: loaded (/etc/systemd/system/shutdownTB.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sat 2018-06-16 21:52:49 NZST; 2s ago
  Process: 4617 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 4617 (code=exited, status=0/SUCCESS)

Jun 16 21:52:49 craig-ThinkPad-T530 systemd[1]: Starting Kill Thunderbird on shutdown...
Jun 16 21:52:49 craig-ThinkPad-T530 systemd[1]: Started Kill Thunderbird on shutdown.
so I guess that means it is set up?

User avatar
AndyMH
Level 4
Level 4
Posts: 486
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Run a script at shutdown

Post by AndyMH » Sat Jun 16, 2018 12:35 pm

Been here and wasted a lot of time - wanted to run a backup script on shutdown. Did a lot of searching on the web on systemd and found that you couldn't control what processes shut down in what order. In essence, my backup script could have been run after my drives had been unmounted - useless.

In the end I did what was suggested above - added a shutdown command to the end of my backup script and stuck a launcher to it on the desktop. Much simpler than trying to mess with systemd.
Thinkpad T430 i7-3632 Cinnamon 18.3, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

User avatar
cabbagetreemo
Level 1
Level 1
Posts: 49
Joined: Fri Mar 31, 2017 2:36 am
Location: Spain

Re: Run a script at shutdown

Post by cabbagetreemo » Sat Jun 16, 2018 9:58 pm

As I understand the scripts in the /etc/systemd/system/ folder are run in ASCIIbetical order so you can force scripts t be run first by prepending zeros to the file name.

User avatar
AndyMH
Level 4
Level 4
Posts: 486
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Run a script at shutdown

Post by AndyMH » Sun Jun 17, 2018 4:46 am

Now, that's useful information.
Thinkpad T430 i7-3632 Cinnamon 18.3, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

Post Reply

Return to “Scripts & Bash”