6

In particular, I created a script to start Firefox which I double-click and choose 'Run in terminal,' but when the shell exits Firefox is killed immediately.

How do I prevent this from happening?

Garrett Hall
  • 5,281

3 Answers3

6

You need to put the nohup before the command that launch firefox, so it needs to looks like that:

>$ nohup firefox

Hanan
  • 5,771
  • Also executing bashrc in your script can set environment variables needed by Firefox or other programs. – Garrett Hall Nov 30 '11 at 14:42
  • You can also use disown, a bash internal to achieve the same thing. This wont redirect output like nohup will but it can be used to orphan any long running jobs you started without the likes of nohup and you want to keep them running after you leave. – Matthew Ife Nov 30 '11 at 18:38
1

Also Make sure you execute firefox as a background job and not as a front running since the shell is parent and is waiting for the firefox to run and exit before the prompt is to be returned.

0

Most solution what you find on the net, will run the firefox's script in a subshell. You will get the control back, after this subshell exits. If the firefox script runs in the background, you won't see its changes and variables set, and if its parent shell (your real shell) exits, it will probably still continue in the background.

The main reason behind that, that shells, including bash, have a make very clear difference between their

  • interactive modes: where they display prompt, read commands from the user, and execute them in a cycle.
  • and non-interactive modes: it this case, they are executing essentially as an interpreter language, interpreting a shell script.

There is no easy way to change bash from a non-interactive script mode to an interactive one, after a script execution (this time, the firefox starter/wrapper script).

It would be possible, but as I know, no shell has support for this.

Fortunately, there many tricky ways to reach your goal:

  1. You can run the non-interactive script as the initialization file of your bash. So: bash --bashrc /here/is/what/i/want.sh.
  2. You can modify /here/is/what/i/want.sh to re-execute its own interpreter, but this time already in interactive mode. This can be done with an exec /bin/bash -i command. Simply put this to the end of your script.
peterh
  • 9,731