【发布时间】:2018-10-02 01:25:06
【问题描述】:
我创建了以下脚本以在 X 分钟后关闭 Ubuntu 机器。
import subprocess
def execute_command(command):
command = command.split()
try:
print(command)
command_process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = command_process.stdout.decode('utf-8')
output_message = "Command output: {}".format(output)
error = command_process.stderr.decode('utf-8')
error_message = "Command error: {}".format(error)
print(output_message)
print(error_message)
except Exception as error:
command_error = str(error)
print("Error: "+command_error)
command = "shutdown -h 50"
execute_command(command)
输出:
['shutdown', '-h', '50']
Command output:
Command error: Shutdown scheduled for Sat 2018-04-21 19:37:25 +06, use 'shutdown -c' to cancel.
我的问题:
- 为什么
stdout是空的? - 为什么消息显示在
stderr中?我的脚本有错误吗?
【问题讨论】:
标签: python ubuntu terminal python-3.5