Prevent an application from being closed on terminal exit

7,788

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
7,788

Related videos on Youtube

Lars
Author by

Lars

Everyday Ubuntu user

Updated on September 18, 2022

Comments

  • Lars
    Lars over 1 year

    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.

  • Lars
    Lars over 12 years
    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.)
  • Michael K
    Michael K over 12 years
    does it work even when you close the terminal (command line) after starting your script?
  • Lars
    Lars over 12 years
    Yes, perfectly so. It only fails when started from a desktop launcher.
  • Panther
    Panther over 12 years
    If you would like help with a script, please post the entire script for review.
  • Dennis Fazekas
    Dennis Fazekas almost 3 years
    I also had to add the sleep 1 after the nohup line as you described. This worked perfect. Thank you.