0

I'm writing a linux server program in python. I want the server to continue running after I close the terminal. And I find two way to implement it:

  1. use the nohup command: (or screen)
nohup python main.py >/dev/null 2>&1 &
  1. Daemonize the program by double-fork. Need to write additional python code. (How do you create a daemon in Python?)

I'm wondering what's the difference between the two implementations? Which one is prefered?

  • 1
    0. write a [systemd service file](https://www.freedesktop.org/software/systemd/man/systemd.service.html) – Klaus D. Jan 01 '23 at 10:15
  • 1
    nohup works when command is started and prevents any user input. double fork can be done later after user interaction. – stark Jan 01 '23 at 12:27

1 Answers1

1

Daemon mode is a different functioning of the software, running in an infinite loop without the user interaction component. Using & doesn't guarantee that the software won't continue to accept input on stdin and output on stdout. Oftentimes, daemon mode has different features, for example software will by default log more thoroughly in daemon mode -- in standard mode, it simply outputs to stdout.

Nohup will ignore the output from the software but it still doesn't make the software run in daemon mode. Worst case, there is potential that the software could hang if it is expecting user input.

So Deamon make it superior over nohup.