Linux 自定义service启动

    大多数Linux开机自启动,都是把命令写到/etc/rc.d/rc.local或者 /etc/rc.local里,这样虽然能够实现随机运行,但是并不够灵活。不能像mysql,apache等服务一样能够使用service命令或者调 用init.d下的脚本启动、关闭或者重启进程。例如,

service mysql restart
service apache2 stop

或者

/etc/init.d/mysql restart
/etc/init.d/apache2 stop

编写一个启动控制脚本,以hxht为例,建立/etc/init.d/hxht文本文件,输入下面的内容:

复制代码
#!/bin/sh

case "$1" in
start)
start-stop-daemon --start --background --exec /home/fx/hxht.py
;;
stop)
start-stop-daemon --stop --name hxht.py
;;
restart)
start-stop-daemon --stop hxht.py
start-stop-daemon --start --background --exec /home/fx/hxht.py
;;
*)
echo "please use args: start stop restart!"
;;
esac
复制代码


这是一个简单的shell脚本,case .. in是用来根据调用参数进行不同的操作,start-stop-daemon是一个可以管理daemon进程的程序,要查看它的详细说明,可以运行man start-stop-daemon。start的时候,使用--exec指定要执行的文件,stop的时候,使用--name根据进程名字来使用 killall结束匹配的进程。

接着,设置脚本文件属性,设置可执行标记。

root@localhost:~# chmod 755 /etc/init.d/hxht

这样子,就可以使用service命令来启动和关闭进程了,例如启动进程如下:

root@localhost:~# service hxht start
root@localhost:~# ps aux|grep hxht
root       353  1.4  1.9   8644  5212 ?        S    09:50   0:00 /usr/bin/python /root/hxht.py
root       355  0.0  0.2   1900   596 pts/0    S+   09:50   0:00 grep --color=auto hxht
关闭进程,

root@localhost:~# service hxht stop
root@localhost:~# ps aux |grep hxht
root       365  0.0  0.2   1900   592 pts/0    S+   09:51   0:00 grep --color=auto hxht
到这里,一个Linux服务的进程控制脚本已经写好了,但是要实现随机启动,还需要一个步骤。
Linux开机的时候,不是直接运行/etc/init.d下的所有脚本的,而是根据不同的runlevel来执行/etc/rc$runlevel.d 下的脚本。这里的runlevel是用以区别系统的运行方式(例如单用户的runlevel,多媒体桌面的runlevel,服务器的runlevel都 不同)。

在Ubuntu里,可以使用update-rc.d来把/etc/init.d/hxht安装到各个runlevel中。更多关于update-rc.d的说明,请参见man update-rc.d。

root@localhost:~# update-rc.d hxht defaults 99
update-rc.d: warning: /etc/init.d/hxht missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/hxht ...
   /etc/rc0.d/K99hxht -> ../init.d/hxht
   /etc/rc1.d/K99hxht -> ../init.d/hxht
   /etc/rc6.d/K99hxht -> ../init.d/hxht
   /etc/rc2.d/S99hxht -> ../init.d/hxht
   /etc/rc3.d/S99hxht -> ../init.d/hxht
   /etc/rc4.d/S99hxht -> ../init.d/hxht
   /etc/rc5.d/S99hxht -> ../init.d/hxht
update-rc.d后面有三个参数,分别是/etc/init.d下的脚本名字,默认安装方式,运行的优先级。优先级的数字越大,表示越迟运行,这里我们把自己写的服务放在最后运行。

如果要卸载随机启动的服务,执行

update-rc.d -f hxht remove

在update-rc.d安装的时候提示了警告信息,是因为我们写的/etc/init.d/hxht太简陋了,连LSB的信息也没有提供。

update-rc.d: warning: /etc/init.d/hxht missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>

只需要做一些小改动,就可以避免那个警告了。如下:

#!/bin/sh
### BEGIN INIT INFO
# Provides: hxht
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop the hxht service
### END INIT INFO

case "$1" in
start)
start-stop-daemon --start --background --exec /home/fx/hxht.py
;;
stop)
start-stop-daemon --stop --name hxht.py
;;
restart)
start-stop-daemon --stop hxht.py
start-stop-daemon --start --background --exec /home/fx/hxht.py
;;
*)
echo "please use args: start stop restart!"
;;
esac

到此,一个最简单的随机启动服务写好了,看起来文章挺长的,但其实也就几个命令而已。
在下次开机启动的时候,hxht.py就会以root用户身份被自动运行。



现在,你应该知道怎么编写属于自己的service命令了吧,编写一个脚本,然后把它放在/etc/init.d这个目录底下,你就可以用service +脚本名字 运行它。如果是要开机自动启动那就得用chkconfig命令了。

注意:

A、service这个命令往往是即时生效,不用开关机,但是重启后服务会回到默认状态。

B、chkconfig是用于把服务加到开机自动启动列表里,只要启动它,就能自动启动,重启后永久生效

即:chkconfig --add COMMAND

    chkconfig COMMAND on/off    重启后永久生效

猜你喜欢

转载自zhouyangang2008201410260331.iteye.com/blog/2155308