0
  • OS Environment: Pfsense 2.4.4 based on FreeBSD 11.2-RELEASE-p10
  • Python: Python 2.7.16 (default, May 10 2019, 17:54:37)
  • Problem description:

I want to set my first python script running as a daemon, and I define a function:

def set_daemon(cur_woring_path):
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError, e:
        print "First fork failed:%s" % e.message
        sys.exit(1)

    os.chdir(cur_woring_path)
    os.setsid()
    os.umask(0)

    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError, e:
        print "Second fork failed:%s" % e.message
        sys.exit(1)

    os.close(sys.stdin.fileno())
    os.close(sys.stdout.fileno())
    os.close(sys.stderr.fileno())

After I called set_daemon(...), I wan to execute shell command and get the output:

    load_logger_config("INFO")
    logger = logging.getLogger("debug_info_logger")

    target_process_name = "my_second_process.py"
    set_daemon("/root")

    cmd = "ps -aux | grep " + target_process_name + "| grep -v grep|awk -F \" \" '{print $2}' "
    out = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) 
    logger.warning("Found process is running..., pid = %s", out.stdout.read()[:-1])

But the out string is always empty, I have tried:

commands.getoutput(cmd)
os.popen(cmd)
popen2

redirect to file is empty too.
cmd = "ps -aux | grep " + target_process_name + "| grep -v grep|awk -F \" \" '{print $2}' > tmp.txt"

...

Cause of the problem maybe:

    os.close(sys.stdin.fileno())
    os.close(sys.stdout.fileno())
    os.close(sys.stderr.fileno())

because without set_daemon, everything is ok. I've searched google, but nothing help.

Are there any solutions? I need both set_daemon and shell output, could I reopen the stdout fileno? I appreciate a lot anyone could help me

  • If you are only just learning the basics, you should probably ignore Python 2, and spend your time on the currently recommended and supported version of the language, which is Python 3. – tripleee Jan 02 '20 at 10:03
  • Is this a problem only about python2, or just my coding mistake. I am really confused. thanks anyway! – liuxsong Jan 02 '20 at 10:07
  • That's a comment, not an answer. I think this should work pretty much identically (or not work) on Python 3 actually. I'm looking at the `subprocess.Popen()` mess as probably the culprit but not in a place where I can troubleshoot properly. – tripleee Jan 02 '20 at 10:09
  • You should break your command into the individual pieces as a list of strings. – James Jan 02 '20 at 10:10
  • @James That alone will not help, it requires `shell=True` or a significant refactoring as it is now. – tripleee Jan 02 '20 at 10:14
  • Running just `ps -aux` in a subprocess and replacing the pipeline with Python-native code should be just 3-4 lines and give you much more control over the process. My guess is that the Python script's name is not visible in the `ps` listing but that's just a vague conjecture. See also https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee Jan 02 '20 at 10:22
  • I just delete the flowing 3 line to avoid this problem temporarily `os.close(sys.stdin.fileno())` ` os.close(sys.stdout.fileno())` `os.close(sys.stderr.fileno())` and then, I can get `ps -aux` output smoothly, but sometimes the python script output will appear on the terminal. This may not be the best solution. I will keep trying, thanks everyone! If anybody finds a better way, I will appreciate it if you could tell me. – liuxsong Jan 03 '20 at 03:38

0 Answers0