Linux服务注册小结

主流的Linux大多使用init.d或systemd来注册服务。下面以centos6.6演示init.d注册服务(centos6 没有使用 systemd,所以没有 systemctl 命令);以centos7.1演示systemd注册服务。注意xxxx.sh一定要记得(chmod +x  test.sh )添加可执行权限

1. 基于Linux的init.d部署

sudo ln -s /etc/systemd/xxxx.sh  /etc/init.d/xxxx

其中 xxxx 就是服务名

service xxxx start       启动服务
service xxxx stop        停止服务
service xxxx status      服务状态
chkconfig xxxx on          开机启动

注:在执行开机启动命令时,如果报服务不支持 chkconfig的解决方法

示例,auto_run的前三行如下:

#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run

第一行,告诉系统使用的shell,所的shell脚本都是这样。

第二行,chkconfig后面有三个参数2345,80和90告诉chkconfig程序,需要在rc2.d~rc5.d目录下,创建名字为 S80auto_run的文件连接,连接到/etc/rc.d/init.d目录下的的auto_run脚本。第一个字符是S,系统在启动的时候,运行脚 本auto_run,就会添加一个start参数,告诉脚本,现在是启动模式。同时在rc0.d和rc6.d目录下,创建名字为K90auto_run的 文件连接,第一个字符为K,系统在关闭的时候,会运行auto_run,添加一个stop,告诉脚本,现在是关闭模式。

注意上面的三行中,二,第三行是必须的,否则在运行chkconfig --add auto_run时,会报错。

2.  基于Linux的systemd部署

在/etc/systemd/system/目录下新建文件test.service,填入下面内容:

[Unit]
Description=test
After=syslog.target
[Service]
ExecStart= /etc/systemd/xxxx.sh  ./xxxx.sh
[Install]
WantedBy=multi-user.target

注意,在实际使用中修改Description和ExecStart后面的内容

systemctl start test 或  systemctl start test.service           启动服务
systemctl stop test 或  systemctl stop test.service             停止服务
systemctl status test 或  systemctl status test.service         服务状态
systemctl enable test 或  systemctl enable test.service         开机启动

注:journalctl 用来查询 systemd-journald 服务收集到的日志。systemd-journald 服务是 systemd init 系统提供的收集系统日志的服务。

journalctl -u test 或  journalctl -u test.service

备注:这都是在实际操作过程中遇到的问题和自己的一个小的总结和积累,借鉴了许多其他人的文章,此处不做详细引用,感谢其他人的辛苦创作。

发布了8 篇原创文章 · 获赞 1 · 访问量 265

猜你喜欢

转载自blog.csdn.net/qq_40635011/article/details/88427057