本文目錄一覽:
- 1、在Linux 系統管理中 Python腳本 可以完全代替 Bash腳本 嗎
- 2、在Python運行bash命令問題,怎麼解決
- 3、python如何使用gitbash執行git命令?
- 4、linux下,寫了一個python腳本,但是在bash里只能通過python環境運行,無法直接運行,求助
- 5、Linux後台運行Python程序
- 6、如何在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 osimport timedef bash_shell(bash_command):
“””
python 中執行 bash 命令 :param bash_command:
:return: bash 命令執行後的控制台輸出
“””
try:
return os.popen(bash_command).read().strip()
except: return Nonedef 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_dirif __name__ == ‘__main__’:
print(“start execute bash ………..”)
st = time.time()
cwd = os.getcwd()
# this for repo
f
or 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命令來讓程序在後台運行
括弧內容表示可以將平時輸出到控制台中的內容重定向到*.log這個文件中,這個是可選的,如果沒有這個,則會默認輸出到nohup.out文件中。括弧後面你的表示後台運行。
舉個例子:
第二種方法是寫一個腳本,假設我們定義了一個腳本run.sh
#!/bin/bash 是指此腳本使用/bin/bash來解釋執行下面的語句,其中cd是表示將當前目錄跳到所要運行文件所在目錄,然後python3 文件名.py則表示運行***python文件,當寫完該腳本後,執行以下命令來執行該腳本從而讓程序在後台運行。
通過 ps -ef|grep python3 命令可以查看後台運行的進程都有哪些
如何在bash中調用python腳本?
bash會帶上一些環境變數過去。 如果你本身環境變數配置的好。也可以不用這麼做,直接用python執行腳本,
如果python腳本本身第一行是#!/user/bin/python,而且屬性是777那麼,也可以直接執行這個腳本。
不過你在進程查看里會發現。它其實還是通過shell這個系統界面調用的python再調用的腳本。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/206285.html