linux 添加 service 服务并自动添加 chkconfig 启动级别

下面以添加一个叫做watchcat的服务为例进行说明:

1、写一个提供给service命令使用的脚本

service 命令的使用方法一般如下

启动:

$ service watchcat start

停止:

$ service watchcat stop

重启:

$ service watchcat restart

所以这个watchcat脚本就需要至少接受这三个命令

大概如下:$ cat watchcat

#!/bin/bash

function start_cat()
{
    echo "cat run ..."
}

function stop_cat()
{
    echo "cat stop ..."
}

case "$1" in
    start)
    start_cat
    ;;  

    stop)
    stop_cat
    ;;  

    restart)
    stop_cat
    start_cat
    ;;
    *)
    echo "Usage : start | stop | restart"
    ;;
esac

注:这个脚本不是最终脚本,还不能作为服务使用

2、还需要让这个脚本被 chkconfig 支持

其实这个很重要,那就是在刚刚的脚本上面添加几行注释,这几行只是是用来给chkconfig使用的。也就是说,当chkconfig 看到这几行注释的时候,它是不会把这几行当做注释的。

完整的脚本如下:

 1 #!/bin/bash
 2 #
 3 # watchcat    start/stop cats
 4 #
 5 # chkconfig: 12345 80 90
 6 # description: start/stop cats
 7 #
 8 ### BEGIN INIT INFO
 9 # Description: start/stop cats
10 ### END INIT INFO
11
12 function start_cat()
13 {
14     echo "cat run ..."
15 }
16
17 function stop_cat()
18 {
19     echo "cat stop ..."
20 }
21
22 case "$1" in
23     start)
24     start_cat
25     ;;
26
27     stop)
28     stop_cat
29     ;;
30
31     restart)
32     stop_cat
33     start_cat
34     ;;
35     *)
36     echo "Usage : start | stop | restart"
37     ;;
38 esac

现在这个脚本算是可以正式使用了。

上面代码的第5行是必须的,这是给chkconfig看的。

第5 行的12345是表示这个watchcat 程序是需要在系统的运行级别为1、2、3、4、5的时候都进行启动的。80和90 的意思是在rc1.d/~rc5.d/目录下建立S80xxxxx和K90xxxxx的链接文件的。

3、配置watchcat的service支持

首先给watchcat添加可执行权限:

$ chmod 775 watchcat

把脚本添加复制到/etc/init.d/目录下

$ cp watchcat /etc/init.d/
cp: cannot create regular file `/etc/init.d/watchcat‘: Permission denied
$ sudo cp watchcat /etc/init.d/

此时就可以使用service进行控制了,如下:

$ service watchcat start
cat run ...
$ service watchcat stop
cat stop ...

4、然并卵,这只是完成了手动控制的阶段,还需要开机启动

先查看一下$ chkconfig --list watchcat
service watchcat supports chkconfig, but is not referenced in any runlevel (run ‘chkconfig --add watchcat‘)收我们没有添加这个叫watchcat的服务
$ chkconfig --add watchcat
You do not have enough privileges to perform this operation.
$ sudo chkconfig --add watchcat检查添加后的结果
$ chkconfig --list watchcat
watchcat           0:off    1:on    2:on    3:on    4:on    5:on    6:off

这样就完成了。

引用文献:

http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html

http://www.linuxidc.com/Linux/2015-01/111438.htm

时间: 2024-07-30 20:23:18

linux 添加 service 服务并自动添加 chkconfig 启动级别的相关文章

php-fpm添加service服务

原文:http://blog.csdn.net/whatday/article/details/50659992 php-fpm在5.3.2版本以后不支持service php-fpm start等命令,如果想要支持这些命令可以做如下操作 我使用源码安装的php5.6.30 修改安装目录下的/www/php/etc/php-fpm.conf文件,把;pid = run/php-fpm.pid之前的';'去掉 把源码包目录下的init.d.php-fpm 拷贝到/etc/init.d/php-fp

在centos中添加freeswitch服务开机自动运行

新建 /etc/init.d/freeswitch 脚本,脚本内容如下: #! /bin/sh # # freeswitch: Starts the freeswitch Daemon # # chkconfig: 345 96 02 # processname: freeswitch # description: Freeswitch fedora init script # config: # Author: gled # Source function library. . /etc/in

Linux架设DDNS服务器之自动更新脚本

问题描述:客户端是动态IP,每次连网之后要nsupdate下才可以把客户端的hostname 与IP映射更新到DNS Server上 命令如下: nsupdate -k K*****.key >server 192.*.*.* #dns server ip address >update delete yourfqdn A delete >update add yourFQDN 600 A your new IP 这样的效率实在是有点低,于是我就自己写个脚本来更新了.直接贴上我的脚本.脚

添加service到SystemService硬件服务

添加service到SystemService: 添加硬件服务. 创建时间:2015年3月9日(星期一) 晚上11:07 | 分类:硬件驱动Android | 天气: 修改时间:2015年3月10日(星期二) 中午11:37 <note_content> .添加service到SystemService 测试的service类是InnerToolService,表示内置到framework的自定义服务,使用的aidl包括: IInnerToolService 定义接口,InnerToolSer

Provisioning Services 7.8 入门系列教程之八 自动添加设备

续Provisioning Services 7.8 入门系列教程之七 批量导入设备 对于批量导入设备,其最大的缺点是在导入前,必须准确记录所有设备的MAC地址.当然,这种方式也有许多方便之处,如可以将不同的设备添加到不同地站点.不同的集合. 下面介绍第三种方式:通过自动添加向导完成设备的添加 使用自动添加向导 自动添加向导可以自动配置各种规则,以便利用自动添加功能将新的目标设备自动添加到 Provisioning Services 数据库中. 可以在场.站点.集合或设备级别启动自动添加向导.

PowerDesigner 如何添加每个表中共用的字段及自动添加注释

PowerDesigner 如何添加每个表中共用的字段: 有时候在创建表的时候会有一些共用的字段,但是每一张表都要去创建,这样做很麻烦,特别是这样重复的工作,稍不留意就会出现问题,实际上在PD中有这样一种方法帮我们省去这样繁琐重复的工作. 1>.选中一张表,点击属性,打开Columns. 2>.点击Add Columns(Ctrl+Add),然后选择共用的字段,点击确定,OK,烦恼结束.(Point:这样做还是新增了共用的字段,并不是和其它的表进行关联),如图: 我在建模的时候,希望在生成脚本

Linux下chkconfig命令详解即添加服务以及两种方式启动关闭系统服务

chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 一.chkconfig 的使用语法 1.chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <levels等级代号>][系统服务][on/off/reset] 2.参数用法: --add 增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文

chkconfig添加服务错误service memcached does not support chkconfig

chkconfig添加服务错误,提示XXX服务不支持 故障原文: [[email protected] ~]# chkconfig --add memcached service memcached does not support chkconfig 故障说明: 无法添加提示该错误是在服务的启动脚本里面没添加chkconfig的级别 解决方案: 我们一般在脚本开头加入下面两句就好了 #gedit /etc/init.d/memecached 添加下面两句到 #!/bin/bash 之后. #

问题:Custom tool error: Failed to generate code for the service reference &#39;AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法

添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运行事先定义好的Unit Test,确保在客户端使用Service之前Service是可以正确的运行的.在我创建Unit Test之前,需要在测试项目中添加对WCF Service的引用,而这时却出现了错误. Custom tool error: Failed to generate code for