View previous topic :: View next topic |
Author |
Message |
psychedup n00b
Joined: 02 Oct 2012 Posts: 19 Location: Bremerton, WA, US
|
Posted: Wed Dec 16, 2020 3:55 am Post subject: [solved] dwmrc script keeps running |
|
|
I've recently been getting sucked into the world of dwm and other suckless.org apps. Having a little issue with my custom .dwmrc script though, probably a dumb mistake but hopefully somebody here can help.
I'm using lightdm to log in, and it seems to call a script in /etc/X11/Sessions/dwm:
Code: |
#!/bin/sh
DIR=${HOME}/.dwm
if [ -f "${DIR}"/dwmrc ]; then
/bin/sh "${DIR}"/dwmrc &
else
while true; do
xsetroot -name "`date`"
sleep 1
done &
fi
exec /usr/bin/dwm
|
So I created my own file in ~/.dwm/dwmrc, to launch some apps and also runs a while loop to update the statusbar:
Code: |
#!/bin/sh
while true; do
BatStatus=$(cat /sys/class/power_supply/cw2015-battery/status)
BatStatus=${BatStatus:0:1}
BatState=$(cat /sys/class/power_supply/cw2015-battery/capacity)
xsetroot -name "$(cat /proc/loadavg) | $BatStatus$BatState | $(date '+%b %d %a') | $(date '+%H:%M')"
# xsetroot -name " $(date '+%b %d %a') | $(date '+%H:%M') "
sleep 1
done &
/usr/bin/nextcloud --background &
/usr/bin/keepassxc &
/usr/bin/clementine &
|
This works great, mostly - the only problem is, when I log out of dwm (by hitting Shift+Alt+Q), the while loop continues running. And if I log back in again, now it is running twice.
How can I make it kill the script when I log out?
Last edited by psychedup on Thu Dec 17, 2020 5:37 pm; edited 1 time in total |
|
Back to top |
|
|
The Main Man Veteran
Joined: 27 Nov 2014 Posts: 1171 Location: /run/user/1000
|
Posted: Thu Dec 17, 2020 11:04 am Post subject: |
|
|
Why dont' you kill a script before logging out ?
Though it's probably a lighdm thing, I don't use it so I can't tell, if you're the only user maybe you can switch from it to 'startx'
Or change the status generation to something else
https://dwm.suckless.org/status_monitor/ |
|
Back to top |
|
|
AlexJGreen Tux's lil' helper
Joined: 19 Sep 2018 Posts: 149
|
Posted: Thu Dec 17, 2020 12:02 pm Post subject: |
|
|
_
Last edited by AlexJGreen on Mon Dec 28, 2020 2:57 am; edited 1 time in total |
|
Back to top |
|
|
psychedup n00b
Joined: 02 Oct 2012 Posts: 19 Location: Bremerton, WA, US
|
Posted: Thu Dec 17, 2020 5:34 pm Post subject: |
|
|
Thanks, your suggestions helped. i also looked at this blog post.
I tried using a trap but it seems that dwm doesn't send the script any signals when it quits, the trap never gets triggered. So I realized (as in your suggestion), the script itself is going to have to monitor and shut down itself and its child processes.
From that blog I learned that when my script is run, the PID of the process that called it, gets assigned as its PPID. But when that parent process quits, my process's PPID changes to 1. So I just have to monitor the PPID and if it's 1, kill all of my child processes and myself.
So I added this loop to the end of my dwmrc script and it seems to be working:
Code: |
# upon logout, quit all child processes
# if my PPID = 1, that means dwm has quit and I am an orphaned process. In that case, kill all child processes and then exit
while true
do
myPPID=`ps -o ppid --no-headers --pid $$`
if [ $myPPID -eq 1 ]; then
pkill -TERM -P $$
exit 0
fi
sleep 5s
done
|
|
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22676
|
Posted: Fri Dec 18, 2020 11:07 pm Post subject: |
|
|
That is a bad way to detect that your process has become an orphan. Instead, you can consult $PPID, a magic variable that obtains the parent process ID easily.
Second, you could avoid the problem by instructing the kernel to terminate the children automatically. For each child, as you create it, set its parent-death-signal to SIGTERM. When the parent terminates, the kernel will automatically SIGTERM the children. To do this, you can use setpriv --pdeathsig SIGTERM command & instead of a bare command &. Once this is done, just exit your script when you are an orphan, and your children will be terminated automatically. You could even be clever and reexecute yourself with a pdeathsig so that the death of your script's parent kills your script. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|