You are not logged in.

#1 2016-06-28 09:08:58

easysid
Member
From: India
Registered: 2013-01-01
Posts: 256

Help with toggling a shell script

Hi,

I am trying to achieve a lemonbar instance that can be toggled by clicking or a keybind. The end goal is to have a normal panel, with usual desktop info, and clock, and use a second instance, whenever required to display the sysinfo like cpu, ram etc in detail. The second instance overlays on the first to achieve a seamless look.

Here is an example using just the clock from sutils package (or a simple loop with date)

UOJpiSo.png

The detailed instance can be toggled by clicking the arrows (« »).

Here are the scripts for the image

primary_panel.sh

#! /bin/sh

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

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

. ~/.scripts/theme_config

geometry="1166x22+100+29"

# define the toggle command
command="%{r F- A:~/detail_panel.sh:} « %{A}"

~/clock -s -f "${command} %H:%M:%S " | lemonbar -p \
           -g "$geometry" \
           -f "$ICON_FONT" -f "$FONT1"\
           -B "$BAR_BG" \
           -F "$BAR_FG" \
           -a 20 \
           -u 2 \
           | sh &
wait

detail_panel.sh - This script toggles itself when called.  Currently, I use a file to store pid and check if it is running.

#! /bin/sh

pidfile='/tmp/pidfile'

if [ -s "$pidfile" ]; then
    read -r pid < "$pidfile"
    printf "script is already running with pid: %s\nKilling ...\n" "$pid"
    kill "$pid"
    exit 1
fi

. ~/.scripts/theme_config

trap "trap - TERM; rm -v $pidfile;  kill 0" INT TERM QUIT EXIT

echo "$$" > "$pidfile"

geometry='300x22+965+29'

# define the toggle command
command="%{r F- A:~/detail_panel.sh:} » %{A}"

~/clock -s -f "${command} %A, %d %B %H:%M:%S " | lemonbar -p \
    -g "$geometry" \
    -f "$ICON_FONT" -f "$FONT1"\
    -B "$BAR_BG" \
    -F "$BAR_FG" \
    | sh &

wait

Here is the issue. The detail_panel.sh scripts kills both the scripts when called by clicking on the arrows. I can toggle it from the command line, or from a keybind, and it works as expected. But it kills both the instances, if called using click area.

I blame the kill 0 in the trap to kill even the calling parent in this case. I have tried using kill -- -$$, and pkill -TERM -P $$ so that the script exits killing any child processes, but that doesn't seem to work.

It is getting embarrassing at this point, not being able to do this task.

Thanks.

Offline

#2 2016-06-28 19:30:46

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,033

Re: Help with toggling a shell script

A less complicated method would be to have one script that checks the value in a file and if that value is 0 it prints minimal infos else it prints more. Then you just have to find a method that suits to toggle the value in the file.

HTH


You're just jealous because the voices only talk to me.

Offline

#3 2016-06-29 01:21:57

ymf331
Banned
Registered: 2015-04-18
Posts: 9

Re: Help with toggling a shell script

idk, maybe try kill with a pidof to get the pid of the actuall script's process?


i can't put this zsh config on the forum: http://dotshare.it/dots/1144/

Offline

#4 2016-06-29 03:28:57

easysid
Member
From: India
Registered: 2013-01-01
Posts: 256

Re: Help with toggling a shell script

moetunes wrote:

A less complicated method would be to have one script that checks the value in a file and if that value is 0 it prints minimal infos else it prints more. Then you just have to find a method that suits to toggle the value in the file.

HTH

That is the method I use currently, as described here.

But my script updates every 3 seconds, which means there is a lot of latency in the toggle. I could fix the update interval, but I don't want to do that.

Another way to do this is to toggle the info scripts piping into the FIFO for bspwm. Read the toggle state from a file, kill the current script, and start the other one. No second lemonbar needed. I probably will have to go that route.

Offline

#5 2016-06-29 08:23:43

Saint0fCloud
Member
Registered: 2009-03-31
Posts: 137

Re: Help with toggling a shell script

My naive suggestion would have been to try using `kill $$` to terminate the entire process group, but otherwise why not just use a cleanup function that runs in your trap and kills any child processes? I don't know bash, but for example, in zsh you can grab the child job ids using `$jobstates`. Or, in this case, since you're only backgrounding a single process, save the pid and kill it later.

Offline

#6 2016-06-30 13:07:55

easysid
Member
From: India
Registered: 2013-01-01
Posts: 256

Re: Help with toggling a shell script

I have tried different methods, all of which work cleanly on their own, but not when toggling with the mouse click.

e.g. the following code works quite well, although it is not as robust as pid method. Yet again, I can toggle the script through the commandline or a keybind, using the mouse click causes both scripts to terminate.

detail_panel.sh

#! /bin/sh
#
# detail_panel.sh
# displays a lemonbar segment with more detailed sysinfo
#

if [ $(pgrep -cx detail_panel.sh) -gt 1 ]; then
    printf "script is running\n"
    killall detail_panel.sh
fi

. ~/.scripts/theme_config

trap "trap - TERM; kill -- -$$" INT TERM EXIT

geometry='300x22+965+29'

# define the toggle command
command="%{r F- A:~/detail_panel.sh:} « %{A}"

~/clock -s -f "${command} %A, %d %B %H:%M:%S " | lemonbar -p \
    -g "$geometry" \
    -f "$ICON_FONT" -f "$FONT1"\
    -B "$BAR_BG" \
    -F "$BAR_FG" \
    | sh &

wait

following is the expected behaviour. Run the script in a terminal. In another terminal, call the script again. It terminates successfully

 ~ $  ./detail_panel.sh 
script is running
Terminated

Following is the response I get on a second mouse click to terminate the script

script is running
/home/easysid/detail_panel.sh: 1: kill: No such process

/home/easysid/detail_panel.sh: 1: kill: No such process

Terminated

And the bar stays.

Offline

#7 2016-07-01 07:29:51

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

Re: Help with toggling a shell script

I think the problem is the following line in detail_panel.sh:

command="%{r F- A:~/detail_panel.sh:} » %{A}"

which, I think, should be:

command="%{r F- A:~/primary_panel.sh:} » %{A}"

Offline

#8 2016-07-01 18:16:04

easysid
Member
From: India
Registered: 2013-01-01
Posts: 256

Re: Help with toggling a shell script

rockin turtle wrote:

I think the problem is the following line in detail_panel.sh:

command="%{r F- A:~/detail_panel.sh:} » %{A}"

which, I think, should be:

command="%{r F- A:~/primary_panel.sh:} » %{A}"

Wouldn't that just start a new instance of primary_panel.sh, leaving the detail panel still running?

Offline

#9 2016-07-01 21:12:55

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

Re: Help with toggling a shell script

Well, I guess I don't know how lemonbar works, so I really don't know.

Since your primary_panel.sh script starts detail_panel.sh, it just seems to me that detail_panel.sh should start primary_panel.sh. I'm probably wrong about that.

You could possibly use 'exec' instead of 'kill'. Example below.

proc1.sh:

#!/bin/bash

trap 'exec ./proc2.sh' INT TERM
trap 'echo -en "\r\e[2K"; exit' HUP

while :; do
	echo -en "\r\e[2K« $(date '+%I:%M:%S')"
	sleep 1
done

proc2.sh:

#!/bin/bash

trap 'exec ./proc1.sh' INT TERM
trap 'echo -en "\r\e[2K"; exit' HUP

while :; do
	echo -en "\r\e[2K» $(date '+%A, %e %B %I:%M:%S')"
	sleep 1
done

Put these two files in a directory somewhere and start proc1.sh.

    $ ./proc1.sh

After a few seconds hit ctrl-c to send a SIGTERM.  The 'exec' will cause the proc1.sh process to be replaced by proc2.sh. (They will share the same PID.)  You can toggle back and forth by repeatingly hitting ctrl-c.

To kill both processes, go to another xterm.  Use pgrep to find the PID, then do:

    $ kill -HUP $PID

Of course, you would have to modify these scripts for your purposes.

Offline

#10 2016-07-03 12:49:46

easysid
Member
From: India
Registered: 2013-01-01
Posts: 256

Re: Help with toggling a shell script

The click areas on lemonbar work by just sending to stdout whatever is written in between the {A} tags. For example, %{A: command:}text%{A} will print 'command' on stdout, whenever 'text' is clicked. We can execute the command by sending it to shell, or by using eval. Hence the trailing sh in the scripts.

Thanks for the scripts, may be I am missing exec or kill somewhere. But even with my current scripts, I can use ctrl-C, or kill command without any issues. It is only trying to toggle through click areas that I encounter the issues.

I do not want to toggle *between* the scripts. I just want to toggle one (detail_panel.sh) script. It is a popup. When you toggle a popup, you do not want to replace the existing process. You want the popup to run in a different process, and exit if clicked again.

Last edited by easysid (2016-07-03 12:50:44)

Offline

Board footer

Powered by FluxBB