I was helping a colleague who was having problems with a background process intermittently dying.
I found out that they were starting the background process by logging in to the server and executing:
su - <user> -c '<command>' &
"Aha", I exclaimed. "If you start a command with "&" it will hang-up when you exit the controlling terminal. You need to use something like nohup to achieve this. Really this process needs to support running as a daemon, tut tut."
We tested the above command to demonstrate my point and... it seemed to work: the process started by command didn't exit when the we exited the terminal that ran the above command.
command is a custom Python script whose output goes to a file. As far as I can tell there is no intelligent "daemonize" like capability in the script. It doesn't do any of the things needed to run as a daemon listed here 1
Running the command like so behaves as expected:
<command> & exit
In the above case the background process started by command exits when we exit the terminal.
My question is this:
What is happening when we add "su - -c &" that prevents the process from exiting when our terminal exits. I would like to understand in detail with respect to the controlling terminal, standard input and output etc.
Is this a reasonable way to achieve the goal of running this command as a background process. If not why, not?
I want to propagate best practice within my company, but I need to be able to demonstrate and back-up any recommendations I make.
Also, I just want to understand what is going on exactly.