本文目录一览:
- 在Linux 系统管理中 Python脚本 可以完全代替 Bash脚本 吗
- 在Python运行bash命令问题,怎么解决
- python如何使用gitbash执行git命令?
- linux下,写了一个python脚本,但是在bash里只能通过python环境运行,无法直接运行,求助
- Linux后台运行Python程序
- 如何在bash中调用python脚本?
在Linux 系统管理中 Python脚本 可以完全代替 Bash脚本 吗
这个要分怎么说。 Bash能实现的功能,Python完全可以实现,运行效率方面,Bash不高, Python虽然也不高但不会比 Bash慢。 所以如果是自己来用的话,可以完全代替Bash。 但是,操作系统中Bash还有大量的已经在用的脚本代码,短时间内不可能完全被代替,所以从这个角度,不能被Python完全代替。
在Python运行bash命令问题,怎么解决
最近有个需求就是页面上执行shell命令,第一想到的就是os.system
,
代码如下:
os.system('cat /proc/cpuinfo')
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
代码如下:
output = os.popen('cat /proc/cpuinfo')
print(output.read())
通过 os.popen()
返回的是 file read 的对象,对其进行读取 read()
的操作可以看到执行的输出。但是无法读取程序执行的返回值。
尝试第三种方案 commands.getstatusoutput()
一个方法就可以获得到返回值和输出,非常好用。
代码如下:
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print(status, output)
Python Document 中给的一个例子, 代码如下:
import commands
commands.getstatusoutput('ls /bin/ls')
# (0, '/bin/ls')
commands.getstatusoutput('cat /bin/junk')
# (256, 'cat: /bin/junk: No such file or directory')
commands.getstatusoutput('/bin/junk')
# (256, 'sh: /bin/junk: not found')
commands.getoutput('ls /bin/ls')
# '/bin/ls'
commands.getstatus('/bin/ls')
# '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
最后页面上还可以根据返回值来显示命令执行结果。
python如何使用gitbash执行git命令?
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @name : find_t.py
# @author : cat
# @date : 2017/8/2.
import os
import time
def bash_shell(bash_command):
"""
python 中执行 bash 命令
:param bash_command:
:return: bash 命令执行后的控制台输出
"""
try:
return os.popen(bash_command).read().strip()
except:
return None
def find_target(target_path="./../", key='.git'):
"""
查找目标目录所在的目录 : 例如
/aa/bb/.git -- return /aa/bb/
:param target_path:
:param key: target
:return:
"""
walk = os.walk(target_path)
for super_dir, dir_names, file_names in walk:
for dir_name in dir_names:
if dir_name == key:
dir_full_path = os.path.join(super_dir, dir_name)
# print(dir_full_path, super_dir, dir_name, sep=" ## ")
yield super_dir
if __name__ == '__main__':
print("start execute bash ...........")
st = time.time()
cwd = os.getcwd()
# this for repo
for repo_path in find_target(os.getcwd(), key='.repo'):
os.chdir(repo_path)
if repo_path == os.getcwd():
print('find repo in --', repo_path)
print(bash_shell('pwd'))
print(bash_shell('repo forall -c git config core.fileMode false --replace-all'))
else:
print('error in chdir 2 {}'.format(repo_path))
if os.getcwd() != cwd:
os.chdir(cwd)
if os.getcwd() != cwd:
print('change 2 cwd FAIL !!! {}'.format(cwd))
# this for git
for git_path in find_target(os.getcwd(), key='.git'):
os.chdir(git_path)
if git_path == os.getcwd():
print('find git in --', git_path)
print(bash_shell('pwd'))
print(bash_shell('git config --global core.filemode false'))
else:
print('error in chdir 2 {}'.format(git_path))
if os.getcwd() != cwd:
os.chdir(cwd)
if os.getcwd() != cwd:
print('change 2 cwd FAIL !!! {}'.format(cwd))
et = time.time()
print('\n\n #### execute finished in {:.3f} seconds ####'.format(et - st))
print('\n')
# test for bash_command
# print(bash_shell('git init'))
# print(bash_shell('ls -al'))
linux下,写了一个python脚本,但是在bash里只能通过python环境运行,无法直接运行,求助
#!/usr/bin/env python
# -*- coding: utf-8 -*-
一般来说在linux下运行的python文件要加上这两句。 在Linux系统下可以免去很多错误
Linux后台运行Python程序
第一种使用 nohup
命令来让程序在后台运行:
nohup python your_script.py > output.log 2>&1 &
括号内容表示可以将平时输出到控制台中的内容重定向到 output.log
这个文件中,这个是可选的,如果没有这个,则会默认输出到 nohup.out
文件中。括号后面你的表示后台运行。
举个例子:
第二种方法是写一个脚本,假设我们定义了一个脚本 run.sh
:
#!/bin/bash
cd /path/to/your/script
python3 your_script.py
是表示此脚本使用 /bin/bash
来解释执行。其中 cd
是表示将当前目录跳到所要运行文件所在目录,然后 python3 文件名.py
则表示运行 Python 文件。当写完该脚本后,执行以下命令来执行该脚本从而让程序在后台运行。
通过 ps -ef|grep python3
命令可以查看后台运行的进程都有哪些。
如何在bash中调用python脚本?
bash会带上一些环境变量过去。 如果你本身环境变量配置的好。也可以不用这么做,直接用python执行脚本。
如果python脚本本身第一行是 #!/usr/bin/python
,而且属性是777,那么,也可以直接执行这个脚本。
不过你在进程查看里会发现。它其实还是通过shell这个系统界面调用的python再调用的脚本。