RPM开机自启+退出保活,再不担心java进程掉了~

Posted NewWorldForU

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RPM开机自启+退出保活,再不担心java进程掉了~相关的知识,希望对你有一定的参考价值。

RPM 一键部署

RPM简介

RPM的全名是Red Hat Package Manager,本意是Red Hat 软件包管理,顾名思义是Red Hat 贡献出来的软件包管理。
简单说,RPM就是将部署统一化,一键化。

RPM优点和缺点

  1. 优点
    由于已经编译完成并且打包,所以安装很方便
    由于套件信息已经记录在Linux主机的数据库中,方便查询、升级与卸载
  2. 缺点
    安装环境必须与打包时的环境一致
    需要满足软件的依赖属性需求
    卸载时需要特别小心,最底层的软件不可以先删除,否则可能造成整个系统出问题

Java 打RPM包

maven 加rpm-maven-plugin插件

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>rpm-maven-plugin</artifactId>
                    <version>2.2.0</version>
                    <executions>
                        <execution>
                            <id>generate-rpm</id>
                            <phase>package</phase>
                            <goals>
                                <goal>rpm</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <defineStatements>
                            <defineStatement>_target_os linux</defineStatement>
                        </defineStatements>
                        <vendor>ZheJiang Yuce Technologies Ltd.</vendor>
                        <license>GPL</license>
                        <defaultDirmode>0755</defaultDirmode>
                        <defaultFilemode>0644</defaultFilemode>
                        <defaultUsername>work</defaultUsername>
                        <defaultGroupname>work</defaultGroupname>
                        <summary>让世界用好数据</summary>
                        <prefix>$rpm.install.home/$project.name</prefix>
                        <version>$project.version</version>
                        <!--suppress UnresolvedMavenProperty -->
                        <release>$git.commit.id.abbrev</release>

                        <mappings>
                            <mapping>
                                <directory>$rpm.install.home/$project.name</directory>
                                <username>work</username>
                                <groupname>work</groupname>
                                <sources>
                                    <source>
                                        <location>$basedir</location>
                                        <includes>
                                            <include>NOTICE</include>
                                            <include>LICENSE</include>
                                            <include>*.md</include>
                                        </includes>
                                    </source>
                                    <source>
                                        <location>target/$project.build.finalName.jar</location>
                                    </source>
                                </sources>
                            </mapping>

                            <mapping>
                                <directory>$rpm.install.home/config/$project.name</directory>
                                <username>work</username>
                                <groupname>work</groupname>
                                <configuration>true</configuration>
                            </mapping>

                            <mapping>
                                <directory>$rpm.install.home/$project.name/logs</directory>
                                <configuration>true</configuration>
                            </mapping>

                            <mapping>
                                <directory>$rpm.install.home/$project.name/lib</directory>
                                <sources>
                                    <source>
                                        <location>target/lib</location>
                                        <includes>
                                            <include>**/*.jar</include>
                                        </includes>
                                    </source>
                                </sources>
                            </mapping>
                            <mapping>
                                <directory>$rpm.install.home/$project.name/bin</directory>
                                <filemode>0500</filemode>
                                <sources>
                                    <source>
                                        <location>$project.basedir/../distribute/bin</location>
                                        <includes>
                                            <include>**/*.sh</include>
                                        </includes>
                                        <filter>true</filter>
                                    </source>
                                </sources>
                            </mapping>
                            <mapping>
                                <directory>/usr/lib/systemd/system</directory>
                                <directoryIncluded>false</directoryIncluded>
                                <filemode>0644</filemode>
                                <username>root</username>
                                <groupname>root</groupname>
                                <sources>
                                    <source>
                                        <location>$project.basedir/../distribute/setup.service</location>
                                        <filter>true</filter>
                                        <destination>yuce-$project.name.service</destination>
                                    </source>
                                </sources>
                            </mapping>

                            <mapping>
                                <directory>/etc/nginx/conf.d</directory>
                                <directoryIncluded>false</directoryIncluded>
                                <username>nginx</username>
                                <groupname>nginx</groupname>
                                <configuration>noreplace</configuration>
                                <sources>
                                    <source>
                                        <location>src/main/resources/nginx</location>
                                        <failIfLocationNotExists>false</failIfLocationNotExists>
                                        <includes>
                                            <include>*.conf</include>
                                        </includes>
                                        <filter>true</filter>
                                    </source>
                                </sources>
                            </mapping>
                        </mappings>
                        <preinstallScriptlet>
                            <scriptFile>$project.basedir/../distribute/rpm/preinst</scriptFile>
                            <fileEncoding>UTF-8</fileEncoding>
                            <filter>true</filter>
                        </preinstallScriptlet>
                        <postinstallScriptlet>
                            <scriptFile>$project.basedir/../distribute/rpm/postinst</scriptFile>
                            <fileEncoding>UTF-8</fileEncoding>
                            <filter>true</filter>
                        </postinstallScriptlet>
                    </configuration>
                </plugin>
            </plugins>


        </pluginManagement>
    </build>

