查找文件

find / -type d -name *tomcat* 2>/dev/null

按文件/目录大小排序

du -sh * | sort -rn | head

查看进程运行的完整路径

ll /proc/PID

杀进程

pkill -f 进程名称

后台运行

nohup xx >/dev/null 2>&1 &

文本搜索

grep -rni "error" info.log

脚本

1
2
3
#!/bin/bash
day=`date +%Y%m%d%H%M%S`
echo '日期:'$day
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# chkconfig:- 85 15
# description:开机启动
source /etc/profile

# 备份
day=`date +%Y%m%d%H%M%S`
mkdir -p backup/$day
cp -u *.jar backup/$day/
cp -u *.zip backup/$day/

cd update
for process_name in *.jar; do
  echo "$process_name"
  mv -u "$process_name" ..

  # 获取进程ID
  pid=$(pgrep -f $process_name)

  if [[ -n $pid ]]; then
    # 杀死进程
    echo "kill..."
    kill $pid
    
    # 循环检查进程是否退出
    while ps -p $pid > /dev/null; do
        sleep 1
    done
  fi

  # 启动进程
  echo "start..."
  cd ..
  nohup java -jar -Dfile.encoding=utf8 $process_name >/dev/null 2>&1 &
  cd update

  # 检查
  pid=$(pgrep -f $process_name)

  if [[ -n $pid ]]; then
    echo "success"
  else
    echo "fail"
  fi

done

for process_name in *.zip; do
  echo "$process_name"
  mv -u "$process_name" ..

  # 解压
  cd ..
  unzip $process_name

  # 启动进程
  systemctl restart nginx

done

定时任务

1
2
crontab -e #编辑 分 时 日 月 星期 命令
crontab -l #查看

命令大全

日期 date -d '1 day ago' +%Y%m%d

日历 cal

磁盘空间 df

查大文件du -h --max-depth=1 /

空闲内存 free

压缩

zip -r file.zip file

unzip file.zip

代理下载

curl -fLO --socks5 127.0.0.1:10808 url

防火墙

1
2
3
4
5
6
7
8
firewall-cmd --permanent --add-port=8080/tcp #开放端口
firewall-cmd --reload #重启
firewall-cmd --list-all #查看状态
# 关闭
systemctl stop firewalld

#白名单(add改成remove移除)
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.1" port protocol="tcp" port="9200" accept'

授权

chmod +x /etc/profile.d/jdk.sh

修改服务器编码

1
2
3
4
5
6
7
8
vi /etc/bashrc
#添加
LANG="zh_CN.UTF-8"

source /etc/bashrc

#查看
locale

服务器复制目录

1
scp -r root@172.16.0.1:/opt/xxx /opt

开机启动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#1、首先切换到/etc/init.d目录下
cd /etc/init.d

#拷贝要运行的脚本文件到此目录,这个脚本是startMyServer.sh,现在是放在/root/目录下
cp /root/startMyServer.sh /etc/init.d

## 脚本文件startMyServer.sh内容如下:

#!/bin/bash
# chkconfig: - 85 15
# description:开机自启脚本
source /etc/profile
cd /opt/scripts
./serve.sh start

#为这个脚本加上可执行权限
chmod +x startMyServer.sh

#将此脚本加入chkconfig命令进行管理
chkconfig --add startMyServer.sh

#设置该脚本为开机自运行
chkconfig startMyServer.sh on

#移除该自启动脚本,依次执行如下两句即可
chkconfig startMyServer.sh off
chkconfig --del startMyServer.sh

#用CentOS8系统自带的systemctl 命令查看和停止该脚本
systemctl start startMyServer.service #开启该脚本
systemctl restart startMyServer.service #重启动脚本
systemctl stop startMyServer.service  #停止该脚本
systemctl status startMyServer.service #查看服务状态