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.

  • 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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.