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

interpretive language scripts


Moderator: Forum moderators

Post Reply
miltonx
Posts: 156
Joined: Sat Nov 28, 2020 12:04 am
Has thanked: 11 times
Been thanked: 6 times

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

Post 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?

Last edited by miltonx on Thu Sep 15, 2022 2:55 am, edited 1 time in total.
Rantanplan
Posts: 116
Joined: Thu Jun 03, 2021 2:25 pm
Has thanked: 38 times
Been thanked: 24 times

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

Post by Rantanplan »

sorry, it was a stupid mistake on my part.

Last edited by Rantanplan on Thu Sep 15, 2022 8:37 am, edited 1 time in total.
Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

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

Post 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

miltonx
Posts: 156
Joined: Sat Nov 28, 2020 12:04 am
Has thanked: 11 times
Been thanked: 6 times

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

Post 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.

Post Reply

Return to “Scripts”