You are not logged in.

#1 2014-09-10 21:12:11

Ploppz
Member
Registered: 2013-09-14
Posts: 313

[bash] Can't terminate panel script

Hi. I'm working on my panel script:

#!/bin/sh

export PANEL_FIFO="/tmp/panel-fifo"
export INPUT_FIFO="/tmp/panel-input-fifo"

if [ $(pgrep -cx panel) -gt 1 ] ; then
	printf "%s\n" "The panel is already running." >&2
	exit 1
fi

trap 'trap - TERM; kill 0' INT TERM QUIT EXIT

# MAKE FIFO

[ -e "$PANEL_FIFO" ] && rm "$PANEL_FIFO"
mkfifo "$PANEL_FIFO"

[ -e "$INPUT_FIFO" ] && rm "$INPUT_FIFO"
mkfifo "$INPUT_FIFO"
# DEFINE FUNCTIONS
# ( cut out of this sample paste)

# WRITE TO FIFO
#wm
bspc control --subscribe > "$PANEL_FIFO" &
#clock
clock > "$PANEL_FIFO" &
#countdown
while true; do echo $(countdown); sleep 1; done > "$PANEL_FIFO" &
#
temperatures > "$PANEL_FIFO" &
music > "$PANEL_FIFO" &
cpu > "$PANEL_FIFO" &

#Battery
if [[ $HOSTNAME == "lilalan" ]]; then
	battery >"$PANEL_FIFO" &
fi
set -x
# DISPLAYING
NUM_LAYOUTS=1
echo 1 > "$INPUT_FIFO" &
while read -r line <"$INPUT_FIFO"; do
	((line -- ))
	line=$(($line % $NUM_LAYOUTS))
	[[ -n $PANEL_PID ]] && kill -9 $PANEL_PID
	cat "$PANEL_FIFO" | eval "layout$line" | bar -B '#00000000' -f "*terminus*,*stlphallus*" & PANEL_PID=$!
	echo end
done
wait

The reason I have a loop in the end, reading a fifo and displaying the bar, is that I have an input fifo where I write numbers 1-12, when pressing F{1-12}, that the panel script is supposed to read and eventually reaload another "layout" script (describing contents of the panel).

My problem is that when I run the panel in a terminal, I can't fully terminate it. The bar ceases, but the terminal won't return to the shell, and I have to kill it using kill -9 <pid>.

If you notice the set -x in there, here's the output:

+ NUM_LAYOUTS=1
+ read -r line
+ echo 1
+ (( line --  ))
+ line=0
+ [[ -n '' ]]
+ cat /tmp/panel-fifo
+ eval layout0
+ PANEL_PID=313
+ echo end
++ layout0
end
+ read -r line
+ bar -B '#00000000' -f '*terminus*,*stlphallus*'
^C

I need to know why it won't quit. Thanks.

Last edited by Ploppz (2014-09-10 21:12:52)

Offline

#2 2014-09-11 04:27:31

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [bash] Can't terminate panel script

The following line looks like trouble to me

#countdown
while true; do echo $(countdown); sleep 1; done > "$PANEL_FIFO" &

Offline

#3 2014-09-11 10:16:37

Ploppz
Member
Registered: 2013-09-14
Posts: 313

Re: [bash] Can't terminate panel script

rockin turtle wrote:

The following line looks like trouble to me

#countdown
while true; do echo $(countdown); sleep 1; done > "$PANEL_FIFO" &

Why is that? It just writes to the fifo every second.

Offline

#4 2014-09-11 21:14:10

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [bash] Can't terminate panel script

Well, I don't know what exactly you are trying to accomplish, but that line starts an infinite loop that won't shut down when your script exits.

It also looks like a race condition.

I don't see what else could be causing your problem.

Offline

#5 2014-09-12 14:37:42

Ploppz
Member
Registered: 2013-09-14
Posts: 313

Re: [bash] Can't terminate panel script

The following lines also do that:

temperatures > "$PANEL_FIFO" &
music > "$PANEL_FIFO" &
cpu > "$PANEL_FIFO" &

However, shouldn't all child processes die on exit, because of this line?

trap 'trap - TERM; kill 0' INT TERM QUIT EXIT

Offline

#6 2014-09-12 17:01:14

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [bash] Can't terminate panel script

You could be correct about the trap.  I don't use trap alot, so I assumed that it only applied to the current script and not any background processes. Also, it's not apparent that a command like

temperatures > "$PANEL_FIFO" &

is an infinite loop without knowing what temperatures does.

I also don't understand why you execute another trap command when you get one of INT, TERM, QUIT, or EXIT.  Your usage may be correct, I just don't know what that does. Why wouldn't you just do?

trap 'exit' INT TERM QUIT

Offline

#7 2014-09-14 14:56:06

Ploppz
Member
Registered: 2013-09-14
Posts: 313

Re: [bash] Can't terminate panel script

Actually, that trap is just the same as in the example here. It has to reset the TERM trap before killing the children, because else the children are going to trap the signal as well, in my understanding.

Edit: I narrowed down the problem to the trap command.

#!/bin/sh

trap 'trap - TERM; kill 0' INT TERM QUIT EXIT

[ -e /tmp/test-fifo ] && rm /tmp/test-fifo
mkfifo /tmp/test-fifo

[ -e /tmp/test-input ] && rm /tmp/test-input
mkfifo /tmp/test-input

while true; do echo "hei"; sleep 1; done > /tmp/test-fifo &
while true; do echo "hei2"; sleep 1; done > /tmp/test-fifo &
while read -r line </tmp/test-input; do
	cat /tmp/test-fifo | ./trap-test2
	echo end loop
done
wait

./trap-test2 being a simple script that just reads lines and echoes it out.
Now, if I didn't comment the `trap` command, it would act like my panel script, refusing to exit. However, if I went to another terminal writing `echo "hei" > /tmp/test-input`, it would have no problems exiting.
Further, I went ahead running the `while read -r line </tmp/test-input; do` line in the background, which seems right now to have done the trick. I will test it in my actual panel script.

Last edited by Ploppz (2014-09-14 19:53:42)

Offline

Board footer

Powered by FluxBB