2

How do you automatically disown an application after you have started it from a terminal?

For example: if you start a terminal and run firefox the application will start, but when you then close the terminal firefox will close as well. To avoid unintentionally closing applications that have been started from a terminal, one can first put them in the background with the ampersant: firefox & which also restores the ability to use that terminal's prompt. Next, you can disown the application from that same terminal using the process ID (PID) of the application, see this example below:

$ firefox & 
$ ps | grep firefox
14917 pts/6    00:00:00 firefox
$ disown 14917

The application now runs independently from the terminal you are using, and closing the terminal will no longer terminate the application.

But how can you do this automatically each time you start an application?

  • I don't understand what the question is here. Are you looking for a way to automatically run “applications” through startAndDisown? How would you define “applications”? ps is an application, just like firefox. – Gilles May 21 '16 at 21:45
  • Maybe I'm misinformed about this, but AFAIK, sending the application to background continues to work even after you close the terminal. – ILMostro_7 May 22 '16 at 0:47
  • Did you mean to post the second part of this as an answer? – djsmiley2k May 22 '16 at 12:00
  • @djsmiley2k yes I did, but I did not have enough reputation on this board yet. – PDiracDelta May 22 '16 at 19:31
4

The simplest way is to execute:

daemon firefox

so you can continue using/closing the terminal itself

  • 3
    or nohup firefox if you're running ordinary linux – Jasen May 22 '16 at 1:25
  • Hah! the answer was much easier than I thought myself! Thanks for improving :) – PDiracDelta May 22 '16 at 19:37
  • for those wondering what difference this makes, see unix.stackexchange.com/q/3886/103120 – Blauhirn Apr 19 at 16:04
2

EDIT: a better answer was provided by I-V.

To do this automatically, you could use a bash alias. If you add the following lines to your .bash_aliases file in your home directory, you can start any application my_application from the terminal while automatically putting it on the background and subsequently disowning it from that terminal, using the command s my_application:

# start programs from shell but immediately disown them
startAndDisown() {
    $1 & disown $! 
}
alias s=startAndDisown

Note that $! automatically returns the PID of the last async job.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.