Page 1 of 1

Running script from xterm in Debian then closing xterm causes weird behavior (Solved)

Posted: Wed Sep 14, 2022 9:40 am
by miltonx

This is not puppy related. Just a weird thing I noticed on my DIY-ed debian.

System: Debian Bullseye (installed via debootstrap).
Terminal: xterm
File Manager: pcmanfm
/usr/bin/sh: linked to bash

Experiment:
1. Open a leafpad window and leave it there;
2. Write a script: /root/xxxxxx.sh with these lines:

Code: Select all

sleep 3
if ! pgrep leafpad; then
	geany
fi

3. Doubleclick /root/xxxxxx.sh to run it. Three seconds later, nothing happens, Geany does NOT open.
4. Open xterm, run this:

Code: Select all

./xxxxxx.sh & disown -h

Then quickly shut this xterm window.
Weird thing happens - three seconds later, geany opens!
(But if the xterm window is kept open till after three seconds expires, geany does not open!)

Why does the script fail to observe the if condition when the command is run from xterm and then disowned and xterm closed?


Re: Running command from terminal and closing terminal causes weird behavior

Posted: Wed Sep 14, 2022 1:33 pm
by Rantanplan

sorry, it was a stupid mistake on my part.


Re: Running command from terminal and closing terminal causes weird behavior

Posted: Wed Sep 14, 2022 1:35 pm
by Burunduk
miltonx wrote: Wed Sep 14, 2022 9:40 am

Why does the script fail to observe the if condition when the command is run from xterm and then disowned and xterm closed?

It doesn't. Your script will start geany if pgrep hasn't found the PID or has encountered an error. In this case pgrep can't print the PID to the closed stdout.
If you redirect the stdout, the script will work. This can be done either in the script itself or by running it with nohup command that redirects output to the nohup.out file.

Code: Select all

sleep 3
if ! pgrep leafpad >out.txt; then
	geany
fi

or

Code: Select all

nohup ./xxxxxx.sh

Another way to consume the output: if [ `pgrep -c leafpad` -eq 0 ]; then


Re: Running command from terminal and closing terminal causes weird behavior

Posted: Thu Sep 15, 2022 2:53 am
by miltonx
Burunduk wrote: Wed Sep 14, 2022 1:35 pm
miltonx wrote: Wed Sep 14, 2022 9:40 am

Why does the script fail to observe the if condition when the command is run from xterm and then disowned and xterm closed?

It doesn't. Your script will start geany if pgrep hasn't found the PID or has encountered an error. In this case pgrep can't print the PID to the closed stdout.
If you redirect the stdout, the script will work. This can be done either in the script itself or by running it with nohup command that redirects output to the nohup.out file.

Code: Select all

sleep 3
if ! pgrep leafpad >out.txt; then
	geany
fi

or

Code: Select all

nohup ./xxxxxx.sh

Another way to consume the output: if [ `pgrep -c leafpad` -eq 0 ]; then

Thanks! Now I have just learned that although disown removes the process from the shell's job list, it still used the shell's stdout. nohup will fix it.