Let's take example of a command "example-command".
example-command
in terminal, and example-command
executes.example-command
gets killed too.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?
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.
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.
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.
There are two ways, identical in result.
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.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With