centos7制作开机自启动服务

Posted mingyuewu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7制作开机自启动服务相关的知识,希望对你有一定的参考价值。

centos7 制作开机自启动服务

文章目录

自启动服务脚本路径/usr/lib/systemd/system/zookeeper.service
查看所有的开机自启动服务systemctl list-units --type=service

一、 zookeeper

vim /usr/lib/systemd/system/zookeeper.service

1 编写自启动脚本

cat >/usr/lib/systemd/system/zookeeper.service<<EOF
[Unit]
Description=Zookeeper
After=network.target
[Service]
Type=forking
Environment=ZOO_LOG_DIR=/usr/local/zookeeper/logs
Environment=PATH=/usr/local/jdk1.8.0_231/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PIDFile=/usr/local/zookeeper/data/zookeeper/zookeeper_server.pid
ExecStart=/usr/local/zookeeper/bin/zkServer.sh start
ExecStop=/usr/local/zookeeper/bin/zkServer.sh stop
ExecRestart=/usr/local/zookeeper/bin/zkServer.sh restart
Restart=on-failure
User=zookeeper
Group=zookeeper
[Install]
WantedBy=multi-user.target
EOF

2、配置zookeeper系统服务及自启动

systemctl daemon-reload
systemctl enable zookeeper.service

3、启动与停止zookeeper服务

systemctl start zookeeper
ps -ef|grep zookeeper
systemctl stop zookeeper

二、nginx

1 编写服务脚本

vim /usr/lib/systemd/system/nginx.service 
[Unit]
Description=Nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Restart=on-failure
User=nginx
Group=nginx
[Install]
WantedBy=multi-user.target

配置nginx的负载均衡时,upstream 中的server 要配置内网IP

curl -v URL curl 是客户端(client)URL的简写,-v参数输出整个通信过程

三、redis

vim  /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /usr/local/redis/logs/redis.pid`
Restart=on-failure
User=redis
Group=redis

[Install]
WantedBy=multi-user.target

四、Tomcat

vim  /usr/lib/systemd/system/tomcat.service
[root@node01 system]# cat tomcat.service 
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFILE=/usr/local/tomcat/logs/tomcat.pid
ExecStart=/usr/local/tomcat/bin/catalina.sh start  
ExecStop=/usr/local/tomcat/bin/catalina.sh stop
ExecReload=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target


使用systemctl status tomcat启动tomcat时,

报错Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

虽然配置了JDK的环境变量,但tomcat无法识别,需要手动在/usr/local/tomcat/bin/catalina.sh脚本开头,添加java_home和jre_home

######jdk  jre path
export  JAVA_HOME=/usr/local/jdk1.8.0_231
export  JRE_HOME=/usr/local/jdk1.8.0_231/jre

在catalina.sh脚本中写pid文件路径

五、格式说明

[Unit]  服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
Type=forking是后台运行的形式
PIDFILE=pid文件路径
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
User
Group   运行该服务的用户和组
注意:启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户

六、开机自启服务举例

systemctl list-units --type=service
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  crond.service                      loaded active running Command Scheduler
  docker.service                     loaded active running Docker Application Container Engine
  mysqld.service                     loaded active running MySQL Server
● nginx.service                      loaded failed failed  SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
  node_exporter.service              loaded active running node_exporter
  prometheus.service                 loaded active running Prometheus
  redis.service                      loaded active running redis-server
  rsyslog.service                    loaded active running System Logging Service  serial-getty@ttyS0.service         loaded active running Serial Getty on ttyS0
  sshd.service                       loaded active running OpenSSH server daemon
  sysstat.service                    loaded active exited  Resets System Activity Logs
  tomcat.service                     
  zookeeper.service                  

以上是关于centos7制作开机自启动服务的主要内容,如果未能解决你的问题,请参考以下文章

Linux 之centos7 制作服务自启动systemd

centos7中设置服务开机自启的两种方法

centos怎么设置开机自启动ssh服务

centos怎样将服务加入开机自动启动

Centos7下添加开机自启动服务和脚本

centos7 哪些自启动服务可以关闭