Vikrant Chaudhary
Vikrant Chaudhary

Reputation: 11319

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

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?

Upvotes: 25

Views: 29081

Answers (8)

alamar
alamar

Reputation: 19343

In Zsh (not bash) you can:

example-command &; disown {pid}

or just

example-command &; disown

Upvotes: 7

Benjamin Pollack
Benjamin Pollack

Reputation: 28460

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.

Upvotes: 28

Eduardo
Eduardo

Reputation: 7851

Run: example-command

Press: Control-Z

Run: bg

Upvotes: -2

Bash
Bash

Reputation: 4750

Please search for similar questions first.

Besides the ways listed above, you can do:

setsid command_name

For example:

setsid xclock

Thanks

Upvotes: 11

MatthieuP
MatthieuP

Reputation: 1126

You could also consider using the screen command.

Upvotes: 5

Aiden Bell
Aiden Bell

Reputation: 28384

disown is a bash builtin. You could create a wrapper shellscript for your command such as

#!/bin/bash
$1 &
P=`which $1`
disown `pidof ${P}`

Not the most robust script (by any means) but may help get you going. For example:

$./launch_script.sh myProgram

You can also do this in the source of the program if you are editing it.

Upvotes: 1

Shane C. Mason
Shane C. Mason

Reputation: 7615

You can also use the 'at' or 'batch' commands and give it the current time.

Upvotes: 1

hiena
hiena

Reputation: 955

nohup example-command

Upvotes: 1

Related Questions