You are not logged in.

#1 2020-01-11 17:11:12

coolgoose54
Member
Registered: 2015-12-09
Posts: 13

[SOLVED] Running script before and after systemd session unit

Has anyone tried running a script before and after systemd session unit ?

I am trying to run a script that would fetch decryption keys from a key server and unlock home directory before the session and unmount the partition when session ends.

Last edited by coolgoose54 (2020-01-15 23:15:51)

Offline

#2 2020-01-11 18:58:50

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,901
Website

Re: [SOLVED] Running script before and after systemd session unit

man systemd.service wrote:

ExecStartPre=, ExecStartPost=
...


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2020-01-12 13:18:00

coolgoose54
Member
Registered: 2015-12-09
Posts: 13

Re: [SOLVED] Running script before and after systemd session unit

How does one define ExecStartPre and ExecStartPost for session units ?

Offline

#4 2020-01-12 13:47:49

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,901
Website

Re: [SOLVED] Running script before and after systemd session unit


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2020-01-15 16:50:04

coolgoose54
Member
Registered: 2015-12-09
Posts: 13

Re: [SOLVED] Running script before and after systemd session unit

I am not sure what I am doing wrong.

[ ~]$ sudo cat /etc/systemd/system/user\@.service.d/override.conf 
ExecStartPre=-/usr/bin/bash /etc/log.sh %i
ExecStartPort=-/usr/bin/bash /etc/log.sh %i
[ ~]$ sudo cat /etc/systemd/system/user\@service.service.d/override.conf 
ExecStartPre=-/usr/bin/bash /etc/log.sh %i
[ ~]$ cat /etc/log.sh
#!/bin/bash

echo "" >> /tmp/pre.txt
date >> /tmp/pre.txt
env >> /tmp/pre.txt
ls /dev/mapper >> /tmp/pre.txt
echo "" >> /tmp/pre.txt
[~]$ sudo systemctl show 'user@service' | grep Exec
ExecMainStartTimestampMonotonic=0
ExecMainExitTimestampMonotonic=0
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0
ExecStart={ path=/usr/lib/systemd/systemd ; argv[]=/usr/lib/systemd/systemd --user ; ignore_errors=yes ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
ExecStartEx={ path=/usr/lib/systemd/systemd ; argv[]=/usr/lib/systemd/systemd --user ; flags=ignore-failure ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
MemoryDenyWriteExecute=no
[ ~]$ sudo systemctl show 'user@.service' | grep Exec
Failed to get properties: Unit name user@.service is neither a valid invocation ID nor unit name.
[ ~]$ sudo systemctl daemon-reload
[ ~]$ sudo systemctl show 'user@service' | grep Exec
ExecMainStartTimestampMonotonic=0
ExecMainExitTimestampMonotonic=0
ExecMainPID=0
ExecMainCode=0
ExecMainStatus=0
ExecStart={ path=/usr/lib/systemd/systemd ; argv[]=/usr/lib/systemd/systemd --user ; ignore_errors=yes ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
ExecStartEx={ path=/usr/lib/systemd/systemd ; argv[]=/usr/lib/systemd/systemd --user ; flags=ignore-failure ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
MemoryDenyWriteExecute=no
[ ~]$ sudo systemctl show 'user@.service' | grep Exec
Failed to get properties: Unit name user@.service is neither a valid invocation ID nor unit name.

And nothing gets logged to the log file if I login or logout.

Offline

#6 2020-01-15 17:59:00

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 5,093
Website

Re: [SOLVED] Running script before and after systemd session unit

coolgoose54 wrote:
[ ~]$ sudo cat /etc/systemd/system/user\@.service.d/override.conf 
ExecStartPre=-/usr/bin/bash /etc/log.sh %i
ExecStartPort=-/usr/bin/bash /etc/log.sh %i
[ ~]$ sudo cat /etc/systemd/system/user\@service.service.d/override.conf 
ExecStartPre=-/usr/bin/bash /etc/log.sh %i

You have one override for user@.service and one for user@service.service, you should delete the latter.

And I think you need to declare the relevant section in override.conf, like this:

[Service]
ExecStartPre=-/usr/bin/bash /etc/log.sh %i
ExecStartPort=-/usr/bin/bash /etc/log.sh %i
coolgoose54 wrote:
[ ~]$ sudo systemctl show 'user@.service' | grep Exec
Failed to get properties: Unit name user@.service is neither a valid invocation ID nor unit name.

Try

systemctl cat user@.service

Offline

#7 2020-01-15 19:02:50

coolgoose54
Member
Registered: 2015-12-09
Posts: 13

Re: [SOLVED] Running script before and after systemd session unit

Head_on_a_Stick wrote:

And I think you need to declare the relevant section in override.conf, like this:

[Service]
ExecStartPre=-/usr/bin/bash /etc/log.sh %i
ExecStartPort=-/usr/bin/bash /etc/log.sh %i

That works, thanks. I think was most likely the section declaration.

Offline

#8 2020-01-15 19:11:54

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 5,093
Website

Re: [SOLVED] Running script before and after systemd session unit

Fantastic, please mark the thread [SOLVED] for the benefit of others.

Offline

Board footer

Powered by FluxBB