10

linux的一些运维指令和技巧

 3 years ago
source link: http://www.androidchina.net/5328.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近一段时间加强了对liunx的学习和实践。学到了一些不错的技巧和方法。切实感觉到了linux一切皆文本的的魅力。最近一周开启轻测,抽时间记录一下。

  • 查看机器cpu型号: #cat /proc/cpuinfo
  • 查看线程占用CPU: #top -H -p pid

shell带参数

  • 参数在shell中使用 1,2代替传入的参数。
  • 如下脚本调用的时候如: ./test.sh 800 4
#!/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),(arg2)代替传入的参数。
  • makefile在执行的时候可以带上参数名如: make start serverId=1000 status=3
.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方式。如下
curl -u username http://115.182.4.26:8080
  • 使用post方式,-d参数表示post方式,格式curl -d context url。如下
curl -d "server_id=800&command=2&command_cls=1&command_key=0" http://115.182.4.26:8080

文本日志相关

  • 文本写入日志文件,通常可以使用echo可以实现。
#覆盖方式写入,使用>操作符
echo '测试内容' > test.log 
#追加方式写入
echo '测试内容' >> test.log 
#写入日期的方式
echo $(date "+%Y-%m-%d %H:%M:%S") >> test.log
  • 查询日志并输出
    有时候日志文件会非常大,查看起来会非常费劲,导入到windows下也是打不开。这里的不说,linux在处理文本方面还是比较强大的。虽然看起来不直观。可以利用grep查找后很方便的重定向到文件,只看有用的部分。
#grep something > test.log
  • 查看日志
    有时候不方便用cat的时候,可以使用下tail来查看,例如:
--查看日志文件的最后200行
#tail -200 test.log
--动态查看日志文件,日志文件更新后,会显示出来
#tail -f test.log

暂时只记录这么多,后续有时间在补上来。

转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog)

转载请注明:Android开发中文站 » linux的一些运维指令和技巧


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK