Linux

文章目录

常用命令

# 解压   
tar -zxvf 

#移除目录:
rm -rf [目录名字]

#统计某个字符出现的次数
grep -o objStr  filename|wc -l
#如果是多个字符串出现次数,可使用:#直接用\| 链接起来即
grep -o ‘objStr1\|objStr2'  filename|wc -l  

#从某个字符出现开始查询
sed -n '/XXXXXXXXXXXXXXX/,// p' a.txt |more

#查看端口被占用进程
lsof -i :9105
netstat -tunlp |grep 9105

#安装jdk
yum -y list java*  # 列出可安裝
yum install -y java-1.8.0-openjdk-devel.x86_64   #安裝

运行jar

#1、使用XShell登录目标机器

#2、查看占用端口的PID
lsof -i:[端口号] 

#3、杀死进程
kill -s 9 [进程号]

#4、移除上版的jar包
rm -rf /root/jar/xxxxx.jar
#5、上传程序至/root/jar/

#6、使用命令,启动程序
nohup java -jar ./xxxx.jar --spring.profiles.active=prod >xxxx.out 2>&1 &

#7、查看日志
tail -f xxxx.out

启动Tomcat

# 启动服务的方式
systemctl start tomcat.service

# 查找tomcat进程
 ps -ef|grep tomcat 
# 启动
 ./startup.sh
# 停止
 ./shutdown.sh
# 编辑配置
  vi catalina.sh
# 结束进程
kill -9 31968

# 赋予执行sh权限
chmod +x  xxx.sh给执行权限

# 查看动态的日志
tail -f catalina.out  

设置固定ip

网络适配器为NAT模式

#编辑文件
vim /etc/sysconfig/network-scripts/ifcfg-ens33

#修改
BOOTPROTO=static #这里讲dhcp换成static
ONBOOT=yes #将no换成yes
#新增
IPADDR=192.168.10.10 #静态IP
GATEWAY=192.168.10.2 #默认网关,需要再vmware中查看这个网关
NETMASK=255.255.255.0 #子网掩码

#保存文件
:wq

#重启网络
systemctl restart network

防火墙开放端口

#查看状态
firewall-cmd --state //running 表示运行
#重新加载
firewall-cmd --reload
#开放端口 permanent 永久
firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp
#关闭端口
firewall-cmd --permanent --zone=public --remove-port=8080-8081/tcp
#开放端口 permanent 临时
firewall-cmd --zone=public --add-port=8080-8081/tcp
#查看所有开放服务
firewall-cmd --permanent --zone=public --list-services
#查看所有开放端口
firewall-cmd --permanent --zone=public --list-ports
#查看所有开放端口
 iptables -L -n

执行etl工具kettle

