Skip to content

ALinux常用命令

Kervin edited this page Jun 9, 2021 · 30 revisions

一、shell脚本

1. 取配置文件属性值

canal_destination=`sed '/canal.destination/!d;s/.*=//' $base/conf/example.properties | tr -d '\r'`
canal_destination=`sed '/canal.destination/!d;s/canal.destination=//' $base/conf/example.properties | tr -d '\r'`
web_sersion=sed '/\"version\"/!d;s/.*://' package.json | tr -d '[:space:]|\"|\,'

2. 配置文件属性赋值

sed -i "s#^canal.destination=.*#canal.destination=$(echo $canal_destination)#g" conf/example.properties
sed -i "s#\"version\":.*#\"version\": \"$(echo $version)\",#g" package.json
sed -i 's/\r//' conf/*.properties

修改xml文件

sed -i "s/<version>$(echo $version)</<version>$(echo $latest)</" pom.xml

sed -i "s#<version>.*#<version>$(echo $latest)</version>#" pom.xml

3. 删除配置文件行

sed -i '1,2d' conf/example.properties

4. 获取Linux服务IP

server_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 0.0.0.0|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

5. shell脚本格式化问题

cd /home/xxx/bin
dos2unix *

vim test.sh
:set fileformat=unix
:wq!

解决问题:syntax error unexpected end of file

6. 获取参数

param=$1
eval A='('$*')'
unset A[0]
unset A[1]
unset A[2]
unset A[3]
#数组长度: ${#A[*]} 或 ${#A[@]}
#数组下标: ${!A[*]} 或 ${!A[@]}
#数组值: ${A[*]} 或 ${A[@]}
#增加JVM参数
for i in ${!A[*]}
do
	OPT=${A[$i]}
	if [[ $OPT == -D* ]];then
		JAVA_OPTS=" $JAVA_OPTS $OPT"
		unset A[$i]
	fi
done      

一、性能监控

1. 用户使用率

sar -u 1 3

2. 网络传输

sar -n DEV 1 3

3. 磁盘IO

iostat -xz 1 3

4. CPU占用情况

mpstat -P ALL 1 3

5. 内存使用情况

free -h

6. 系统负载情况

top
### 带c键查看更多

7. 硬盘使用情况

df -h

8. 硬盘文件夹使用情况

du -h --max-depth=2 /home

9. 查看网卡网口名称

ifconfig
#查看对于IP前的网口名称,如:eth1、p4p1

#centos静态IP设置
vim /etc/sysconfig/network-scripts/ifcfg-ens33

#BOOTPROTO="dhcp"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR=172.21.32.129
PREFIX=24
GATEWAY=172.21.32.2
DNS1=8.8.8.8

#Ubuntu静态IP设置(/etc/netplan/50-cloud-init.yaml)
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eno1:
            dhcp4: false
            addresses:
                - 172.21.32.38/24
            gateway4: 172.21.32.254
            nameservers:
                addresses: [172.21.100.15, 172.21.100.11]
        ens33:
            dhcp4: true
            routes:
                - to: 0.0.0.0/0
                  via: 192.168.154.2
                  metric: 30
        ens38:
            addresses:
                - 172.21.32.112/24
            routes:
                - to: 172.21.0.0/16
                  via: 172.21.32.254
                  metric: 20
    version: 2

#重启网卡
service network restart

10. 查看网口带宽

ethtool p4p1
#带宽参数Speed: 10000Mb/s == 1250 MB/s
Settings for p4p1:
	Supported ports: [ FIBRE ]
	Supported link modes:   1000baseT/Full 
	                        10000baseT/Full 
	Supported pause frame use: Symmetric Receive-only
	Supports auto-negotiation: No
	Advertised link modes:  10000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: No
	Speed: 10000Mb/s
	Duplex: Full
	Port: FIBRE
	PHYAD: 1
	Transceiver: internal
	Auto-negotiation: off
	Supports Wake-on: g
	Wake-on: d
	Current message level: 0x00000000 (0)
#千兆模块
rmmod sk98lin
modprobe sk98lin
#万兆模块
rmmod ixgbe
modprobe ixgbe
#查询百兆/千兆网卡
lspci | grep Ethernet  或者 lspci -vvv | grep Ethernet
18:00.0 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
18:00.1 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
19:00.0 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
19:00.1 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
#这是千兆网卡
# (1)先设置自动
ethtool -s eth4 advertise 0x03F
# (2)设置速度
ethtool -s eno4 speed 1000 duplex full
# (3)设置千兆
ethtool -s eth4 advertise 0x020

advertise的值:
0x001    10 Half
0x002    10 Full
0x004    100 Half
0x008    100 Full
0x010    1000 Half(not supported by IEEE standards)
0x020    1000 Full
0x8000   2500 Full(not supported by IEEE standards)
0x800    10000 Full
0x03F    Auto

参考地址: http://blog.chinaunix.net/uid-15145533-id-2775810.html

11. 查看内存占用前10条情况

ps aux|head -1;ps aux --width 200|sort -rn -k +4|head

12. 查看CPU占用前10条情况

ps aux|head -1;ps aux --width 200|sort -rn -k +3|head

13. 查看端口是否存在

netstat -ano|grep -v grep|grep 8631|awk '{print $4}'|tr -d ":"

14. 查看服务器IP

server_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 0.0.0.0|grep -v 192.168|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

function ipconf() {
    case "`uname`" in
        Darwin)
         server_ip=`echo "show State:/Network/Global/IPv4" | scutil | grep PrimaryInterface | awk '{print $3}' | xargs ifconfig | grep inet | grep -v inet6 | awk '{print $2}'`
         ;;
        *)
         server_ip=`ip route get 1 | awk '{print $NF;exit}'`
         ;;
  esac
  echo ${server_ip}
}

15. 设置主机名

sysctl kernel.hostname=dbs-server02
echo 'dbs-server02' > /etc/hostname

16. 内核优化

#禁止交换区和最大映射文件数(/etc/sysctl.conf)
vm.swappiness = 1
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 262144
vm.max_map_count = 655360
fs.file-max = 6553560

#刷新生效
sysctl -p
#内存锁定以及文件句柄优化(/etc/security/limits.conf)
*            soft    nofile          65536
*            hard    nofile          131072
*            soft    nproc           8096
*            hard    nproc           8096
*            soft    memlock         unlimited
*            hard    memlock         unlimited

17. 挂载nfs硬盘

umount -f /home/deploy/sync
mkdir -p /home/deploy/sync
mount 172.21.32.120:/data/images /home/deploy/sync
echo '172.21.32.120:/data/images  /home/deploy/sync  nfs    defaults 0      0' >> /etc/fstab

18. 查看CPU情况

#CPU信息
cat /proc/cpuinfo
# 逻辑CPU即VCPU个数
cat /proc/cpuinfo | grep "processor" | wc -l
# 物理CPU个数
cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l
# 每个CPU的核数
cat /proc/cpuinfo | grep "core id" | uniq |  wc -l

三、命令

1. 查杀进程

kill -9 `ps -ef|grep BackendMain|grep -v grep|awk '{print $2}'`
kill -9 $(netstat -anp|grep -v grep|grep ${HTTP_PORT}|grep 'LISTEN'|awk '{print $7}'|tr -d "/node")

2. 查java进程

jps -m -v

3. 防火墙

iptables -A INPUT -p tcp -d 10.10.1.189 --dport 24999 -j ACCEPT
iptables -A OUTPUT -p tcp -d 10.10.1.189 --sport 24999 -j ACCEPT
iptables -L -n --line-number
iptables -D INPUT 1
iptables -D OUTPUT 1

及时开启指定端口 /etc/sysconfig/iptables

#重启iptables 
service iptables save && service iptables restart
#关闭防火墙
chkconfig iptables off && service iptables stop
#
/etc/init.d/iptables save 
/etc/init.d/iptables restart
/usr/sbin/iptables save
/usr/sbin/iptables restart

centos系统

#关闭防火墙
systemctl stop firewalld.service
#禁止防火墙
systemctl disable firewalld.service

ubuntu系统

#开启防火墙
ufw enable
systemctl start ufw
#关闭防火墙
ufw disable
systemctl stop ufw

4.创建文件目录

cd /home/docker
mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf

5.远程拷贝文件或目录

scp -r /home/deploy [email protected]:/home
scp -r [email protected]:/home/deploy /home
rsync -a /home/deploy [email protected]:/home
rsync -a [email protected]:/home/deploy /home 

6.执行计划

crontab -e 
Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano
  2. /usr/bin/vim.basic        <---- easiest
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2
0 22 * * * /home/deploy/bak/backup_data_mysql.sh > /dev/null 2>&1
0 0 * * * docker system prune -a -f  > /dev/null 2>&1

开启日志

vim /etc/rsyslog.d/50-default.conf
#将cron前面的注释符去掉
cron.*              /var/log/cron.log
#重启rsyslog
service rsyslog  restart 
tail -f /var/log/cron.log

修改crontab编辑方式

select-editor

7.Linux桌面

#关闭桌面
systemctl set-default multi-user.target
#开启桌面
systemctl set-default graphical.target

8.Linux修改时区

tzselect

按步骤进行时区选择

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
date --s="2019-11-01 13:06:00"
hwclock -w
hwclock --hctosys
hwclock --localtime --systohc
timedatectl set-time "2019-11-01 13:06:00"
timedatectl
echo "Asia/Shanghai" > /etc/timezone

备注:开启NTP服务

timedatectl set-local-rtc 1
timedatectl set-ntp 1
hwclock -w
hwclock --hctosys
hwclock --localtime --systohc

ntp 服务校时: time1-7.aliyun.com

四、脚本

1. restart.sh

#!/bin/bash
export BUILD_ID=bash_build_`date "+%s%3N"`
BASE_PATH="${BASH_SOURCE-$0}"
BASE_PATH="$(dirname "${BASE_PATH}")"
BASE_PATH="$(cd "${BASE_PATH}";pwd)"
BASE_PATH=${path}/..
BASE_PATH="$(cd "${BASE_PATH}";pwd)"
echo "BASE_PATH:${BASE_PATH}"
cd ${BASE_PATH}
case "`uname`" in
    Linux)
		linux=true
		;;
	*)
		linux=false
		;;
esac

function localIP() {
    case "`uname -v`" in
        *Darwin*)
         server_ip=`echo "show State:/Network/Global/IPv4" | scutil | grep PrimaryInterface | awk '{print $3}' | xargs ifconfig | grep inet | grep -v inet6 | awk '{print $2}'`
         ;;
        *Ubuntu*)
        server_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 0.0.0.0|grep -v 192.168|grep -v inet6|grep -v 0.1|awk '{print $2}'`
		 ;;
        *)
		server_ip=`ip route get 1 | awk '{print $NF;exit}'`
         ;;
    esac
  	echo ${server_ip}
}

function opts(){
	eval A='('$*')'
	for i in ${!A[*]}
	do
		OPT=${A[$i]}
		if [[ $OPT == -D* ]];then
			JAVA_OPTS=" $JAVA_OPTS $OPT"
			unset A[$i]
		fi
	done
	echo ${JAVA_OPTS}
}

function active(){
	eval A='('$*')'
	for i in ${!A[*]}
	do
		OPT=${A[$i]}
		if [[ $OPT == -Dspring.profiles.active* ]];then
			ENV_ACTIVE=${OPT##*=}
		fi
	done
	echo ${ENV_ACTIVE}
}

CONF=${BASE_PATH}/config/application.properties
ENV_ACTIVE=`active $*`
if [ -n "${ENV_ACTIVE}" ] ; then
	echo "ENV:${ENV_ACTIVE}"
	sed -i "s#^spring.profiles.active=.*#spring.profiles.active=$(echo ${ENV_ACTIVE})#g" $CONF
fi

SPRING_BOOT=platform-manager-svc
LOG_CONF=${BASE_PATH}/config/log4j2.properties
ENV_NAME=`sed '/spring.profiles.active/!d;s/.*=//' $CONF | tr -d '\r'`
echo "ENV_ACTIVE:${ENV_NAME}"
APP_NAME=${SPRING_BOOT}-${ENV_NAME//,/_}
HTTP_PORT=`sed '/server.port/!d;s/.*=//' $CONF | tr -d '\r'`
SERVER_IP=`localIP`

if [ -n "${APP_NAME}" ] ; then
	kid=`ps -ef |grep ${APP_NAME}|grep -v grep|awk '{print $2}'`
	echo [${SERVER_IP}]pid[$kid] from `uname` system process!
fi

if [ -n "$kid" ] ; then
	echo [${SERVER_IP}] ${APP_NAME} process [$kid] is Running!
	kill -9 $kid;
	sleep 5;
fi

if [ -n "${HTTP_PORT}" -a ! -z "${HTTP_PORT}" ] ; then
	occupy=`netstat -ano|grep -v grep|grep ${HTTP_PORT}|grep 'LISTEN'`
	if [ -n "$occupy" ] ; then
		echo [${SERVER_IP}] Port[${HTTP_PORT}] is occupied!
		echo $occupy
		exit 1
	fi
fi

if [ ! -d ${BASE_PATH}/logs ] ; then
	mkdir -p ${BASE_PATH}/logs
else
	rm -rf ${BASE_PATH}/logs/*
fi

if [ ! -d ${BASE_PATH}/data ] ; then
	mkdir -p ${BASE_PATH}/data
fi

if [ "$JAVA_HOME" != "" ]; then
  JAVA="$JAVA_HOME/bin/java"
else
  JAVA=java
fi
JAVA_ENV="`opts $*` -server -Xms512M -Xmx512M -Xss1m"
JAVA_OPTS="$JAVA_ENV -DAPP_NAME=${APP_NAME} -Dbase.path=${BASE_PATH} -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -Djdk.io.permissionsUseCanonicalPath=true -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j.skipJansi=true -XX:+HeapDumpOnOutOfMemoryError "

for i in "${BASE_PATH}"/lib/*.jar
do
    CLASSPATH="$i:$CLASSPATH"
done

if [ -e $CONF -a -d ${BASE_PATH}/logs ]
then
	set timeout 60
	echo -------------------------------------------------------------------------------------------
	echo ------$JAVA_OPTS
	cd ${BASE_PATH}
	for file in "${BASE_PATH}"/*.jar
	do
	    file=${file##*/}
	    filename=${file%.*}
	    if [[ $file == *-sources.jar ]];then
	    	continue;
	    fi
	    app=$file
	    echo -----------------file=${file},filename=${filename},app=$app------------------
	    if [[ $filename =~ $SPRING_BOOT ]]; then
	    	app=$file
	    	echo app jar:$app
	    	break;
	    fi
	done
	
	CLASSPATH="${BASE_PATH}/config:$CLASSPATH";
	
	echo ${APP_NAME} Starting ...
	$JAVA $JAVA_OPTS -classpath .:$CLASSPATH -jar $app ${A[*]} --logging.config=$LOG_CONF >/dev/null 2>${BASE_PATH}/logs/error.log &
	echo ${APP_NAME} Finish ...
	DEV_LOOPS=0;
	while(true);
	do
		sleep 5;
		if $linux; then
			kpid=`ps -ef|grep java|grep "${APP_NAME}"|grep -v grep|awk '{print $2}'`
		else 
			kpid=`ps -ef|grep java|grep "${APP_NAME}"|grep -v grep|awk '{print $2}'`
		fi
		
		if [ "${kpid}" != "" ] ; then
			echo "[pid:${kpid}]OK! cost:${DEV_LOOPS}"
			break;
		fi
		
		if [ ${DEV_LOOPS} -gt 10 ] ; then
			echo "[pid:${kpid}]NO! cost:${DEV_LOOPS}"
			break;
		else
			let DEV_LOOPS=${DEV_LOOPS}+1;
			printf "...";
		fi
	done;
	
  	if [ "${kpid}" != "" ] ; then
		echo "=========>`hostname`(${SERVER_IP}):${APP_NAME}[pid:${kpid}]STARTUP SUCCESS!"
	else
		echo "=========>`hostname`(${SERVER_IP}):${APP_NAME}[pid:${kpid}]STARTUP FAIL!"
		exit 1
	fi
	echo -------------------------------------------------------------------------------------------
else
	echo "${APP_NAME} config([$ENV_NAME]$CONF) Or logs direction is not exist,please create first!"
	exit 1
fi
Clone this wiki locally