6

I want to run a process in a remote linux server and keep that process alive after close the putty terminal,

what is the correct command?

Buddhi
  • 2,224
  • 5
  • 32
  • 43

4 Answers4

10

You have two options:

  1. Use GNU screen, which will allow you to run the command and detach it from your terminal, and later re-attach it to a different session. I use it for long-running processes whose output I want to be able to monitor at any time. Screen is a truly powerful tool and I would highly recommend spending some time to learn it.
  2. Run the command as nohup some-command &, which will run the command in the background, detach it from the console, and redirect its output into nohup.out. It will swallow SIGHUPs that are sent to the process. (When you close the terminal or log out, SIGHUP is sent to all processes that were started by the login shell, and the default action the kernel will take is to kill the process off. This is why appending & to put the process in the background is not enough for it to survive a logout.)
cdhowie
  • 158,093
  • 24
  • 286
  • 300
1

The modern and easy to use approach that allows managing multiple processes and has a nice terminal UI is hapless utility.

Install with pip install hapless (or python3 -m pip install hapless) and just run

$ hap run my-command  # e.g. hap run python my_long_running_script.py
$ hap status  # check all the launched processes

See docs for more info.

ui

Most Wanted
  • 6,254
  • 5
  • 53
  • 70
1

don't use that nohup junk, i hate seeing that on servers; screen is a wasting pile of bits and rot -- use tmux.

if you want to background a process, double fork like every other daemon since the beginning of time:

# ((exec sleep 30)&)
# grep PPid /proc/`pgrep sleep`/status
PPid:   1
# jobs
# disown
bash: disown: current: no such job

enjoy.

anthonyrisinger
  • 2,889
  • 1
  • 20
  • 10
  • 2
    Is there an urgent reason other than "I hate seeing that" for not using `nohup`? – Damon May 21 '13 at 15:03
  • 1
    well, not _urgent_ i suppose, unless being pointless meets the requirements. using `nohup` for this task is a workaround to a 100% *non-issue*... i have no idea how/why `nohup` became the commonplace "solution", nor do i know of a technical limitation to the method outlined above. a double-fork is clean, fast (avoids a fork, should it matter), retains the use of SIGHUP proper, and works on any shell since ~30yrs. while i can't cite details off-hand, `nohup` bit me a few times when unknowingly ran applications under it that made legitimate use of SIGHUP (of which there are many). – anthonyrisinger Jun 06 '13 at 10:56
0

A command launched enclosed in parenthesis

(command &)

will survive the death of the originating shell.

yPhil
  • 8,049
  • 4
  • 57
  • 83