原创

常见应用在Centos下配置开机启动的方法

查询Centos的版本

通过 cat /etc/redhat-release 查询centos版本,请根据你的Centos版本来确认使用哪种方式来设置开机启动

Centos7.x

nginx

如果是自己在服务器手动编译的nginx,请先走第一步创建nginx.service文件,如果是直接使用yum install安装的nginx请直接走第二步:
1、创建nginx.service文件

vi /etc/systemd/system/nginx.service

写入以下内容
其中ExecStart、ExecReload、ExecStop 请根据自己安装的nginx实际路径来填写

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/app/nginx/sbin/nginx
ExecReload=/app/nginx/sbin/nginx -s reload
ExecStop=/app/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2、 配置自动启动

systemctl daemon-reload
systemctl enable nginx.service 
systemctl status nginx.service

mysql

拷贝脚本到/etc/init.d

cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

vi打开/etc/init.d/mysqld文件,找到basedir和datadir的路径,修改为你自己的部署路径
添加开机启动服务

chkconfig --add mysqld
chkconfig --level 345 mysqld on
chkconfig --list

tomcat

进入目录创建tomcat.service

cd /etc/systemd/system
vi tomcat.service

写入以下内容

tomcat的启动和停止脚本需要按照实际路径填写

[Unit]
Description=tomcat
After=network.target

[Service]
Type=oneshot
ExecStart=/app/tomcat/bin/startup.sh
ExecStop=/app/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

设置开机启动

systemctl daemon-reload
systemctl enable tomcat
systemctl status tomcat

springboot应用

进入目录创建xxxx.service文件 (xxxx为应用名称)

以下会用dtmp代替xxxxx

cd /etc/systemd/system
vi dtmp.service

写入以下内容

启动脚本和停止脚本路径请按照实际路径填写

[Unit]
Description=dtmp
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/app/dtmp/start-dtmp.sh
ExecStop=/app/dtmp/stop-dtmp.sh
ExecRestart=/app/dtmp/restart.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

配置服务开机启动

systemctl daemon-reload
systemctl enable dtmp
systemctl status dtmp

Centos6.x

nginx

里面的nginx路径和配置文件路径请按照实际部署路径来填写
如果不需要指定配置文件请注释

在/etc/init.d/下添加nginxd文件
vi /etc/init.d/nginxd

#!/bin/bash  
#  
#chkconfig: - 85 15  
#description: Nginx is a World Wide Web server.  
#processname: nginx  

nginx=/usr/sbin/nginx
#conf=/usr/local/nginx/nginx.conf

case $1 in  
       start)  
              echo -n "Starting Nginx"  
              #$nginx -c $conf
              $nginx
              echo " done"  
       ;;  

       stop)  
              echo -n "Stopping Nginx"  
              killall -9 nginx  
              echo " done"  
       ;;  

       test)  
              #$nginx -t -c $conf
              $nginx -t
       ;;  

        reload)  
              echo -n "Reloading Nginx"  
              ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP  
              echo " done"  
       ;;  

        restart)  
              $0 stop  
              $0 start  
       ;;  

       show)  
              ps -aux|grep nginx  
       ;;  

       *)  
              echo -n "Usage: $0 {start|restart|reload|stop|test|show}"  
       ;;  

esac

添加执行权限并添加到服务中

chmod +x /etc/init.d/nginxd
chkconfig --add nginxd
chkconfig nginxd on
chkconfig --list

mysql

拷贝脚本到/etc/init.d

cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

vi打开/etc/init.d/mysqld文件,找到basedir和datadir的路径,修改为你自己的部署路径
添加开机启动服务

chkconfig --add mysqld
chkconfig --level 345 mysqld on
chkconfig --list

tomcat

创建/etc/init.d/tomcat文件
vi /etc/init.d/tomcat

#!/bin/bash 
# 
# tomcat startup script for the Tomcat server 
# 
# chkconfig: 345 80 20 
# description: start the tomcat deamon 
# 
# Source function library 
. /etc/rc.d/init.d/functions 
prog=tomcat 
CATALANA_HOME=/data/apache-tomcat-8.0.8/
export CATALINA_HOME 
case "$1" in 
start) 
  echo "Starting Tomcat..." 
  $CATALANA_HOME/bin/startup.sh 
  ;; 
stop) 
  echo "Stopping Tomcat..." 
  $CATALANA_HOME/bin/shutdown.sh 
  ;; 
restart) 
  echo "Stopping Tomcat..." 
  $CATALANA_HOME/bin/shutdown.sh 
  sleep 2 
  echo 
  echo "Starting Tomcat..." 
  $CATALANA_HOME/bin/startup.sh 
  ;; 
*) 
  echo "Usage: $prog {start|stop|restart}" 
  ;; 
esac 
exit 0

配置服务自启动

chmod +x /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig --level 345 tomcat on
chkconfig --list

springboot应用

编写/etc/init.d/xxxxx文件 (xxxx为应用名称,以下使用dtmp替代)
vi /etc/init.d/dtmp

#!/bin/bash 
# 
# dtmp startup script
# 
# chkconfig: 345 80 20 
# description: start the dtmp app deamon 
# 
# Source function library 
. /etc/rc.d/init.d/functions 
prog=dtmp 
start_script=/data/dtmp/start-dtmp.sh
stop_script=/data/dtmp/stop-dtmp.sh

case "$1" in 
start) 
  echo "Starting DTMP..." 
  sh ${start_script}
  ;; 
stop) 
  echo "Stopping DTMP..." 
  sh ${stop_script}
  ;; 
restart) 
  echo "Stopping DTMP..." 
  sh ${stop_script}
  sleep 2 
  echo 
  echo "Starting DTMP..." 
  sh ${start_script}
  ;; 
*) 
  echo "Usage: $prog {start|stop|restart}" 
  ;; 
esac 
exit 0

配置服务自启动

chmod +x /etc/init.d/dtmp
chkconfig --add dtmp
chkconfig --level 345 dtmp on
chkconfig --list

springboot启动停止脚本

start-dtmp.sh 和 stop-dtmp.sh,其中dtmp可替换为你自己的应用名称
编写完成之后,请执行以下命令赋权:
chmod 755 start-dtmp.sh stop-dtmp.sh

start-dtmp.sh

#!/bin/sh
cd /data/dtmp
nohup java -jar  -Xms1024m -Xmx20484m dtmp-admin.jar > service.log 2>&1 &

stop-dtmp.sh

#!/bin/sh
ps -ef|grep java|grep dtmp-admin|grep -v grep|cut -c9-14|xargs kill -9
正文到此结束