<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="https://dev1galaxy.org/extern.php?action=feed&amp;tid=1958&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Dev1 Galaxy Forum / Warn on exit script for Openbox, JWM etc.]]></title>
		<link>https://dev1galaxy.org/viewtopic.php?id=1958</link>
		<description><![CDATA[The most recent posts in Warn on exit script for Openbox, JWM etc..]]></description>
		<lastBuildDate>Fri, 20 Apr 2018 20:42:41 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8522#p8522</link>
			<description><![CDATA[<p>After some more thinking, tinkering and reading around, I found a solution to this problem that&#039;s pretty workable. Let me get there step by step.</p><p>The idea was to somehow have the window manager&#039;s exit command invoked as independent from the currently running script but also put it on hold as long as that script is running. First, this implies that the hold action and the exit action need to be implemented as one command. Second, to run that command as independent from the current script, it has to be put in the background on invocation, which is done by appending an ampersand to the end of the line.</p><p>A very primitive and generally unreliable way to achieve this is using the <span class="bbc">sleep</span> command and the <span class="bbc">&amp;&amp;</span> operator:</p><div class="codebox"><pre><code>#!/bin/bash
#
# procman1

cmd_name=${0##*/}  # Get basename of the entered command
cmd_pid=$$         # Get PID of this script

sleep 5 &amp;&amp; openbox --exit &amp;  # Create a process in the background that will wait
                             # for 5 seconds and then exit Openbox.

printf &quot;${cmd_name}($cmd_pid): I&#039;ve given order to kill Openbox. This will \
happen shortly after I&#039;m done.\n&quot;

exit</code></pre></div><p>(On a side note: I found that making the wait-and-exit routine a compound command by enclosing it in <span class="bbc">{}</span> is not necessary because <span class="bbc">&amp;&amp;</span> already ties both commands together.)</p><p>Running this script and asking the shell for its exit status after it has finished shows that it is able to exit cleanly before the window manager is closed down: </p><div class="codebox"><pre><code>$ ./devel/shell/procman/procman1 &amp;&amp; echo $?
procman1(4742): I&#039;ve given order to kill Openbox. This will happen shortly after I&#039;m done.
0</code></pre></div><p>The problem here is that allowing the current script to finish is achieved simply by waiting 5 seconds before shutting down Openbox. And while there&#039;s a very good chance this script is never going to take longer than 5 seconds to complete, scheduling a command execution based on the estimated execution time of another – instead of actually checking if that process is still alive – is fundamentally risky and should not be done. </p><p>The wait-and-exit routine should rather stay awake and explicitly wait for the script to finish. Now, waiting for processes to complete before executing anything else can be achieved by issuing the <span class="bbc">wait</span> command. But that has its limitations: It can only handle child processes of the current shell. If <span class="bbc">sleep</span> is simply replaced by <span class="bbc">wait</span> (plus PID of the running script), the shell will complain about that.</p><div class="codebox"><pre><code>#!/bin/bash
#
# procman2

cmd_name=${0##*/}  # Get basename of the entered command
cmd_pid=$$         # Get PID of this script

wait $cmd_pid &amp;&amp; openbox --exit &amp;  # Create a process in the background that
                                   # will wait for this script to finish and
                                   # then exit Openbox.

printf &quot;${cmd_name}($cmd_pid): I&#039;ve given order to kill Openbox. This will \
happen shortly after I&#039;m done.\n&quot;

exit</code></pre></div><p>Running the script results in an error message from <span class="bbc">wait</span>:</p><div class="codebox"><pre><code>$ ./devel/shell/procman/procman2 &amp;&amp; echo $?
procman2(5529): I&#039;ve given order to kill Openbox. This will happen shortly after I&#039;m done.
./devel/shell/procman/procman2: line 8: wait: pid 5529 is not a child of this shell
0</code></pre></div><p>I&#039;m still unsure why this really happens. I assume that <span class="bbc">#!/bin/bash</span> at the beginning of a script creates a subshell in which the script is then executed. However, this is not reflected by the output of <span class="bbc">ps</span>, as far as I can see.</p><p>Anyway, the question is: How is it possible to wait for any process to complete before executing a particular command? My solution has been inspired by <a href="https://stackoverflow.com/questions/1058047/wait-for-any-process-to-finish" rel="nofollow">some answers to this question on Stackoverflow</a>. And it goes like this:</p><div class="codebox"><pre class="vscroll"><code>#!/bin/bash
#
# procman3

cmd_name=${0##*/}  # Get basename of the entered command
cmd_pid=$$         # Get PID of this script

function f_wait_exit {

  while ps -p $cmd_pid &gt; /dev/null  # Check if this script is still running, but
                                    # redirect stdout of the ps command to
                                    # /dev/null
  do
    printf &quot;Waiting for $cmd_pid to finish...\n&quot;
    sleep 1  # Don&#039;t go crazy on CPU usage
  done

  sleep 3  # A bit of time to read the exit code of this script after it&#039;s done
  openbox --exit
}

f_wait_exit &amp;  # Execute the wait-and-exit routine in the background

printf &quot;${cmd_name}($cmd_pid): I&#039;ve given order to kill Openbox. This will \
happen shortly after I&#039;m done.\n&quot;

sleep 3  # Keep this script alive for 3 seconds for demonstration purposes

exit</code></pre></div><p>Running this script will give the following results:</p><div class="codebox"><pre><code>$ ./devel/shell/procman/procman3 &amp;&amp; echo $?
procman3(4805): I&#039;ve given order to kill Openbox. This will happen shortly after I&#039;m done.
Waiting for 4805 to finish...
Waiting for 4805 to finish...
Waiting for 4805 to finish...
0</code></pre></div><p>So, problem solved? I guess so, apart from the fact that using <span class="bbc">sleep</span> in the while loop creates a race condition. But, when the script is not kept alive for demonstration, the sleep time could be brought down to half a second or less there to reduce the risk of any new process taking <span class="bbc">$cmd_pid</span> in the meantime.</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Fri, 20 Apr 2018 20:42:41 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8522#p8522</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8314#p8314</link>
			<description><![CDATA[<p>Another problem I&#039;ve been facing with that script is that executing it involves killing its parent process. When running the script directly (meaning not from a terminal window), the process tree will look like this:</p><div class="codebox"><pre><code> PID  PPID COMMAND
2379     1   login
3678  2379     bash
3687  3678       startx
3709  3687         xinit
3710  3709           Xorg
3715  3709           openbox
4000  3715             wex
4015  4000               xmessage</code></pre></div><p>Now, when Openbox is shut down by <span class="bbc">wex</span>, <span class="bbc">wex</span> will be killed immediately in return, meaning the end of the script will not be reached. To prevent this, I would probably have to do two things:</p><ul><li><p>have the exit command for the window manager executed as a process independent of the running script</p></li><li><p>have the system wait for <span class="bbc">wex</span> to finish before doing that</p></li></ul><p>Btw, one thing that I don&#039;t understand about the process tree above is that, apparently, no new instance of bash is being created when <span class="bbc">wex</span> is run, which is a bit strange given that it&#039;s a bash script.</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Wed, 11 Apr 2018 12:21:09 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8314#p8314</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8155#p8155</link>
			<description><![CDATA[<p>Right. I already noticed this is causing problems when, at one point, the script would not exit the window manager even though I had put all remaining windows on the ignore list. The reason was that one text editor window I was running had a 5-digit process id.</p><p>After searching the web for a bit, I found a really neat solution, allowing me to go back to using the initial <span class="bbc">cut</span> command. As mentioned above, the problem with <span class="bbc">cut</span> is that it cannot collapse multiple spaces into single ones. But you can have <span class="bbc">tr</span> do that before you pipe the output to <span class="bbc">cut</span>:</p><div class="codebox"><pre><code>wmctrl -l -p | tr -s &#039; &#039; | cut -d &#039; &#039; -f 3</code></pre></div><p><span class="bbc">tr -s</span>, as the manual page states, &quot;replace(s) each input sequence of a repeated character (...) with a single occurrence of that character.&quot;</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Thu, 29 Mar 2018 22:58:21 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8155#p8155</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8154#p8154</link>
			<description><![CDATA[<p>Well, since process ids may have anything between one and five digits, you could as well let the interpreter do the tokenization, making it be like the following:</p><div class="codebox"><pre><code>wmctrl -l -p | while read a b c d ; do echo $c ; done</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (ralph.ronnquist)]]></author>
			<pubDate>Thu, 29 Mar 2018 21:23:15 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8154#p8154</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8133#p8133</link>
			<description><![CDATA[<p>Trying to use <span class="bbc">sed</span>, I realized that it&#039;s probably not the right tool for what I want to do here. <span class="bbc">cut</span> is actually fine. You just have to use it properly, which would mean using <span class="bbc">cut -c 15-18</span> in this case.</p><p>But then I thought: This is nothing but a simple substring extraction, so there should be a way to do it using parameter expansion. And there is: Once a line printed by <span class="bbc">wmctrl</span> has been assigned to a variable, you can just use <span class="bbc">&quot;${line:14:4}&quot;</span>, where the first number is the starting position and the second the substring&#039;s character count.</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Thu, 29 Mar 2018 13:19:05 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8133#p8133</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8124#p8124</link>
			<description><![CDATA[<div class="quotebox"><cite>fsmithred wrote:</cite><blockquote><div><p>I&#039;m not understanding the behavior of cut.</p></div></blockquote></div><p>It is a bit strange indeed. Thanks for spotting that.</p><p>The problem seems to have two causes: First, the second column of <span class="bbc">wmctrl</span>&#039;s output, which displays the number of the virtual workspace a window is on, has a different width for sticky windows (those that are visible across all workspaces), since they are identified by a value of <span class="bbc">-1</span>. (The same goes for windows with a workspace number above 10.)</p><p>Then, as ralph.ronnquist has already pointed out, <span class="bbc">cut -d &quot; &quot;</span> doesn&#039;t seem to collapse multiple spaces into one. What it seems to do instead is a bit weird: If there is only one space between two printable character strings, it obviously takes that space as a delimiter, e.g.:</p><div class="codebox"><pre><code>$ var1=&quot;one two&quot;
$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 1
one
$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 2
two</code></pre></div><p>But if there&#039;s more than one space, <span class="bbc">cut</span> seems to count the first one as a delimiter only, but the following ones each as both a delimiter and a field, so:</p><div class="codebox"><pre><code>$ var1=&quot;one   two&quot;  # 3 spaces inbetween
$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 1
one
$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 2  # will print the 2nd space

$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 3  # will print the 3rd space

$ echo &quot;$var1&quot; | cut -d &quot; &quot; -f 4
two</code></pre></div><p>There seems to be no way to have <span class="bbc">wmctrl</span> not show the second column, so the script should rather use <span class="bbc">sed</span> with a regular expression instead of <span class="bbc">cut</span> there. I&#039;ll change that.</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Wed, 28 Mar 2018 16:48:53 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8124#p8124</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8121#p8121</link>
			<description><![CDATA[<p>It looks like it takes every space character as a delimiter, whereas awk sees space sequences as delimiting units.</p>]]></description>
			<author><![CDATA[dummy@example.com (ralph.ronnquist)]]></author>
			<pubDate>Wed, 28 Mar 2018 12:18:24 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8121#p8121</guid>
		</item>
		<item>
			<title><![CDATA[Re: Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8120#p8120</link>
			<description><![CDATA[<p>I&#039;m not understanding the behavior of cut. Why are the field counts not the same for each line? I see the pid in the third field on all lines, and awk sees it the same way I do.</p><div class="codebox"><pre class="vscroll"><code>user@refracta:~$ wmctrl -l -p
0x0080001c -1 1866   refracta panel
0x00a00004  0 1865   refracta user@refracta: ~
0x00e00008 -1 1868   refracta spacefm
user@refracta:~$ wmctrl -l -p | cut -d &quot; &quot; -f 4

1865

user@refracta:~$ wmctrl -l -p | cut -d &quot; &quot; -f 1
0x0080001c
0x00a00004
0x00e00008
user@refracta:~$ wmctrl -l -p | cut -d &quot; &quot; -f 2
-1

-1
user@refracta:~$ wmctrl -l -p | cut -d &quot; &quot; -f 3
1866
0
1868
user@refracta:~$ wmctrl -l -p | cut -d &quot; &quot; -f 4

1865

user@refracta:~$ wmctrl -l -p |awk &#039;{print $3}&#039;
1866
1865
1868</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (fsmithred)]]></author>
			<pubDate>Wed, 28 Mar 2018 11:53:45 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8120#p8120</guid>
		</item>
		<item>
			<title><![CDATA[Warn on exit script for Openbox, JWM etc.]]></title>
			<link>https://dev1galaxy.org/viewtopic.php?pid=8119#p8119</link>
			<description><![CDATA[<p>As I&#039;ve already mentioned <a href="https://dev1galaxy.org/viewtopic.php?id=1951" rel="nofollow">over there</a>, I&#039;ve been working on a Bash script that will display a warning dialog in case the user tries to exit the window manager when there are remaining open windows.</p><p>The whole thing is sort of finished for now, though it surely requires some additional tweaks and fixes. The source can be found at <a href="https://github.com/msiism/desktop-scripts" rel="nofollow">https://github.com/msiism/desktop-scripts</a>.</p><p>Testing results (though there&#039;s not really much to test there) and comments on the code would be much appreciated. I&#039;ve been using it without a problem up until now.</p><p>Two additional remarks: While the README file on GitHub states the script can currently be used with Openbox or JWM only, it should&#160; be able work with any window manager that can be queried by <span class="bbc">wmctrl</span> and offers an exit switch as a command line option. It would simply have to be added. Also, I deliberately chose <span class="bbc">xmessage</span> for the GUI for two reasons: One, it&#039;s part of X and doesn&#039;t depend on anything else. Two, my plan is to expand and re-write the whole script in Python and use Tk for the GUI eventually. So it doesn&#039;t have to look totally beautiful yet.</p>]]></description>
			<author><![CDATA[dummy@example.com (msi)]]></author>
			<pubDate>Wed, 28 Mar 2018 02:26:08 +0000</pubDate>
			<guid>https://dev1galaxy.org/viewtopic.php?pid=8119#p8119</guid>
		</item>
	</channel>
</rss>
