手动添加linux自启服务

1. 编写脚本,修改执行权限
vim myservice.sh, chmod +x myservice.sh
#!/bin/bash
case "$1" in
start)
echo "Starting myservice...";;
stop)
echo "Shutting down myservice...";;
restart)
echo "Shutting down myservice..."
echo "Starting myservice...";;
*)
echo "Usage: #0 {start|stop|restart}";;
esac

2. 将脚本(去掉.sh后缀)放到/etc/init.d/目录下
cp /tmp/myservice.sh /etc/init.d/myservice

3. 运行/etc/init.d/myservice服务
/etc/init.d/myservice start | stop | restart

4. 使用chkconfig --add添加到启动的服务,发现没有myservice
chkconfig --add myservice
系统提示:service myservice does not support chkconfig

5. 查看帮助,man chkconfig, /example
得到服务的脚本格式:
# chkconfig: 启动level 启动顺序SXX 关闭顺序KXX
# description: 描述

vim /etc/init.d/myservice, 添加代码:
#chkconfig: 35 24 25
#description: this is a test

6.添加系统开机自动启动服务
chkconfig --add myservice
chkconfig --list myservice
myservice       0:off 1:off 2:off 3:on 4:off 5:on 6:off
myservice服务在启动level为3或5时,开机启动

7. 查看添加的服务
ls /etc/rc.d/rc*.d/*myservice,可以看到:
/etc/rc.d/rc0.d/K25myservice  /etc/rc.d/rc4.d/K25myservice
/etc/rc.d/rc1.d/K25myservice  /etc/rc.d/rc5.d/S24myservice
/etc/rc.d/rc2.d/K25myservice  /etc/rc.d/rc6.d/K25myservice
/etc/rc.d/rc3.d/S24myservice

About the Author

Avatar photo

今生在线

Comments

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据