How best to run a user script at shutdown?

Questions about applications and software
Forum rules
Before you post please read how to get help
Post Reply
Hansl
Level 3
Level 3
Posts: 103
Joined: Fri Dec 09, 2011 3:50 pm

How best to run a user script at shutdown?

Post by Hansl » Fri Sep 28, 2018 6:07 am

What would be the best way to run a script in userspace at shutdown?

I'd like to reliably synchronize mail folders and profiles from the local disc to a server when the user shuts down its LMDE3/Cinnamon machine, like so:

Code: Select all

rsync -vahz --delete /home/<user>/eMail/   <user>@server:eMail/
rsync -vahz --delete /home/<user>/Profiles/    <user>@server:Profiles/ 
where the server runs rsync and is connected via ssh/public key.
Since these are gigabytes the script will take several seconds, so the shutdown must wait for it to complete before closing the network or unmounting the disks.
Plus, for ssh, it needs to run as <user>, not as root. 
Plus, Thunderbird and Firefox should have shut down correctly before sync'ing.

Currently I have these directories as Samba shares directly on the same server, but it is slow and gives some troubles with Thunderbird.  Therefore I'd prefer to have them local and sync at the end of the day.  I've tinkered with init.d scripts but without success so far.

Thanks!

axisofevil
Level 2
Level 2
Posts: 84
Joined: Mon Nov 14, 2011 12:22 pm

Re: How best to run a user script at shutdown?

Post by axisofevil » Fri Sep 28, 2018 9:12 am

There might not be a way to do this.
Shutdown is going to be terminating processes as quickly as possible - not starting new ones.

One possibility would be to do a specially controlled shutdown which runs a script that does the rsync then issues a shutdown.
You would have to give the script SUID permissions to allow it to issue the real shutdown, which you might not want to do.

Another would be to issue periodic syncs via cron, so that you are mostly synchronised...
Rsync is very fast, especially for large numbers of small files.

User avatar
catweazel
Level 17
Level 17
Posts: 7751
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: How best to run a user script at shutdown?

Post by catweazel » Fri Sep 28, 2018 9:32 am

Hansl wrote:
Fri Sep 28, 2018 6:07 am
What would be the best way to run a script in userspace at shutdown?
Use systemd.

https://www.freedesktop.org/software/sy ... rvice.html

In debian derivatives, the directory you want to put your script in is /lib/systemd/system-shutdown/ however these scripts, if any, are executed at the end of the shutdown process so you'll have to remount your drives if you need to write to them as they're read-only at this point. Perhaps an easier method using systemd is this: viewtopic.php?f=42&t=275464

Your service definition will need these parameters:

Code: Select all

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/<name_of_your_script>

[Install]
WantedBy=multi-user.target
The service definition should be saved in /lib/systemd/system and not in /etc/systemd/system/ as described in the linked article.
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

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

Re: How best to run a user script at shutdown?

Post by AndyMH » Fri Sep 28, 2018 1:37 pm

When I looked at this some time back I ended up coming to the conclusion that it was all too difficult. I don't think you can control which processes shut down it what order (could be wrong :) ). So you might find services you need have already terminated by the time your script runs. I wanted to run a rsync script backing up any changes to /home.

It the end, the simplest thing to do was put a shutdown command at the end of the script - job done!
Homebrew i5-8400+GTX1080 Cinnamon 19, Thinkpad T430 i7-3632 Cinnamon 19, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

Hansl
Level 3
Level 3
Posts: 103
Joined: Fri Dec 09, 2011 3:50 pm

Re: How best to run a user script at shutdown?

Post by Hansl » Sat Sep 29, 2018 4:06 am

Hmm, that all sounds quite convoluted.  Maybe I should not look at Linux's shutdown but at Cinnamon (Mate) logout?
Is there a way to hook into the desktop's shutdown (and delay it as necessary), before it finally calls system shutdown? When the user clicks the DE's button “Shutdown” (or logout)?

For myself, I use a script I manually call before logging off.
For others, I tried a script for running TB like —

Code: Select all

thunderbird && rsync … 
but that relies on the user to correctly close TB before DE shutdown which is not a given (and for rsync to be done in time).
In former times, in Windows XP, I did use group policies to execute (and wait for) scripts at either logout or shutdown which worked just fine.

Hansl
Level 3
Level 3
Posts: 103
Joined: Fri Dec 09, 2011 3:50 pm

Re: How best to run a user script at shutdown?

Post by Hansl » Sat Sep 29, 2018 4:24 am

@catweazel — right, I was still thinking SysV, I'm not used to SystemD yet.  Will look into that.
Since I plan on rsync, the Samba shares on the server won't be used and may be unmounted; the local drive need only be readable, network must still be active (was connected via user's NetworkManager).
But I need to run rsync as <user> for its ssh pubkey connect, root can't connect to the server. 
How do I know in SystemD which user just logged out? (for generality; at the moment there's just one fixed user per machine)

Post Reply

Return to “Software & Applications”