Move logging code outside of command execution try-except block (#2739)
This commit is contained in:
parent
8ec1715eb8
commit
481b570dae
|
|
@ -302,28 +302,29 @@ class SysCommandWorker:
|
||||||
# https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
|
# https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
|
||||||
if not self.pid:
|
if not self.pid:
|
||||||
history_logfile = pathlib.Path(f"{storage['LOG_PATH']}/cmd_history.txt")
|
history_logfile = pathlib.Path(f"{storage['LOG_PATH']}/cmd_history.txt")
|
||||||
|
|
||||||
|
change_perm = False
|
||||||
|
if history_logfile.exists() is False:
|
||||||
|
change_perm = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
change_perm = False
|
with history_logfile.open("a") as cmd_log:
|
||||||
if history_logfile.exists() is False:
|
cmd_log.write(f"{time.time()} {self.cmd}\n")
|
||||||
change_perm = True
|
|
||||||
|
|
||||||
try:
|
if change_perm:
|
||||||
with history_logfile.open("a") as cmd_log:
|
os.chmod(str(history_logfile), stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
|
||||||
cmd_log.write(f"{time.time()} {self.cmd}\n")
|
except (PermissionError, FileNotFoundError):
|
||||||
|
# If history_logfile does not exist, ignore the error
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
exception_type = type(e).__name__
|
||||||
|
error(f"Unexpected {exception_type} occurred in {self.cmd}: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
if change_perm:
|
if storage.get('arguments', {}).get('debug'):
|
||||||
os.chmod(str(history_logfile), stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
|
debug(f"Executing: {self.cmd}")
|
||||||
except (PermissionError, FileNotFoundError):
|
|
||||||
# If history_logfile does not exist, ignore the error
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
|
||||||
exception_type = type(e).__name__
|
|
||||||
error(f"Unexpected {exception_type} occurred in {self.cmd}: {e}")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
if storage.get('arguments', {}).get('debug'):
|
|
||||||
debug(f"Executing: {self.cmd}")
|
|
||||||
|
|
||||||
|
try:
|
||||||
os.execve(self.cmd[0], list(self.cmd), {**os.environ, **self.environment_vars})
|
os.execve(self.cmd[0], list(self.cmd), {**os.environ, **self.environment_vars})
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
error(f"{self.cmd[0]} does not exist.")
|
error(f"{self.cmd[0]} does not exist.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue