Skip to content

linux相关知识

linux相关运维知识

jar包运维

md
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re
import time
import sys

#日志所在目录
LOG_PATH="/usr/local/projects/logs/demo/"
#远程服务器日志所在目录
REMOTE_LOG="/usr/local/projects/logs/demo/"
#jar源路径
SOURCE="/usr/local/projects/publish/"
#目标目录
TARGET="/usr/local/projects/cloud/demo/"
#备份目录
BACK_PATH="/usr/release/"
#远程jar包发布目录
REMOTE_DIR="/usr/local/projects/publish/"
#远程启动脚本
REMOTE_SHELL="/usr/local/projects/cloud/demo/cts.py"
#远程用户
REMOTE_USER=""
#远程服务器密码
REMOTE_PWD=""
#远程服务器IP
REMOTE_IP=""
#app相关
API_NAME="demo-service-1.0.jar"

#获取当前时间:yyyyMMDDHHmmss
def getTime():
    now = int(time.time())
    timeStruct = time.localtime(now)
    timeStr = time.strftime("%Y%m%d%H%M%S", timeStruct)
    return timeStr

#启动项目
def start():
    print "启动:" + API_NAME
    opt = " -Dspring.profiles.active=prod"
    opt += " -Dserver.port=7010 -DworkId=3"
    opt += " -Xms1024m -Xmx1024m -Xmn512m"
    opt += " -XX:CMSInitiatingOccupancyFraction=92"
    cmd = "nohup java -jar" + opt + " " + API_NAME + ">/dev/null 2>&1 &"
    os.system(cmd)
    time.sleep(5)
    logCmd = "tail -f " + LOG_PATH + "info.log"
    os.system(logCmd)

#停止项目
def stop():
    print "停止项目:" + API_NAME
    cmd = "ps -ef | grep -v grep | grep " + API_NAME
    print cmd
    res = os.popen(cmd)
    for v in res:
        res = re.sub(" +"," ",v)
        arr = res.split(" ")
        print API_NAME + "pid:" + arr[1]
        # size = len(arr)
        killCmd = "kill -9 " + arr[1]
        os.system(killCmd)
        print "项目已停止。。。。。。"

#复制项目
def copy():
    print "开始发布目录:" + API_NAME
    cmd = "cp " + SOURCE + API_NAME + " " + TARGET + API_NAME
    os.system(cmd)

#备份项目
def back():
    print "开始备份目录:" + API_NAME
    timeStr = getTime()
    backName = API_NAME + "." + timeStr
    cmd = "cp " + API_NAME + " " + BACK_PATH + backName
    os.system(cmd)
    cmd = "echo " + backName + ">" + TARGET + "back"
    os.system(cmd)
    print "备份完成。。。。。。。。。。"
    
#还原上一个版本
def reduction():
    path = TARGET + "back"
    exitsts = os.path.exists(path);
    if exitsts != True:
        print("back文件不存在,无法回滚")
        return
    f = open(path)
    jarName = f.readline()
    if len(jarName) == 0:
        print("前一个版本不存在")
        return
    cmd  = "cp " + BACK_PATH + jarName.replace("\n","") + " " + TARGET + API_NAME
    print("开始回滚:" + jarName + " 命令:" + cmd)
    os.system(cmd)
    stop()
    start()

#发布到远程目录
def scp_remote():
    print "开始传输:" + TARGET + API_NAME + "到远程:" + REMOTE_IP
    cmd = "sshpass -p " + REMOTE_PWD + " scp "
    cmd += TARGET + API_NAME + " " + REMOTE_USER + "@"+REMOTE_IP + ":" + REMOTE_DIR
    os.system(cmd)
    print "传输完毕。。。。。。。。"

#执行远程脚本
def start_remote():
    print "开始执行远程脚本。。。。。。。"
    time.sleep(3)
    cmd = "sshpass -p " + REMOTE_PWD + " ssh " + REMOTE_USER + "@" + REMOTE_IP
    cmd += " python " + REMOTE_SHELL + " publish"
    os.system(cmd)

#重启项目
def restart():
    stop()
    start()

#发布项目
def publish():
    back()
    copy()
    stop()
    start()

#启动第二节点    
def scp():
    scp_remote()
    start_remote()

#main方法
def main():
    args = sys.argv
    size = len(args)
    if size < 2:
        print "可选参数:>>[ start|stop|restart|publish|scp|back ]<<"
        return
    i = args[1]
    if i == "start":
        start()
        return
    if i == "stop":
        stop()
        return
    if i == "restart":
        restart()
        return
    if i == "publish":
        publish()
        return
    if i == "scp":
        scp()
        return
    if i == "back":
        reduction()
        return
    print "可选参数:>>[ start|stop|restart|publish|scp|back ]<<"

if __name__ == '__main__':
    main()
```

VUE包发布

md
```python
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import re
import time
import sys

#前端发布目录
VIEW_PATH = "/usr/local/nginx/html/views/"
#前端待发布目录
VIEW_PUBLISH_PATH = "/usr/local/projects/publish/views/"
#前端备份目录
VIEW_BACK_PATH = "/usr/release/"

#获取当前时间:yyyyMMDDHHmmss
def getTime():
    now = int(time.time())
    timeStruct = time.localtime(now)
    timeStr = time.strftime("%Y%m%d%H%M%S", timeStruct)
    return timeStr

#发布目录
def publish(param):
    file_path = VIEW_PUBLISH_PATH + param
    if os.path.exists(file_path) == False:
        print("待发布目录不存在,不能执行发布命令:" + file_path)
        return
    cmd = "cp -r " + VIEW_PUBLISH_PATH + param + " " + VIEW_PATH
    print("开始执行发布命令:" + cmd)
    os.system(cmd)
    print("目录发布完毕")

#备份目录
def back(param):
    file_path = VIEW_PATH + param
    if os.path.exists(file_path) == False:
        print("待备份目录不存在,备份失败")
        return
    cmd = "cp -r " + VIEW_PATH + param + " " + VIEW_BACK_PATH + param + "." + getTime()
    print("开始执行备份命令:" + cmd)
    os.system(cmd)
    print("备份完成")

#删除源文件
def remove_old(param):
    file_path = VIEW_PATH + param
    if os.path.exists(file_path) == False:
        print("旧目录不存在,不再执行删除目录操作:" + file_path)
        return
    cmd = "rm -rf " + VIEW_PATH + param
    print("开始删除旧目录命令:" + cmd)
    os.system(cmd)
    print("删除执行完成")

def main():
    args = sys.argv
    size = len(args)
    if size < 2:
        print("请输入需要发布的目录:ticket、driver、store")
        return
    i = args[1]
    file_path = VIEW_PUBLISH_PATH + i
    if i == "ticket" or i == "driver" or i == "store":
        if os.path.exists(file_path) == False:
            print("待发布目录不存在,不能执行发布操作:" + file_path)
            return
        #备份前端目录
        back(i)
        #删除前端目录
        remove_old(i)
        #发布前端目录
        publish(i)


if __name__ == '__main__':
    main()
```

运维常用命令

查看进程是否存在

md
ps -ef | grep 进程名

查看进程执行目录

md
ll /proc/pid

查看日志

grep命令常用指令:
grep -A 显示匹配指定内容及之后的n行

grep -B 显示匹配指定内容及之前的n行

grep -C 显示匹配指定内容及其前后各n行

less -i(忽略大小写查询)命令,推出时按q

zgrep 查询.zip压缩包内容

查看防火墙状态

md
systemctl status firewalld.service

开放端口

md
firewall-cmd --zone=public --add-port=6030/tcp --permanent 
firewall-cmd --zone=public --add-port=1000-2000/tcp --permanent

重新载入:firewall-cmd --reload
查看:firewall-cmd --zone=public --query-port=60/tcp
scp

md
scp local_file remote_username@remote_ip:remote_folder 

scp local_file remote_username@remote_ip:remote_file

find

md
find ./ -iname

df

md
df -Th

df查看系统磁盘使用情况-T显示磁盘文件系统类型,-h以友好方式显示磁盘大小

free

md
free -h

查看可用内存

du

md
du -sh [*]

统计目录下所有文件大小

wc

md
wc -l

统计数量

ll

md
ll -h

详细列举目录下文件,及文件大小
curl

md
curl -X POST -H "Content-Type: application/json" -d '{"inmap":{"word":"6.5"}}' http://127.0.0.1:8100/nlpApi/release/dictByParam

wget

md
wget --header='Content-Type: application/json' --post-data='{"inmap":{"word":"6.5"}}' --output-document=- http://127.0.0.1:8100/nlpApi/release/dictByParam

iotop

md
yum install iotop
iotop

以非交互模式运行iotop,即不显示实时更新的数据。这对于生成报告或进行批处理操作非常有用。

md
iotop -b

设置更新间隔时间,单位为秒。例如,设置间隔为5秒:

md
iotop -d 5

排除某些进程或线程,不显示它们的I/O信息。例如,排除所有以httpd开头的进程:

md
iotop -e httpd

设置显示的列。例如,只显示进程ID、用户、读写速度和读写字节:

md
iotop -i PID,USER,RW,BW,SW

只显示特定进程的I/O信息。例如,只监控进程ID为1234的进程:

md
iotop -o -p 1234

显示所有进程的I/O信息,包括子进程。这对于分析整个进程树的I/O使用情况非常有用。

md
iotop -P

以树形结构显示进程的I/O信息,这样可以更直观地看到父子进程之间的关系。

md
iotop -t

More

Check out the documentation for the full list of markdown extensions.