0

MyUnit2.service

[Unit]
Description = MyUnit2
[Service]
RemainAfterExit=true
ExecStart=/usr/mscript2
Type=oneshot
[Install]
WantedBy=shutdown.target reboot.target

mscript2

\> /var/log/mlog2.log
echo "Time:" >> /var/log/mlog2.log
date >> /var/log/mlog2.log
echo "Uptime:" >> /var/log/mlog2.log
uptime >> /var/log/mlog2.log

According to the task it is necessary that the script be executed after shutdown, reboot, user exit. The first two options I did. How do I finish the job?

1
  • ~/.bash_logout might help. – Cyrus Jul 14 '19 at 7:43
1

You could go several routes to start a script after user logout. There may be more but here are the two I am aware of

  1. use gdm to start a script (placed in /etc/gdm/PostSession) after logout. More on this here.
  2. Use two new separate systemd services. The second one should be started by the first one at the time the first one stops. Set up the first service to stop at user logout by binding it to the already existing user session service (user@.service).

I am not familiar with the details or merits of the first route, so I will only describe the second route.

In a quick test I logged in as a different user on a different tty and checked that my process (a simple echo message) was successfully started/executed upon logout (cmd logout) of this user. It worked fine on a debian-based system with systemd v232.

My example contains three elements:

  1. Modify the existing user@.service to make it start an instance of a service (helperunit@.service) which binds to user@.service. This way two things are achieved:
    • you get instantiated services which can easily be linked to users (for logging, etc)
    • the helper service is stopped when the user logs out

# /lib/systemd/system/user@.service

[Unit]
Description=User Manager for UID %i
After=systemd-user-sessions.service

[Service]
User=%i
PAMName=systemd-user
Type=notify
ExecStart=-/lib/systemd/systemd --user
Slice=user-%i.slice
KillMode=mixed
Delegate=yes
TasksMax=infinity
TimeoutStopSec=120s

# /etc/systemd/system/user@.service.d/afterlogout.conf
[Service]
Environment="USERUID=%i"
ExecStartPost=+/bin/systemctl start helperunit@${USERUID}.service
  1. Create the helperunit service file (bound to user@.service), which is used to start a second service (afterlogout@.service) at the time it is stopped (i.e. upon user logout)

# /etc/systemd/system/helperunit@.service

[Unit]
Description=helper-to-start-afterlogout-service Service
BindsTo=user@%i.service

[Service]
Environment="USERUID=%i"
RemainAfterExit=yes
Type=oneshot
ExecStart=/bin/true
ExecStopPost=/bin/systemctl start afterlogout@${USERUID}.service

[Install]
  1. Create a second service unit file which runs your script after user logout

# /etc/systemd/system/afterlogout@.service

[Unit]
Description=trigger-script-after-user-logout Service
Before=reboot.target shutdown.target

[Service]
Environment="USERUID=%i"
Type=oneshot
ExecStart=/bin/echo "user UID=${USERUID} has logged out"

[Install]

The way this is set up the service instances will have the user UID in their name (for example, afterlogout@1000.service)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.