You are not logged in.

#1 2015-01-12 18:11:22

fkol-k4
Member
From: Greece
Registered: 2013-12-25
Posts: 9
Website

[Solved] systemd unit that runs a script on shutdown (syntax help)

Hi guys!
I need to run a a small bash script on shutdown - reboot.
The script is a simple "take that text file, cut this piece, save it there" job, only it needs to be done while "/mnt/some_partition" is still mounted (because that's where it will be saved).

Searching in systemd.unit and systemd.service documentation as well as other sources (arch wiki, ubuntu wiki, some stack exchange guides) i found a lot of information/examples on how to do $TASK_X on system startup but nothing on how to do something on reboot/shutdown.

Not being a developer doesn't really help to understand enough of systemd's documentation either, so all i have come up so far is this unit file:

# custom systemd unit file
# file: /etc/systemd/system/mycustomservice.service
#

[Unit]
Description=A_brief_description
RequiresMountsFor=/mnt/A, /mnt/B
Before=shutdown.target

[Service]
Type=simple
User=system
Execstart=/bin/bash /usr/local/bin/my_custom_folder/my_script

[Install]
WantedBy=reboot.target shutdown.target poweroff.target halt.target

but it doesn't work, i 'm still missing something here (i have already of course run systemctl enable on it and it seemed to work, it did the necessary links to the targets).

For now i 'm testing it in a VM so i can reboot it often enough (change a thing - reboot - check again - change something else - reboot) but i can't seem to get it to work.

For the purpose that knowing the exact thing i'm trying to do might be helpful, i'm trying to "port" this Upstart job from Ubuntu-Upstart to Arch-systemd (in the test VM, the script has only a "touch" command, just to test if it works). Up until now, i'm doing it via a custom updates bash script (this one), but i want to separate the two actions and leave just the updates to the script.

Thanks in advance guys and have a great year!
Any suggestions or links to relevant documentation (preferably by examples) would be greatly appreciated.

Last edited by fkol-k4 (2015-01-12 19:37:10)

Offline

#2 2015-01-12 18:56:21

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [Solved] systemd unit that runs a script on shutdown (syntax help)

My first thought would be to use a 'normal' service, with the script run in ExecStop like this.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2015-01-12 19:36:47

fkol-k4
Member
From: Greece
Registered: 2013-12-25
Posts: 9
Website

Re: [Solved] systemd unit that runs a script on shutdown (syntax help)

Thank you very much. The following did it after all:

[Unit]
Description=My_Description

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /usr/local/bin/my_custom_folder/my_script

[Install]
WantedBy=multi-user.target

I would have never though to put "WantedBy=multi-user.target" in a script that i want for shutdown/reboot.

Thank you for your help!

Offline

Board footer

Powered by FluxBB