Current File : //proc/24011/task/24011/root/etc/node_exporter/runner_proc
#!/usr/bin/env python

import os
import signal
import subprocess
import sys
from threading import Timer

pathScripts = "/etc/node_exporter/scripts"
pathText = "/etc/node_exporter/text"

#pathScripts = "./scripts_test"
#pathText = "./text_test"

f = sys.argv[1]

myCmd = "%s/%s" % (pathScripts,f)

def kill():
    print p.pid
    os.kill(os.getpgid(p.pid), signal.SIGTERM)

print "####################"
print "cmd: ", myCmd

p = subprocess.Popen(myCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

timer = Timer(30, kill)

try:
    timer.start()
    (output, err) = p.communicate()
    p_status = p.wait()

    print "err: ", err
    print "output: ", output
    print "exit code: ", p_status

    if p_status == 0 and len(err) == 0:
        filePath = "%s/%s.prom" % (pathText, f)
        f = open(filePath, "w")
        f.write(output)
        f.close()
    else:
        print output
        print p_status
finally:
    timer.cancel()