# 1、赋权限制:
chmod +x ./data-integration/*.sh
# 2、查看权限:
ls -l  ./data-integration
# 3、查看部署成功:
./data-integration/kitchen.sh
# 4、运行job: 
/root/etl/data-integration/kitchen.sh -file=/root/etl/file/CDR_NURSING.kjb  -log=log.log
# 5、运行sh:
sh CDR_NURSING.sh

# 6、CDR_NURSING.sh文件
#!/bin/sh
/root/etl/data-integration/kitchen.sh -file=/root/etl/file/CDR_NURSING.kjb  -logfile=/root/etl/file/logs/LOG_$(date -d "today" +"%Y%m%d_%H%M%S").log

启动停止多个jar

启动文件设置

多文件同时启动

gollum.sh

#!/bin/sh
#chkconfig: 2345 80 05
#description: gollum

##jar包
export EUREKA=/root/gollum/jar/server-eureka-1.0.jar
export ZUUL=/root/gollum/jar/server-zuul-1.0.jar
export LAB=/root/gollum/jar/service-laboratory-1.0.jar
export EXAM=/root/gollum/jar/service-examination-1.0.jar
export CONSULT=/root/gollum/jar/service-consultation-1.0.jar

##端口
export EUREKA_port=8000
export ZUUL_port=8080
export LAB_port=9106
export EXAM_port=9107
export CONSULT_port=9110

##日志
export EUREKA_log=/root/gollum/jar/log/eureka.out
export ZUUL_log=/root/gollum/jar/log/zuul.out
export LAB_log=/root/gollum/jar/log/laboratory.out
export EXAM_log=/root/gollum/jar/log/examination.out
export CONSULT_log=/root/gollum/log/consultation.out

##配置文件,dev/prod
export spring_config=prod
 
case "$1" in
 
start)
        ## 启动EUREKA
        echo "--------EUREKA 开始启动--------------"
        nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 &
        EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$EUREKA_pid" ]
            do
              EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "EUREKA pid is $EUREKA_pid" 
        echo "--------EUREKA 启动成功--------------"
 
        ## 启动ZUUL
        echo "--------开始启动ZUUL---------------"
        nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 &
        ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$ZUUL_pid" ]
            do
              ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "ZUUL pid is $ZUUL_pid"     
        echo "---------ZUUL 启动成功-----------"
		
		## 启动LAB
        echo "--------开始启动LAB---------------"
        nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 &
        LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$LAB_pid" ]
            do
              LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "LAB pid is $LAB_pid"     
        echo "---------LAB 启动成功-----------"
		
		## 启动EXAM
        echo "--------开始启动EXAM---------------"
        nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 &
        EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$EXAM_pid" ]
            do
              EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "EXAM pid is $EXAM_pid"     
        echo "---------EXAM 启动成功-----------"
		
		## 启动CONSULT
        echo "--------开始启动CONSULT---------------"
        nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 &
        CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$CONSULT_pid" ]
            do
              CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "CONSULT pid is $CONSULT_pid"     
        echo "---------CONSULT 启动成功-----------"
		
		echo "===startAll success==="
        ;;
 
 stop)
        P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===EUREKA process not exists or stop success"
        else
            kill -9 $P_ID
            echo "EUREKA killed success"
        fi
		P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ZUUL process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ZUUL killed success"
        fi
		P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===LAB process not exists or stop success"
        else
            kill -9 $P_ID
            echo "LAB killed success"
        fi
		P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===EXAM process not exists or stop success"
        else
            kill -9 $P_ID
            echo "EXAM killed success"
        fi
		P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===CONSULT process not exists or stop success"
        else
            kill -9 $P_ID
            echo "CONSULT killed success"
        fi	
		
        echo "===stopAll success==="
        ;;   
 
restart)
        $0 stop
        sleep 2
        $0 start
        echo "===restartAll success==="
        ;;   
esac	
exit 0

可单文件启动

gullum-one.sh

#!/bin/sh
#chkconfig: 2345 80 06
#description: gollum

export EUREKA=/root/gollum/jar/server-eureka-1.0.jar
export ZUUL=/root/gollum/jar/server-zuul-1.0.jar
export LAB=/root/gollum/jar/service-laboratory-1.0.jar
export EXAM=/root/gollum/jar/service-examination-1.0.jar
export CONSULT=/root/gollum/jar/service-consultation-1.0.jar
 
export EUREKA_port=8000
export ZUUL_port=8080
export LAB_port=9106
export EXAM_port=9107
export CONSULT_port=9110

export EUREKA_log=/root/gollum/jar/log/eureka.out
export ZUUL_log=/root/gollum/jar/log/zuul.out
export LAB_log=/root/gollum/jar/log/laboratory.out
export EXAM_log=/root/gollum/jar/log/examination.out
export CONSULT_log=/root/gollum/log/consultation.out

##配置文件,dev/prod
export spring_config=prod
 
case "$1" in
 
start)
	case "$2" in
		
		eureka|EUREKA)
			## 启动EUREKA
			echo "--------EUREKA 开始启动--------------"
			nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 &
			EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`
			until [ -n "$EUREKA_pid" ]
				do
				  EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "EUREKA pid is $EUREKA_pid" 
			echo "--------EUREKA 启动成功--------------"
			;;
			
		zuul|ZUUL)
			## 启动ZUUL
			echo "--------开始启动ZUUL---------------"
			nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 &
			ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$ZUUL_pid" ]
				do
				  ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "ZUUL pid is $ZUUL_pid"     
			echo "---------ZUUL 启动成功-----------"
			;;
			
		lab|LAB)
			## 启动LAB
			echo "--------开始启动LAB---------------"
			nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 &
			LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$LAB_pid" ]
				do
				  LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "LAB pid is $LAB_pid"     
			echo "---------LAB 启动成功-----------"
			;;
		exam|EXAM)
			## 启动EXAM
			echo "--------开始启动EXAM---------------"
			nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 &
			EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$EXAM_pid" ]
				do
				  EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "EXAM pid is $EXAM_pid"     
			echo "---------EXAM 启动成功-----------"
			;;
		consult|CONSULT)
			## 启动CONSULT
			echo "--------开始启动CONSULT---------------"
			nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 &
			CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$CONSULT_pid" ]
				do
				  CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "CONSULT pid is $CONSULT_pid"     
			echo "---------CONSULT 启动成功-----------"
			;;
			
    esac
	;;
 
 stop)
	case "$2" in
		
		eureka|EUREKA)
			P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===EUREKA process not exists or stop success"
			else
				kill -9 $P_ID
				echo "EUREKA killed success"
			fi
			;;
			
		zuul|ZUUL)
			P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===ZUUL process not exists or stop success"
			else
				kill -9 $P_ID
				echo "ZUUL killed success"
			fi
			;;
						
		lab|LAB)
			P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===LAB process not exists or stop success"
			else
				kill -9 $P_ID
				echo "LAB killed success"
			fi
			;;
		exam|EXAM)
			P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===EXAM process not exists or stop success"
			else
				kill -9 $P_ID
				echo "EXAM killed success"
			fi
			;;
		consult|CONSULT)
			P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===CONSULT process not exists or stop success"
			else
				kill -9 $P_ID
				echo "CONSULT killed success"
			fi
			;;
					
	esac
	;;
 
restart)
        $0 stop $2
        sleep 2
        $0 start $2
        echo "===restart $2 success==="
        ;;   
esac	
exit 0

启动命令

#全部jar执行
启动:./gollum.sh start
停止:./gollum.sh stop
重启:./gollum.sh restart

#单个jar执行
启动:./gollum-one.sh start eureka 其他服务类似
停止:./gollum-one.sh stop eureka 其他服务类似
重启:./gollum-one.sh restart eureka  

开机启动

#文件授权:
chmod +x /etc/init.d/gollum.sh
chkconfig --list
#脚本添加到启动服务中: 
chkconfig --add  gollum.sh
#脚本启动服务中移除:
chkconfig --del  gollum.sh
#启动服务: 
chkconfig  gollum.sh on

#最后reboot重启系统即可

service gollum.sh start
service gollum.sh stop

常见问题

/bin/bash^M: 坏的解释器: 没有那个文件或目录

执行shell脚本是报错:/bin/bash^M: 坏的解释器: 没有那个文件或目录是因为该文件在windows系统上打开过,关闭后其中的空格符号和Linux的不同,导致这个报错,我们可以通过sed命令与正则的配合将文件中的空格符号替换成linux的空格

sed -i 's/\r$//' java.sh 

-bash: ./java.sh: 权限不够

chmod +7 java.sh

防火墙端口设置

# 添加指定需要开放的端口:
firewall-cmd --add-port=123/tcp --permanent
# 重载入添加的端口: 
firewall-cmd --reload
# 查询指定端口是否开启成功: 
firewall-cmd --query-port=123/tcp
上一篇:【已解决】在conda环境中运行Jupyter lab/ Jupyter notebook 无法加载该环境


下一篇:Why Lab探索实验室成员厦大区块链研究中心看区块链应用