Linux(RedHat)中service命令和/etc/init.d/的关系

本文转载自http://blog.csdn.net/kkdelta/article/details/7748842


service xxx启动 /etc/init.d/ 目录下的xxx脚本

如一个脚本名为 mysvc保存在/etc/init.d/下可以用 service mysvc 运行mysvc脚本
echo passed in option $0 $1

if [ $1 = "start" ]
then
        echo myservice started........................................
elif [ $1 = "stop" ]
then
        echo myservice stoped........................................
else
        echo myservice not supported option........................................

fi


[root@localhost init.d]# service mysvc start
passed in option /etc/init.d/mysvc start
myservice started........................................

[root@localhost init.d]# service mysvc stop
passed in option /etc/init.d/mysvc stop
myservice stoped........................................

[root@localhost init.d]# service mysvc xxx
passed in option /etc/init.d/mysvc xxx
myservice not supported option........................................

如何增加一个服务在机器启动时启动:
1.服务脚本必须存放在/etc/ini.d/目录下;
2.chkconfig --add servicename
    在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;
3.chkconfig --level 35 servicename on
    修改服务的默认启动等级。

脚本需要有下面的的内容,否则会 service servicename does not support chkconfig

# chkconfig: - 85 15
# description: description for the service

#上面为固定格式:- 表示运行级别(所有),85表示开机执行顺序,15为关机顺序,完整内容如下 

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/bin/bash  
  2. #chkconfig: - 85 15  
  3. # description: mytestservice is a serveice created for testing.   
  4.   
  5. if [ $1 = "start" ]  
  6. then  
  7.         echo myservice started `date` ....................................... >>  
  8.  /home/mytestservice.log  
  9. elif [ $1 = "stop" ]  
  10. then  
  11.         echo myservice stoped  `date` ........................................ >  
  12. > /home/mytestservice.log  
  13. else  
  14.         echo myservice not supported option `date` .............................  
  15. ........... >>/home/mytestservice.log  
  16.   
  17. fi  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值