service资源

service资源主要对服务做启动、重启、关闭,监控进程的状态等.


service的参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
service {  'resource title' :
   name       =>  # (namevar) The name of the service to run.  This name is...
   ensure     =>  # Whether a service should be running.  Valid...
   binary     =>  # The path to the daemon.  This is only used for...
   control    =>  # The control variable used to manage services...
   enable      =>  # Whether a service should be enabled to start at...
   flags      =>  # Specify a string of flags to pass to the startup 
   hasrestart =>  # Specify that an init script has a `restart...
   hasstatus  =>  # Declare whether the service's init script has a...
   manifest   =>  # Specify a command to config a service, or a path 
   path       =>  # The search path for finding init scripts....
   pattern    =>  # The pattern to search for in the process table...
   provider   =>  # The specific backend to use for this `service...
   restart    =>  # Specify a *restart* command manually.  If left...
   start      =>  # Specify a *start* command manually.  Most...
   status     =>  # Specify a *status* command manually.  This...
   stop       =>  # Specify a *stop* command...
   # ...plus any applicable metaparameters.
}


binary:指定二进制程序的系统路径,用于那些不支持init的操作系统.如果守护进程没有自启动脚本,可以通过此属性启动服务.


enable:指定服务在开机的时候是否启动,可以设置true和false值.


ensure:是否运行服务,running表示运行,stopped表示停止服务.


hasrestart:指出管理脚本是否支持restart值,如果不支持,就用stop值和start值实现restart效果.可以设置为true和false.


hasstatus:指出管理脚本是否支持status值.puppet用status值来判断服务是否已经在运行,如果系统不支持status值,puppet可以利用查找运行进程列表里面是否有服务名来判断服务是否在运行.可以设置为true和false.


name:守护进程的名字,如果记得不准确可以在/etc/init.d目录下面查找.


path:启动脚本的搜索路径,可以用冒号分割多个路径,或者用数组指定.


pattern:设置匹配进程的字符串,当服务停止时,通过进程列表来判断服务的状态,主要用于不支持init脚本的系统.


restart:指定用于重启服务的脚本,否则只能手动先停止该服务再启动该服务.


start:指定启动服务的命令,通常init模式的管理脚本都支持,不需要手工指定.


status:指定status命令,如果不指定,就从近乎才呢过列表查询该服务.


stop:指定停止服务的脚本.


provider:操作系统支持的init模式的管理脚本,支持base|daemontools|init,默认为init.



示例一:

启动httpd服务,设置开机自启动.


注意:启动服务的前提是httpd软件包已经安装.


1
2
3
4
service { "httpd" :
     ensure => running,
     enable  =>  true ,
}
1
2
[root@sh-web1 ~] # /etc/init.d/httpd status
httpd (pid  121592) is running...
1
2
[root@sh-web1 ~] # chkconfig --list | grep httpd
httpd          0:off1:off2:on3:on4:on5:on6:off


示例二:

之前文章写过怎么将源码包封装成rpm包,下面是封装好的mysql rpm包,测试puppet代码.

1
2
3
4
5
[root@sh-web1 ~] # rpm -ivh mysql-5.6.29-1.x86_64.rpm 
Preparing...                 ########################################### [100%]
    1:mysql                   ########################################### [100%]
` /usr/include/mysql/include ' -> `/data/mysql/include'
[root@sh-web1 ~] # source /etc/profile

安装目录在/data目录下.

1
2
[root@sh-web1 ~] # ls /data/
mysql  mysqldata

启动脚本是/data的脚本cp一份放到/etc/init.d目录下.


下面是puppet启动mysql的代码.

1
2
3
4
5
6
7
8
9
10
11
12
[root@sh-web1 ~] # cat mysqld.pp 
service { "mysqld" :
     ensure => running,
     enable  =>  true ,
     hasrestart =>  true ,
     hasstatus =>  true ,
     provider => init,
     path =>  "/etc/init.d" ,
     restart =>  "/etc/init.d/mysqld reload" ,
     start =>  "/etc/init.d/mysqld start" ,
     stop =>  "/etc/init.d/mysqld stop" ,
}


运行结果:

1
2
3
4
[root@sh-web1 ~] # puppet apply mysqld.pp 
Notice: Compiled catalog  for  sh-web1.localdomain  in  environment production  in  0.06 seconds
Notice:  /Stage [main] /Main/Service [mysqld] /ensure : ensure changed  'stopped'  to  'running'
Notice: Finished catalog run  in  2.44 seconds


查看结果是否启动.

1
2
[root@sh-web1 ~] # /etc/init.d/mysqld status
MySQL running (123039)                                     [  OK  ]

本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1973746