Linux注册系统服务

目录

 

centos6

centos7


centos6

centos6中通过service管理程序,需要自定义脚本加入/etc/init.d这个目录中。

  • 创建脚本activemq.sh
#! /bin/bash
#
# activemq Bring up/down activemq 
#
# chkconfig: 112 63 37
# description: Activates/Deactivates all activemq interfaces configured to \
#              start at boot time.
# Source function library.
. /etc/init.d/functions

start(){
/usr/local/activemq/bin/activemq start
}

stop(){
/usr/local/activemq/bin/activemq stop
}

case $1 in
start):
	start
;;
stop):
	stop
;;
restart):
	stop
	start
;;
*):
echo "using service activema start|stop|restart"
;;
esac
exit 0
  • 将activemq.sh文件放入/etc/init.d目录中。
  • 给activemq.sh赋予可执行权限:执行命令:chmod +x activemq.sh
  • 一般情况下,根据需求只需要修改start和stop中的启动命令。
chmod +x activemq.sh
  • 将脚本加入服务列表
chkconfig --add activemq
  • 设置为开机自启动
chkconfig activemq on
  • 查看加入是否成功
chkconfig --list activemq

  • 通过service启动activemq
service activemq start

  • 停止activemq
service activemq stop

  • 重启
service activemq restart

centos7

centos7中采用Systemd来管理服务,与centos6有所区别。需要创建脚本放在/usr/lib/systemd/system/目录下。以下以配置activemq为例。

  • 创建脚本
[Unit]
Description=ActiveMQ service
After=network.target

[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq

根据系统实际安装目录修改一下两处

ExecStart=/var/activemq/bin/activemq start
       ExecStop=/var/activemq/bin/activemq stop

  • 设置activemq配置文件/var/activemq/bin/env中的JAVA_HOME
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
  •  通过systemctl管理activemq启停
    * 启动activemq服务: ```systemctl start activemq```
    * 查看服务状态: ```systemctl status activemq```
    * 创建软件链接:``` ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service ```
    * 开机自启: ```systemctl enable activemq```
    * 检测是否开启成功(enable): ```systemctl list-unit-files |grep activemq ```
  • 防火墙配置

添加并重启防火墙
       ```
      firewall-cmd --zone=public --add-port=8161/tcp --permanent
      firewall-cmd --zone=public --add-port=61616/tcp --permanent
     systemctl restart firewalld.service
      ```
   或者直接关闭防火墙: ```systemctl stop firewalld.service```

常用命令:

systemctl enable activemq.service #让某个服务开机启动(.service可以省略)
       systemctl disable activemq.service #不让开机启动
       systemctl status activemq.service #查看服务状态
       systemctl start activemq.service #启动某个服务
       systemctl stop activemq.service #停止某个服务
       systemctl restart activemq.service #重启某个服务
       systemctl is-enabled activemq#查看某个服务是否开机启动

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值