Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start a command from terminal so that terminal is not the parent?

Tags:

Let's take example of a command "example-command".

  1. I open terminal
  2. I write example-command in terminal, and example-command executes.
  3. Now if I close terminal, example-command gets killed too.
  4. I now try with "example-command &", but the same behaviour.

How do I execute a command so that when I close the terminal, the command doesn't get terminated?

like image 685
Vikrant Chaudhary Avatar asked May 12 '09 16:05

Vikrant Chaudhary


People also ask

How do I detach a command in terminal?

We can use the & operator, and the nohup, disown, setsid, and screen commands to start a process detached from the terminal. However, to detach a process that has already started, we need to use the bg command after pausing the process using Ctrl+Z.

How do you detach a process?

You can press ctrl-z to interrupt the process and then run bg to make it run in the background. You can show a numbered list all processes backgrounded in this manner with jobs . Then you can run disown %1 (replace 1 with the process number output by jobs ) to detach the process from the terminal.

What does & disown after a command do?

The disown command in Linux is used to remove jobs from the job table. You can also use it to keep a longer and more complex job running in the background even after you log out of the server.


1 Answers

There are two ways, identical in result.

  1. Use nohup when you start your program. E.g., nohup example-command. You can background and work with it normally; it will simply continue running after you've quit.
  2. Alternatively, as @alamar noted, if you use bash as your shell, you can us the disown command. Unfortunately, as far as I know, disown is bash-specific; if you use another shell, such tcsh, you may be restricted to the nohup form above.
like image 102
Benjamin Pollack Avatar answered Oct 14 '22 02:10

Benjamin Pollack