1

[This post has been edited from the original text to add the results of further investigation and get to the point more quickly -- Thanks @ctrl-alt-delor]

Why does this script:

#!/bin/bash
read -p "Press any key to continue: " key
nohup java -jar /path/to/compiled.jar &

not successfully start/display the specified java GUI app when it is invoked by a Terminal window opened as a result of double-clicking the following desktop shortcut:

[Desktop Entry]
Icon=bash
Exec=bash /path/to/the/above/script.sh
Type=Application
Terminal=true

when it works as expected when manually invoked from a manually-opened Terminal window?

Surprisingly, if the script is modified by merely adding a sleep 0 command, to become:

#!/bin/bash
read -p "Press any key to continue: " key
nohup java -jar /path/to/compiled.jar &
sleep 0

then it works as expected regardless of how it is invoked.

My question for this post is: Why is the sleep command required? Can anyone explain this behavior?

I believe this question is subtly different from StackExchange question 3886 and also different from all of the dozen or so others that appear to be similar because none of them refer to the case of script invocation from a desktop shortcut/launcher.

Do not be concerned with the apparent absurdity of the script. I have intentionally simplified it for the purposes of this post to provide the simplest example that will reproduce the issue without getting distracted by other issues.

Subsequent testing has shown that the behavior is the same regardless of whether or not the command to invoke java redirects either or both stdout and sdterr: the java app always displays when the script is manually invoked from a manually-opened Terminal window, but when invoked from a desktop shortcut, it works only if a sleep command is included when the script is launched from a desktop shortcut.

Examination of the Terminal environment (using printenv as suggested by @Michael Prokopek) reveals no differences except for the GNOME_TERMINAL_SERVICE and GNOME_TERMINAL_SCREEN variables which appear to be unique for each Terminal window.

BTW: Works like a charm with no drama at all using a DOS script on Windows.

Chucko
  • 31
  • 1
    To get more and better answers, you may want to change the order of this question: to get to the point earlier. – ctrl-alt-delor Dec 20 '18 at 19:58
  • Does this work nohup java -jar /path/to/compiled.jar 1>/dev/null 2>/dev/null &? – rudib Dec 20 '18 at 20:01
  • I would open a terminal directly and from a shortcut making sure it stays open then printenv in both, check for differences. – Michael Prokopec Dec 20 '18 at 20:07
  • Thanks @rudib. I did not try the exact redirection syntax that you suggest, but I did try something like >/dev/null 2>&1 and the results were essentially as I described. – Chucko Dec 20 '18 at 20:49
  • @ctrl-alt-del: Yeah. I thought of that ... after I posted. Thanks for your suggestion. – Chucko Dec 20 '18 at 20:50
  • @Michael Prokopec, I'll try that tomorrow and post my results. – Chucko Dec 20 '18 at 20:51
  • You do know that you can edit the question? You can make it better now. – ctrl-alt-delor Dec 20 '18 at 21:51
  • 1
    I'd replace the Exec entry with Exec=strace -D -e trace=process -f -o /tmp/strace.log bash /path/to/the/above/script.sh. It looks like there's a race condition with nohup receiving SIGHUP too early. Might require sleep 1 then (strace overhead). – A.B Dec 23 '18 at 18:02
  • In response to A.B, yep! The trace indicated that it was, in fact, a race condition in which the child process simply lost the race. The sleep command was all it took to allow it to consistently do what I wanted. Thanks for the suggestion. – Chucko Jan 02 '19 at 11:25

0 Answers0