Issue
I'm thinking if my run.py
script can act in two different mode.
when in nohup mode: nohup python run.py &
,act like full log output mode.but in normal mode python run.py
, act like log suppressed mode. So it will be tidy and clear for the user.
So.My question: How does my runnable python script knonw itself running in nohup mode or normal mode?
Solution
The nohup
command modifies the OS-level signal handling for the process it launches. This is not reflected in the command's arguments, but the process can query itself to check what actions or signal handlers are installed. Here's how to do it:
import signal
if signal.getsignal(signal.SIGHUP) == signal.SIG_DFL: # default action
print("No SIGHUP handler")
else:
print("In nohup mode")
This will work on any Unix system, and probably on Windows (not tested). There are other ways to set a signal handler, from the shell or from within the program, but if you're just trying to distinguish between nohup
and normal invocation, this will tell you all you need to know.
Answered By - alexis Answer Checked By - Mary Flores (WPSolving Volunteer)