You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cvm/logshell/logcmd

36 lines
887 B
Python

#!/usr/bin/env python
import random, string, subprocess, os, sys
from datetime import time, date, datetime
from time import strftime
try:
command = sys.argv[1]
except IndexError, e:
exit(1)
rand = ''.join(random.choice(string.letters + string.digits) for i in xrange(8))
rand = "%s_%s" % (strftime("%Y%m%d_%H%M%S"), rand)
logfile = open("/etc/cvm/log/log.%s" % rand, "w")
logfile.write("# NON-INTERACTIVE COMMAND EXECUTION\n")
logfile.write("$ " + command + "\n")
prc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while prc.poll():
stdout, stderr = prc.communicate()
logfile.write(stdout + "\n" + stderr + "\n")
sys.stdout.write(stdout)
sys.stderr.write(stderr)
stdout, stderr = prc.communicate()
logfile.write(stdout + "\n" + stderr + "\n")
sys.stdout.write(stdout)
sys.stderr.write(stderr)
logfile.close()
exit(prc.returncode)