通常我们所说的服务,就是一个后台守护程序。下面说一下服务常用操作。
服务基础管理命令
systemctl
/lib/systemd/system下的服务
# 开机启动systemctl enable mysqldsystemctl disable mysqldsystemctl is-active sshd.service# 启动服务systemctl start mysqldsystemctl stop mysqldsystemctl restart mysqldsystemctl status mysqldsystemctl kill mysqld # 结束服务进程(服务无法停止时)# 服务列表systemctl list-unit-files --type=service
init.d
对于 /etc/init.d 下的启动脚本
chkconfig --listchkconfig --add testchkconfig --delete testchkconfig --level 5 httpd on # 开启启动chkconfig --level 5 httpd off # 开机关闭
例如:关闭阿里云盾
service aegis stop #停止服务chkconfig --del aegis # 删除服务
自定义服务
systemd
以activeMq为例
1、新建服务文件
vim /usr/lib/systemd/system/activeMq.service
2、编辑启动、关闭、重启的命令,其中的命令可以是sh文件
[Unit]Description=activemqAfter=network.target remote-fs.target nss-lookup.target[Service]ExecStart=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq startExecReload=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq restartExecStop=/home/zhpt/software/apache-activemq-5.15.2/bin/activemq stop[Install]WantedBy=multi-user.target
init.d
创建日志目录
sudo mkdir -p /data/log
启动脚本
cd /etc/rc.d/init.d && touch autostart && chmod +x autostart
脚本内容
/etc/rc.d/init.d/autostart
#!/bin/bash# chkconfig: 2345 08 92# description: Descriptionexec 2> /data/log/autostart.log # send stderr from rc.local to a log fileexec 1>&2 # send stdout to the same log fileecho "atuostart started" # show start of executionset -x # open log
开机启动
chkconfig --add autostartchkconfig autostart on