preinst 文件

# !/bin/bash

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

/usr/bin/getent group work > /dev/null || /usr/sbin/groupadd --system work
/usr/bin/getent passwd work > /dev/null || /usr/sbin/useradd --system \\
    --gid work \\
    --home-dir /home/work \\
    --shell /sbin/nologin \\
    work > /dev/null

postinst 文件

# !/bin/bash

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

declare -r APP_CONFIG_DIR="$rpm.install.home/config/$project.name"
declare -r APP_SHELL_DIR="$rpm.install.home/shell/$project.name"

if [ ! -d "$APP_CONFIG_DIR" ] ; then
  /usr/bin/mkdir -p "$APP_CONFIG_DIR"
fi

if [ ! -f "$APP_CONFIG_DIR/env.conf" ]; then
  echo "JAVA_OPTS=-Xmx2048m" > "$APP_CONFIG_DIR/env.conf"
fi

function translate_from_file() 
  filename="$rpm.install.home/config/cloud-eyes/$1"
  if [ ! -f "$filename" ] ; then

    # 基于推动升级的考虑,我们不做软链接反向兼容
    /usr/bin/cp "$2" "$filename"
    # /usr/bin/rmdir --ignore-fail-on-non-empty -- "$(/usr/bin/dirname -- $filename)"
  fi


if [ ! -f "$APP_CONFIG_DIR/application.yaml" ] ; then

  case "$project.name" in
  cloud-eyes)
    translate_from_file "default-customize.yaml" "/home/work/frontend/cloud-eyes-0.0.1-SNAPSHOT/config/default-customize.yaml"
    translate_from_file "application.yaml" "/home/work/frontend/cloud-eyes-0.0.1-SNAPSHOT/config/application.yaml"
    translate_from_file "cloud-eyes-config.yaml" "/home/work/frontend/cloud-eyes-0.0.1-SNAPSHOT/config/cloud-eyes-config.yaml"
    translate_from_file "table-config.yaml" "/home/work/frontend/cloud-eyes-0.0.1-SNAPSHOT/config/table-config.yaml"
    translate_from_file "table-gen.yaml" "/home/work/frontend/cloud-eyes-0.0.1-SNAPSHOT/config/table-gen.yaml"
    ;;

  esac
fi

/usr/bin/chown -R work:work "$APP_CONFIG_DIR"
/usr/bin/chmod -R 0755 "$APP_CONFIG_DIR"

/bin/systemctl preset yuce-$project.name
/bin/systemctl daemon-reload
/bin/systemctl --force enable yuce-$project.name
/bin/systemctl restart yuce-$project.name


.service 文件

[Unit]
Description=Yuce Application Service
After=network.target

[Service]
Type=forking
User=work
Group=work
EnvironmentFile=$rpm.install.home/config/$project.name/env.conf
WorkingDirectory=$rpm.install.home/$project.name
ExecStart=/bin/bash $rpm.install.home/$project.name/bin/restart.sh
#PIDFile=$rpm.install.home/$project.name/logs/run.pid
KillMode=control-group
Restart=on-failure
RestartSec=10
StartLimitInterval=600
StartLimitBurst=50
TimeoutStopSec=3s

[Install]
WantedBy=multi-user.target

RPM 常用命令

rpm 基本命令
安装: rpm -ivh cloud-eyes-0.0.1-96e3ea5.noarch.rpm

升级: rpm -Uvh cloud-eyes-0.0.1-96e3ea5.noarch.rpm

查询已安装的rpm: rpm -qa cloud-eyes

查询某rpm详细信息:rpm -qpi cloud-eyes-0.0.1-094a474.noarch.rpm

卸载: sudo rpm -e cloud-eyes --allmatches --nodeps (强制卸载)

参考文档:
https://blog.csdn.net/Mr_Yang__/article/details/84133783
https://www.cnblogs.com/jhxxb/p/10654554.html
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
https://www.cnblogs.com/harrymore/p/8665154.html

以上是关于RPM开机自启+退出保活,再不担心java进程掉了~的主要内容,如果未能解决你的问题,请参考以下文章

RPM开机自启+退出保活,再不担心java进程掉了~

rpm包安装java jar开机自启

Linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本

Android5.0以上app进程保活的正确姿势

linux程序崩溃自动重启

让Node Red开机自启