Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1929  / 3 Years ago, fri, august 13, 2021, 4:40:44

I'm trying to do some stuff at logout, and the task can take up to 5 minutes to complete.



If the user chooses Power off or Reboot directly, the script is killed before completion. If the user just chooses Log out, the script is executed with no problems.



I've tried using pam_exec and lightdm property session-cleanup-script to point to my script, but it's the same in both cases.



The script used to do the testing is this one:



#!/bin/bash
touch /tmpfile
for p in $(seq 0 300) ; do
sleep 1
echo $p >> /tmpfile
done


When logging out, all the 300 numbers are written to the file. When shutting down or rebooting directly from within the user session, only 2 or 3 numbers are written, so the script is being killed.



How is handled the shutdown by lightdm? How can I ensure my script is not being killed?



If you propose an alternative approach, please take care that I need to know what user is being logged out and be able to run a script as that user. I also need to notify the user about what's happening, so better stay on X if possible.


More From » 12.04

 Answers
4

To handle this, I combined three mechanisms:




  • A Lightdm logout script, that gets the user information and writes it on a temporary file, so we can know who is powering off the computer. It also does the heavy task in case we're not shutting down or rebooting.

  • Traditional initrd scripts to do the heavy tasks when shutting down or rebooting.

  • Plymouth status messages to give feedback about that heavy tasks when shutting down or rebooting.



So, I have the heavy task script on /usr/local/bin/heavy.sh:



#!/bin/bash 
touch /tmpfile
for p in $(seq 0 300) ; do
sleep 1
echo $p >> /tmpfile
if [ ! -z $DISPLAY ] ; then
notify-send "Written $p"
else
plymouth message --text="Written $p" &>/dev/null
fi
done


That heavy task tries to output information to X. If that fails, tries to do it to Plymouth.



Then, I added this line to the Lightdm configuration on /etc/lightdm/lightdm.conf:



session-cleanup-script=/usr/local/sbin/logout-tasks.sh


The referenced script writes down the user on logout and tries to do the heavy task:



#!/bin/bash
echo $USER > /run/last-logout-user
/usr/local/bin/heavy.sh


The initrd script, located on /etc/init.d/logout-heavy-task-at-shutdown:



#!/bin/bash
USER_RUN=$(cat /run/last-logout-user)
sudo -u $USER_RUN /usr/local/bin/heavy.sh


That launches the task as the last logged out user when shutting down and restarting, considering we add the corresponding links:



 update-rc.d -n logout-heavy-task-at-shutdown start 05 0
update-rc.d -n logout-heavy-task-at-shutdown start 05 6


And that's it. The heavy task might need to handle when it's killed by X and then launched again by the initrd script. Depending on the task, that might not be a problem at all.


[#32887] Saturday, August 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laceanz

Total Points: 86
Total Questions: 113
Total Answers: 104

Location: Australia
Member since Thu, Jan 26, 2023
1 Year ago
;