shell总结

jar包启停

启动jar包脚本

#!/bin/sh
nohup java -jar  ./overseasNews.jar > /dev/null 2>&1 &
echo `jps | grep overseasNews.jar | grep -v grep ` " started!!!"

启动脚本2

#!/bin/bash
mypath=.
mainclass=com.isi.JdbcTest
for f in lib/*.jar
do
        mypath=$mypath:$f
done

java -Xmx2g -cp $mypath $mainclass

更简洁的写法示例:

#!/bin/bash
nohup java -cp data_monitor.jar:lib/* casia.basic.analysisgroup.monitor.main.DataMonitor "sj4" &

windows下用;代替:号

#!/bin/bash
nohup java -cp data_monitor.jar;lib/* casia.basic.analysisgroup.monitor.main.DataMonitor "sj4" &

停止指定线程脚本

#!/bin/sh
info=`ps -ef|grep  overseasNews.jar  |grep -v grep`
echo "killing thread info ####### " $info
pid=`echo $info |awk '{print $2}' `
kill -9 $pid
echo "pid " $pid " ##### killed!!!!"

停止线程脚本

kill -9 `ps -ef|grep harm_mirror_crawler.jar | grep -v grep|awk '{print $2}'`

两个脚本合并,通过输入参数控制启动和停止

#!/bin/sh
 if [ $# = 1 ] && [ $1 = "start" ] ; then
   nohup java -jar  ./overseasNews.jar > /dev/null 2>&1 &
   echo `jps | grep overseasNews.jar | grep -v grep ` " started!!!"
 elif [ $# = 1 ] && [ $1 = "stop" ] ; then
   info=`ps -ef|grep  overseasNews.jar  |grep -v grep`
   echo "killing thread info ####### " $info
   pid=`echo $info |awk '{print $2}' `
   kill -9 $pid
   echo "pid " $pid " ##### killed!!!!"
 else
  echo " arg error the only argument is start/stop" 
 fi

 批量启停

#!/bin/bash
dir=`pwd`
server=`ls -d *server*`
echo "server path is "$dir/$server
cd $dir/$server
sh ./sbin/start.sh
echo "server is starting wait for 10 s"
sleep 10s 
cd $dir
for i in `ls  -d  client*`
do 
  cd $dir/$i
  echo "client path is==>"  $dir/$i                                                                                                                                                                              
  sh ./sbin/start.sh
  sleep 1s
done

批量计算文件总行数

如果文件总量很大的化建议累加计算

#!/bin/bash
count=0
for i in `ll "/home/temp" | grep "new_"  | awk '{print $9}'`
do
 num=`cat $i | wc -l`
 echo $i " row num is " $num
 count=$((${count} + $num))
done
echo "total rows==" $count

如果文件总量不大可以简化

cat /home/temp/new_* | wc -l


网络

#查看端口是否通

wget 192.167.1.1:2233

#查看端口占用

netstat  -anp  |grep  3306

#可以显示详细的端口连接信息,可以查看和本端口通信的有哪些ip

lsof -i:18180

#直接显示连接的数量:

netstat -an |grep 'ESTABLISHED' |grep -i '18180' |wc -l

#查看进程位置

ll /proc/进程号批量启动

#开放端口

/sbin/iptables -I INPUT -p tcp --dport 20880 -j ACCEPT
/etc/init.d/iptables save 
service iptables restart

centos7关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

#查看本机公网ip

curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i

日志清理

批量清空

for i in `find . -name "*.out"`; do cat /dev/null >$i; done
find  .  -name  '*log'  -type  f  -print  -exec   truncate -s 0 {} \;   

批量删除

find  .  -name  '*.log'  -type  f  -print  -exec  rm  -rf  {} \;
for i in `find . -name "*.out"`; rm -rf >$i; done

注意:如果使用for循环的批量删除和清空,磁盘空间如果满了,就会报 No space left on device,使用find的 -exec就不会

 递归删除

find  .  -name  '*.exe'  -type  f  -print  -exec  rm  -rf  {} \;

查找指定时间段

find ./     -type f  -newermt '2022-08-09 00:00:00' ! -newermt '2022-08-10 00:00:00'

监控

监控日志,重启卡死程序

#!/bin/bash
dir=`pwd`
while true
do
cd $dir
for i in `ls  -d  client*/`
do 
  cd $dir/$i
  echo "client path is==>"  $dir/$i
  size=`cat $dir/$i/nohup.out | wc -l`
  if [ $size == 0 ] 
  then
    sh ./sbin/stop-client.sh                                                                                                                                                                                     
    sh ./sbin/start-client.sh
    echo "size is 0 restart client!!!"
  else
    echo "logsize="$size
  fi
done
 echo "scan over sleep 1 min!!!!"
 sleep 60s
done

监控进程是否存在

#!/bin/bash
source /etc/profile
path=$(dirname $(readlink -f "$0"))/
cd $path
size=`ps -ef|grep 'tomcat'|grep $path |  grep -v grep|awk '{print $2}' | wc -l`
if [  0 == $size  ] 
then
 echo "process is dead do my reset job"
 bash ./bin/startup.sh
else 
 echo "process is alive"
fi

重启脚本

#!/bin/bash
SHELL_FOLDER=$(dirname $(readlink -f "$0"))
echo $SHELL_FOLDER
cd $SHELL_FOLDER
bash ./sbin/stop.sh
bash ./sbin/start.sh

#进入启动脚本的目录

cd `dirname $0`

遍历文件夹

#!/bin/bash
path=`pwd`
for i in `ls -l |grep ^d | awk '{print $9}' | grep shot_spider`
do
  pro_path=$i
  echo "sub_path="$i
  cd $i
  bash ./sbin/start.sh
  cd $path
done

权限

1、用户列表文件:/etc/passwd/
2、用户组列表文件:/etc/group

3、查看系统中有哪些用户

cut -d : -f 1 /etc/passwd

4、查看可以登录系统的用户

cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1

5、查看系统中有哪些用户组

cut -d : -f 1 /etc/group

6、查看可以登录系统的用户组

cat /etc/group | grep -v /sbin/nologin | cut -d : -f 1

7、查看用户组下的用户

#获取指定用户组以及id
cut -d : -f 1,3 /etc/group  | grep isi
返回结果:isi:1000
#查看该用户组下的所有用户
cat /etc/passwd | grep "1000" | cut -d : -f 1

8、查看用户操作:w命令(需要root权限)

9、查看某一用户:w 用户名

10、查看登录用户:who

11、查看用户登录历史记录:last

12、修改root用户密码:passwd

日志翻动

#遇到日志被压缩的场景

cat  xxx | grep -a 

#从后往前翻动日志

less xxx,然后shift+G,上箭头翻动日志

其他常用 

循环请求

#!/bin/bash
for ((i=1; i<=127; i ++))  
do  
    curl is ""
    sleep 4s
done 

压缩与解压

tar -cvf test.zip test
tar -zxvf test.zip

日志清除脚本 flush_log.sh

#!/bin/bash
#########################################
#定时删除日志:
# 1.删除超过保存日期的日志
# 2.将超过指定大小的日志重定向到/dev/null
#########################################
 DatesNum=1 #保存天数 
 Size=1024 #大小限制单位M
#rm超过DatesNum天数的 *.log*
log_name="*.log"
nohup_name="*.out"
dir="/root/site_processor"
for i in `find $dir  -mtime +"$DatesNum" -name $log_name".*"`;
  do
    echo "rm over time file $i";
      rm $i;
 done

 #重写大小大于SizeM的 *.log 文件到/dev/null
 for i in `find $dir -size +"$Size"M -name "$log_name"`;
  do
    echo "clean over zie file $i";
     cat /dev/null > $i;
  done


 for i in `find  $dir -size +"$Size"M -name "$nohup_name"`;
  do
    echo "clean over zie file $i";
     cat /dev/null > $i;
  done


echo "delete log finished!"

find总结

#根据修改时间范围查找并删除
find ./ -type f -newermt '2024-06-15 00:00:00' ! -newermt '2024-06-19 00:00:00' -print -delete

#查找创建时间在10天之前的文件打印路径并删除
find ./ -type f -ctime +10 -print -exec rm -f {} \;

#根据文件名匹配查找并删除文件
find ./ -type f  -name "info.log.2024-04*" -print -delete

#查找指定文件内容并输出路径
find  / -type f -name "*.py" -print0 | xargs -0 grep -l "xxx"
 

ip checker

check.sh

#!/bin/bash

# 要检查的端口号
IP=$1
PORT=$2
# 要检查的主机地址
HOST="http://$IP"

# 使用wget尝试连接指定端口,并且检查返回的响应
wget -q -O- "$HOST:$PORT" &> /dev/null

# 检查wget的退出状态码
if [ $? -eq 0 ]; then
    echo "端口 $IP:$PORT 通"
else
    echo "端口 $IP:$PORT 不通"
fi

 batch_check.sh

#!/bin/bash

# 检查传入的参数
if [ "$#" -ne 1 ]; then
    echo "用法: $0 <aa.txt 文件路径>"
    exit 1
fi

# 读取包含IP和端口的文件
INPUT_FILE="$1"

# 检查文件是否存在
if [ ! -f "$INPUT_FILE" ]; then
    echo "文件 $INPUT_FILE 不存在"
    exit 1
fi

# 循环遍历文件中的每一行
while IFS=: read -r IP PORT
do
    # 调用check.sh脚本检查端口
    bash ./check.sh "$IP" "$PORT"
done < "$INPUT_FILE"

maven导出项目依赖的命令:
 

mvn dependency:copy-dependencies -DoutputDirectory=D:/easyword

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值