歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux的一些運維指令和技巧

linux的一些運維指令和技巧

日期:2017/3/3 11:10:52   编辑:Linux技術
轉載請注明出處:簾卷西風的專欄(http://blog.csdn.net/ljxfblog)
最近一段時間加強了對liunx的學習和實踐。學到了一些不錯的技巧和方法。切實感覺到了linux一切皆文本的的魅力。最近一周開啟輕測,抽時間記錄一下。

狀態相關

查看機器cpu型號: #cat /proc/cpuinfo
查看線程占用CPU: #top -H -p pid

shell帶參數

參數在shell中使用 1,1,2代替傳入的參數。
如下腳本調用的時候如: ./test.sh 800 4
[code]#!/bin/sh 
#test.sh

center_ip=http://115.182.4.26:8080
#參數center_ip: 中心服務器的IP

server_id=998
if [ $2 ]; then
    server_id=$2 
fi
#參數server_id 服務器ID
status=4
if [ $1 ]; then
    status=$1 
fi
#參數status 服務器啟動狀態(1/新服,2/良好, 3/爆滿, 4/調試, 5/激活碼)

curl -d "server_id=$server_id&command=2&command_cls=1&command_key=0&command_content={status=$status, tick=0}" "$center_ip"

makefile帶參數

同上面shell一樣,makefile也可以帶參數執行。
參數在makefile中使用 (arg1),(arg1),(arg2)代替傳入的參數。
makefile在執行的時候可以帶上參數名如: make start serverId=1000 status=3
[code].PHONY: start stop 

center_ip =http://115.182.4.26:8080 #參數center_ip: 中心服務器的IP

server_id = 998
#參數server_id 服務器ID

status = 2
#參數status 服務器啟動狀態(1/新服,2/良好, 3/爆滿, 4/調試, 5/激活碼)

start: 
ifneq ($(server_id), 0)
    curl -d "server_id=$(server_id)&command=2&command_cls=1&command_key=0&command_content={status=$(status), tick=0}" $(center_ip)
endif

stop: 
ifneq ($(server_id), 0)
    curl -d "server_id=$(server_id)&command=2&command_cls=1&command_key=0&command_content={status=0, tick=0}" $(center_ip)
endif

在makefile和shell中使用curl。

curl在linux中的makefile和shell中基本可以直接使用,不用安裝。
curl默認使用get方式。如下
[code]curl -u username' target='_blank'>http://115.182.4.26:8080[/code] 
使用post方式,-d參數表示post方式,格式curl -d context url。如下
[code]curl -d "server_id=800&command=2&command_cls=1&command_key=0"' target='_blank'>http://115.182.4.26:8080[/code] 

文本日志相關

文本寫入日志文件,通常可以使用echo可以實現。
[code]#覆蓋方式寫入,使用>操作符
echo '測試內容' > test.log 
#追加方式寫入
echo '測試內容' >> test.log 
#寫入日期的方式
echo $(date "+%Y-%m-%d %H:%M:%S") >> test.log

查詢日志並輸出
有時候日志文件會非常大,查看起來會非常費勁,導入到windows下也是打不開。這裡的不說,linux在處理文本方面還是比較強大的。雖然看起來不直觀。可以利用grep查找後很方便的重定向到文件,只看有用的部分。
[code]#grep something > test.log

查看日志
有時候不方便用cat的時候,可以使用下tail來查看,例如:
[code]--查看日志文件的最後200行
#tail -200 test.log
--動態查看日志文件,日志文件更新後,會顯示出來
#tail -f test.log

暫時只記錄這麼多,後續有時間在補上來。
Copyright © Linux教程網 All Rights Reserved