3

I have created a bash script that starts an application. Since I wanted the terminal window to close when the application is started, I used this line:

firefox & 2> /dev/null; exit;

It works fine as long as I start the script from the command line. However, if I start it from a desktop laucher the application closes as well as the terminal. Can anyone explain why this is. Im using Ubuntu 10.10.

By only using:

    nohup firefox

it works. But I still would like an explanation to why the original version doesn't work when started from a desktop launcher, if possible.

3 Answers 3

7

The solution is to simply use the right form in the pipe:

firefox > /dev/null 2>&1
exit

This works both from terminal and from a desktop launcher. No need to use nohup.

0
6

Try the nohup-command:

nohup firefox & 2> /dev/null; exit;

nohup disconnects the running jobs from the terminal session which prevents them from being closed on exit.

However, if you only want to start firefox, then simply create a shortcut to it, no bash script required.

Further as explanation: Try to run the script from a terminal, leave the firefox window open and close the terminal in which you ran your script. Firefox will close as well as it is a child process of the terminal session it was started from.

4
  • Thanks but no difference when using nohup. I use a script because ther are other things happening before starting the app, but this is the only line not working. (It does work when started from command line.)
    – Lars
    Jan 9, 2012 at 8:53
  • does it work even when you close the terminal (command line) after starting your script?
    – Michael K
    Jan 9, 2012 at 9:37
  • Yes, perfectly so. It only fails when started from a desktop launcher.
    – Lars
    Jan 9, 2012 at 10:25
  • If you would like help with a script, please post the entire script for review.
    – Panther
    Jan 9, 2012 at 17:42
1

A solution that worked for me is to put a 1 sec sleep into the script.

Here is a minimal example of the script I was trying to run via a gnome desktop launcher:

#!/bin/bash
echo "test"
read e

nohup firefox & 2> /dev/null
exit

This works fine when started from within a terminal, i.e.:

  1. it displays "test" and after pressing enter firefox starts
  2. firefox stays alive when closing the terminal

However, when starting this from a desktop launcher with:

gnome-terminal -e "script"

a terminal displays "test" and shuts down after pressing enter (firefox never shows up, I assume it is killed with the terminal).

However the following works started with a launcher:

#!/bin/bash
echo "test"
read e

nohup firefox & 2> /dev/null
sleep 1
exit

in this case firefox starts after pressing enter, and stays alive when the terminal shuts down after the sleep time (or is closed during the sleep time).

1
  • I also had to add the sleep 1 after the nohup line as you described. This worked perfect. Thank you. Aug 15, 2021 at 23:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .