-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuprocess-example.py
More file actions
38 lines (33 loc) · 1.35 KB
/
suprocess-example.py
File metadata and controls
38 lines (33 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import subprocess
# List of commands to run
commands = [
"ls -l",
"pwd",
"echo 'Hello, subprocess!'",
"uname -a",
"df -h",
"ps aux",
"whoami",
"date",
"echo 'Another subprocess example'",
"ifconfig"
]
# Iterate through the list of commands and run each one
for command in commands:
try:
# Run the command using subprocess.run
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Print the command and its output
print(f"Command: {command}")
print(f"Output:\n{result.stdout}")
print("=" * 40)
except subprocess.CalledProcessError as e:
# If the command returns a non-zero exit code, print the error
print(f"Error running command '{command}': {e}")
print("=" * 40)
~
~
~
~
"subprocess_example.py" 32L, 861B 1,1 All
[ttyd] 0:vim* "student@bchd: ~/mycod" 20:50 13-Nov-23