语法:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

执行cmd命令并返回结果到字符串。

用法:

import subprocess

output = check_output(["cat", "/etc/hostname"]).strip()
print(output)

以上脚本会执行 cat /etc/hostname 命令然后将结果赋值给 output 变量。
strip() 可以将 string 的前后空格去掉。



昨天重装了 vps 系统,在设置 crontab 定时任务时发现并没有在指定的时间执行脚本。于是就进行排查问题。

测试在 /etc/crontab 添加一条测试任务:

30  10    *  *  * root python -V > /root/test.log

在 10:30 并没有看到 test.log 文件生成。

阅读全文