在工作中遇到的有关Linux的问题

ifconfig 个字段的含义

此linux服务器为:kylin

接口名称 类型 用途
enp180s0f2 物理以太网接口 物理网络连接
enp25s0f0 物理以太网接口 物理网络连接
vethd989592 虚拟以太网接口对 容器/虚拟机网络通信
vethdc1bcea 虚拟以太网接口对 容器/虚拟机网络通信
virbr0 虚拟网桥 虚拟机NAT网络

设置tomcat 开机自启

1
2
3
4
5
crontab -e
# 在其中添加 下面这行代码(startup.sh 自行更好,只有一行时 & 不要) 保存
reboot sudo -u root /home/debian/tomcat9/bin/startup.sh start &
# 版本为 Kylin Linux ,用下行代码
@reboot /home/windit_data/dataTransfer/apache-tomcat-8.5.100/bin/startup.sh start

调试tomcat

取到tomcat bin 目录下,调用下面这个命令,可以前台运行

1
sh catalina.sh run

启动脚本

1
nohup java -jar /path/XXX.jar &

设置 jar 开机自启

编写脚本文件
start.sh

1
2
3
4
5
6
7
8
#!/bin/bash
nohup java -server -jar XXX.jar > /dev/null 2>&1 &
#nohup 确保即使用户退出登录,Java进程也会继续运行。
#java -server -jar XXX.jar 启动指定的JAR文件。
#-server 是一个JVM选项,表示以服务器模式运行JVM。服务器模式下的JVM通常会进行更多的优化,适合长时间运行的应用程序。
#> /dev/null 将标准输出(控制台输出)丢弃。
#2>&1 将标准错误(错误信息)也丢弃。
#& 将整个命令放在后台执行。

进入 rc.d目录

1
2
cd /etc/rc.d
cat rc.local

执行命令:vim rc.local , 修改rc.local 。按【i】键进入编辑模式,在最后添加代码:

1
2
3
4
5
6
7
8
9
10
11
sleep 60
cd /myApp/test
sh /myApp/test/startup.sh

#第一句为进入你项目所在的目录,我这里把项目放在/myApp/test下
#第二句执行该目录下的sh文件

##说明
#如果不提前进入所在目录,直接执行第二句,也会开机自启动,但是日志文件会在根目录下的log文件中。只有先进入,再执行,项目的日志文件才会在test文件夹下

##test文件中有jar包、startup.sh、以及jar包的日志文件logs

设置权限:

1
2
chmod +x /etc/rc.d/rc.local
chmod +x /myApp/test/startup.sh

定时任务

1
2
# 每月1号和16号各执行一次(适合固定日期)
0 0 1,16 * * /path/to/your_script.sh

linux 查找文件

1.使用 find 命令

find 是一个非常强大的命令行工具,用于在文件系统中搜索文件和目录。它可以在指定的路径下递归查找,并支持复杂的条件匹配。

2.基本用法

1
find /path/to/search -name "filename"

java linux 启动和关闭脚本

启动: sh server.sh start
关闭: sh server.sh stop
server.sh

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
60
61
62
63
64
65
#!/usr/bin/env sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


APP_HOME=$(cd $(dirname $0)/../;pwd) # shell脚本必须指定,因为脚本不会自动加载环境变量,不写的话导致出现此错误
app=$APP_HOME'/app.jar' #jar包的决定路径
args='-server -Xms4096m -Xmx4096m -XX:PermSize=512m -XX:SurvivorRatio=2 -XX:+UseParallelGC' #java程序启动参数,可不写
args1='--spring.config.location='$APP_HOME'/config/application.properties --logging.config='$APP_HOME'/config/logback-spring.xml --db.file='$APP_HOME'/config/config.db --logging.file.path='$APP_HOME'/logs/'
LOGS_FILE=/dev/null # 把打印的日志扔进垃圾桶

cmd=$1 #获取执行脚本的时候带的参数
pid=`ps -ef|grep java|grep $app|awk '{print $2}'` # 抓取对应的java进程



startup(){
aa=`nohup java -jar $args $app $args1 >> $LOGS_FILE 2>&1 &`
echo "nohup java -jar $args $app $args1 >> $LOGS_FILE 2>&1 &"
}

if [ ! $cmd ]; then
echo "Please specify args 'start|restart|stop'"
exit
fi

if [ $cmd = 'start' ]; then
if [ ! $pid ]; then
startup
else
echo "$app is running! pid=$pid"
fi
fi

if [ $cmd = 'restart' ]; then
if [ $pid ]
then
echo "$pid will be killed after 3 seconds!"
sleep 3
kill -9 $pid
fi
startup
fi

if [ $cmd = 'stop' ]; then
if [ $pid ]; then
echo "$pid will be killed after 3 seconds!"
sleep 3
kill -9 $pid
fi
echo "$app is stopped"
fi