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

42 lines
1.0 KiB
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(["su", "-s", "/bin/bash", "-c", command, "cvm"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while prc.poll() is None:
stdout = prc.stdout.read()
stderr = prc.stderr.read()
if stdout.strip() != "":
sys.stdout.write(stdout)
logfile.write(stdout + "\n")
if stderr.strip() != "":
sys.stderr.write(stderr)
logfile.write(stderr + "\n")
stdout = prc.stdout.read()
stderr = prc.stderr.read()
logfile.write(stdout + "\n" + stderr + "\n")
sys.stdout.write(stdout)
sys.stderr.write(stderr)
logfile.close()
exit(prc.returncode)