linux - running process background
Asked Answered
A

5

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?

Aeolus answered 24/12, 2010 at 8:45 Comment(0)
R
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.)
Rainie answered 24/12, 2010 at 8:49 Comment(0)
I
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.

Inexpert answered 10/3, 2012 at 12:15 Comment(2)
Is there an urgent reason other than "I hate seeing that" for not using nohup?Westering
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).Inexpert
T
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

Ternion answered 29/5, 2022 at 12:9 Comment(0)
M
0

A command launched enclosed in parenthesis

(command &)

will survive the death of the originating shell.

Mcclelland answered 21/5, 2013 at 14:14 Comment(1)
Your process will still be closed if the shell receives a SIGHUP signal, AFAICTRoxie
R
0
  1. screen -ls List sessions
  2. screen -S session_name Starting New Session with name

I've used nohup and &, but personally, I find the screen command to be much better.

Linux Screen is a Terminal multiplexer that lets users create multiple virtual shell sessions in their system. It saves the current process in Terminal, keeping it running even after the user disconnects from the server.

from https://www.hostinger.in/tutorials/how-to-install-and-use-linux-screen

Riot answered 10/9 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.