You are not logged in.

#1 2021-04-22 17:54:00

Harey
Member
From: Bavaria, Germany
Registered: 2007-03-24
Posts: 374

execute Systemd --user scripts on login and logout

Good evening,

I am currently struggling with systemd --user. Maybe someone can give me a hint. Every idea is welcome - I am totally lost by now sad

Problem: My users mout some shares from a samba server during login via pam_mount. This is working well.
One of the shares is a backup share where I stored roaming profiles for firefox and thunderbird. While Firefox is working well with a profile on a network share, thunderbird from time to time looses its brain and acts as if the profile is damaged. Only remedy is to restore the profile from a working backup from the day before.

So I tried to to work with a local profile that is rsynced from the server when the user logs in. This is done via a systemd --user file:

[Unit]
Description=Sync userdirs with network target
Before=umount.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/bin/restore_dirs_on_user_login.sh %u
ExecStop=/usr/bin/backup_dirs_on_user_logout.sh %u

[Install]
WantedBy=default.target

So far - so good. The sync on login is working. But when the user logs out, systemd unmounts the network shares before the ExecStop script is processed hence not syncing. I thought the 'Before=umount.target' would move the ExecStop script before unmounting. But it does not.

How can I accomplish that?

Greetings
Harvey


Linux is like a wigwam: No Gates, no Windows and an Apache inside

Offline

#2 2021-04-22 23:30:01

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,596

Re: execute Systemd --user scripts on login and logout

Harey wrote:

So far - so good. The sync on login is working. But when the user logs out, systemd unmounts the network shares before the ExecStop script is processed hence not syncing. I thought the 'Before=umount.target' would move the ExecStop script before unmounting. But it does not.

I do think this about the unit starting, the service will start before 'umount.target' is started.

while Before= ensures that the configured unit is started before the listed unit begins starting up, After= ensures.....

(see)

man -P 'less -p Those\ \two\ \settings\ \configure\ \ordering\ \depenencies\ \between\ \units' systemd.unit

What I think you're looking for is ExecStopPost.

Additional commands that are executed after the service is stopped. This includes cases where the
commands configured in ExecStop= were used, where the service does not have any ExecStop= defined, or
where the service exited unexpectedly.

(see)

man -P 'less -p Additional\ \commands\ \that\ \are\ \executed\ \after\ \the\ \service\ \is\ \stopped' systemd.service

Which (hopefully) execute your command after the service stopped / exited unexpectedly.

I could be wrong it's sometimes hard to interpret the effects and I have no service I can test it on but it may be worth trying.

Offline

#3 2021-04-23 09:03:24

Harey
Member
From: Bavaria, Germany
Registered: 2007-03-24
Posts: 374

Re: execute Systemd --user scripts on login and logout

qinohe,

First of all, thank you for helping smile

What I think you're looking for is ExecStopPost.
Which (hopefully) execute your command after the service stopped / exited unexpectedly.

Yes, but I need my service to execute its ExecStop directive before pam_mount does unmount my samba shares, not after. So basically I need to tell the umount to wait for my service to stop successfully.

I have this message in my journalctl -b output

systemd[627]: home-harvey-server\x2dhome.mount: Deactivated successfully.

I need to know which service causes this because it is executed earlier than my own backup service...

Harvey

Last edited by Harey (2021-04-23 10:22:17)


Linux is like a wigwam: No Gates, no Windows and an Apache inside

Offline

#4 2021-04-23 13:40:11

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,596

Re: execute Systemd --user scripts on login and logout

Already thought it wouldn't be sufficient...

I have no real idea what gives the message, but isn't this pam_mount itself notifying you?

Anyway, if you can't get the service to work for you, maybe your WM/DE can, that is if you use one...
Use the logout command to do the job for you.
I run i3, so, this is just an example.

sh /usr/bin/backup_dirs_on_user_logout.sh && i3-msg exit

Offline

#5 2021-04-24 13:54:44

Harey
Member
From: Bavaria, Germany
Registered: 2007-03-24
Posts: 374

Re: execute Systemd --user scripts on login and logout

qinohe wrote:

I have no real idea what gives the message, but isn't this pam_mount itself notifying you?

Well, it is even more complicated. There would be a method to let pam_mount execute my script before really unmounting the samba shares. There is an excellent article here that describes how to accomplish this. I managed to adapt this for my usecase. But... it only works if the user logs out of his session. If the system is shut down from the DE systemd manages to unmount the samba shares without execution of the script. In fact this was my first attempt to solve my problem because it would be most elegant and feels like the right place sad

qinohe wrote:

Anyway, if you can't get the service to work for you, maybe your WM/DE can, that is if you use one...
Use the logout command to do the job for you.

Same as above. On shutdown the shares vanish before the script executes. I'm f**d up here.

I have to find a way to stop systemd from unmounting the shares too early.


Linux is like a wigwam: No Gates, no Windows and an Apache inside

Offline

#6 2021-04-24 14:56:51

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,453

Re: execute Systemd --user scripts on login and logout

There is quite a bit of "Talk" on the pam_mount Wiki Talk page regarding automatic unmounting and systemd. Some of it is old and could be outdated, of course. Not sure if there's anything helpful on that Talk page.

A poor man's fallback solution could be to rsync the profile frequently, using a systemd timer, not just on logout.

Btw: how do you know on logout that the profile wasn't corrupted and now you've backed-up a corrupted profile?

Offline

#7 2021-04-24 16:52:44

Harey
Member
From: Bavaria, Germany
Registered: 2007-03-24
Posts: 374

Re: execute Systemd --user scripts on login and logout

twelveeighty wrote:

There is quite a bit of "Talk" on the pam_mount Wiki Talk

Yeah, seen that but it was of no help for me sad

twelveeighty wrote:

A poor man's fallback solution could be to rsync the profile frequently, using a systemd timer, not just on logout.

Risky, because the user might work with the program in exactly that moment. I thought of hiding the shutdown button in the DE and hence forcing logoff before shutdown, but did not find how to do this in gnome 40. There used to be plugins for this in Gnome 3.x... Another idea was to edit thunderbirds startup script to execute the backup script after closing the program. But this is not executed in the user context and seems to be quite hacky sad

twelveeighty wrote:

Btw: how do you know on logout that the profile wasn't corrupted and now you've backed-up a corrupted profile?

You're right, the risk is immanent. But no backup is even riskier wink And my backup strategy consists of daily, weekly and monthly backups in addidition. The data is hosted on workstations and the copy script is to backup data to the server in first place and use it on more than one machine (of course only with one active instance).

Clarification: No data will be lost, because the mails are on an Imap server and the calendars on a Radicale server. The main problem is the local copy of the data which has to be actualized on every start (and takes a lot of time with a Gigabyte Mail archive and several years of calendar data...)

The idea is charming: a local installation of thunderbird pulls mails and calendar data and syncs it to a backup which then will be pulled to another workstation if the user changes seats. So the data will never have to be fetched for more than one time. This saves bandwith both on the mail and calendar server.

Last edited by Harey (2021-04-24 17:02:33)


Linux is like a wigwam: No Gates, no Windows and an Apache inside

Offline

Board footer

Powered by FluxBB