Skip to content

Instantly share code, notes, and snippets.

@bugcy013
Last active January 10, 2020 05:33
Show Gist options
  • Save bugcy013/42d507dc0e5d4a88cfa6d3fbd2b58348 to your computer and use it in GitHub Desktop.
Save bugcy013/42d507dc0e5d4a88cfa6d3fbd2b58348 to your computer and use it in GitHub Desktop.
import os
import subprocess
def run_cmd(cmd="hostname", work_dir=None):
try:
logging.critical("let's execute cmd=%s", cmd)
pipe = subprocess.Popen(cmd, shell=True, cwd=work_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, error = pipe.communicate()
print(out, error)
pipe.wait()
except:
import traceback
print(traceback.print_exc())
if __name__ == '__main__':
run_cmd("ls -ltr")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment