49 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
from sys import argv
import socket
from typing import List
from de.p1st.exec_notify.lib import exec, config, mail, util
def main():
if len(argv) <= 1:
print('No command given to execute!', file=sys.stderr)
exit(1)
exit(executeCommand(argv[1:]))
def executeCommand(command: List) -> int:
"""
Executes the given command and sends an email on failure.
:return: exit code of executed command
"""
exitCode, stdout, stderr = exec.execute(command)
prefix=''
BODY = f'{""*19}\n' \
f'┣╸Command:\n' \
f'{util.appendLinePrefix(prefix, str(command))}\n' \
f'┣╸Exit Code:\n' \
f'{util.appendLinePrefix(prefix, str(exitCode))}\n' \
f'┣╸stderr:\n' \
f'{util.appendLinePrefix(prefix, stderr)}\n' \
f'┣╸stdout:\n' \
f'{util.appendLinePrefix(prefix, stdout)}\n' \
f'{""*19}'
print(BODY)
if exitCode != 0:
SUBJECT = f'{socket.gethostname()} | {str(command)}'
mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY)
return exitCode
if __name__ == '__main__':
main()