编写CentOS的System V init启动脚本
系统本身自带了说明,在/usr/share/doc/initscripts-(*)/sysvinitfiles,内容如下:
所有System V init脚本都命名为/etc/rc.d/init.d/<servicename>,其中<servicename>是服务的名称。必须没有“.init”后缀。
示例脚本:
#!/bin/bash
#
# /etc/rc.d/init.d/<servicename>
#
# <description of the *service*>
# <any general comments about this init script>
#
# <tags -- see below for tag definitions. *Every line* from the top
# of the file to the end of the tags section must begin with a #
# character. After the tags section, there should be a blank line.
# This keeps normal comments in the rest of the file from being
# mistaken for tags, should they happen to fit the pattern.> # Source function library.
. /etc/rc.d/init.d/functions <define any local shell functions used by the code that follows> case "$1" in
start)
echo -n "Starting <servicename> services: "
<start daemons, perhaps with the daemon function>
touch /var/lock/subsys/<servicename>
;;
stop)
echo -n "Shutting down <servicename> services: "
<stop daemons, perhaps with the killproc function>
rm -f /var/lock/subsys/<servicename>
;;
status)
<report the status of the daemons in free-form format,
perhaps with the status function>
;;
restart)
<restart the daemons, normally with $0 stop; $0 start>
;;
reload)
<cause the service configuration to be reread, either with
kill -HUP or by restarting the daemons, possibly with
$0 stop; $0 start>
;;
probe)
<optional. If it exists, then it should determine whether
or not the service needs to be restarted or reloaded (or
whatever) in order to activate any changes in the configuration
scripts. It should print out a list of commands to give to
$0; see the description under the probe tag below.>
;;
*)
echo "Usage: <servicename> {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac注意:重启和重载功能可以(通常)组合成一个测试,vis:
restart|reload)
不禁止您添加其他命令; 列出您打算以交互方式使用到使用消息的所有命令。
/etc/rc.d/init.d/functions函数
daemon [+/-nicelevel] program [arguments] [&]
如果守护程序尚未运行,则启动该守护程序。还有其他一些有用的东西,例如,如果守护进程意外终止,则保留守护进程。
killproc program [signal]
向程序发送信号; 默认情况下,它发送一个SIGTERM,如果进程没有死,它会在几秒钟后发送一个SIGKILL。
如果找到pid文件,它还会尝试删除它。
pidofproc program
试图找到一个程序的pid; 检查可能的pidfiles,使用pidof程序,甚至使用ps。主要用于此文件中的其他函数,但也可用于脚本。
status program
打印状态信息。假设程序名称与servicename相同。
Tags.
# chkconfig: <startlevellist> <startpriority> <endpriority>
必须。<startlevellist>是默认情况下应启动服务的级别列表。<startpriority>和<endpriority>是优先级编号。例如:
# chkconfig:2345 20 80有关详细信息,请阅读“man chkconfig”。
除非有一个非常好的,显性相反的原因,<endpriority>应该等于 100 - <startpriority>
# description: <multi-line description of service>
必须。几行描述,继续使用'\'字符。以下行中的初始注释和后续空格将被忽略。
# description[ln]: <multi-line description of service in the language \ # ln, whatever that is>
可选。应将描述翻译成指定的语言。
# processname:
可选,允许多个条目。对于脚本启动的每个进程名称,应该有一个进程名称条目。例如,samba服务启动两个守护进程:
#processname:smdb
#processname:nmdb# config:
可选,允许多个条目。对于守护程序使用的每个静态配置文件,请使用单个条目。例如:
# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf(可选)如果服务器将自动重新加载配置文件(如果已更改),则可以在行中附加“autoreload”一词:
# config: /etc/foobar.conf autoreload
#pidfile:
可选,允许多个条目。使用就像配置条目一样,除了它指向pidfiles。假设pidfiles仅在进程创建时更新,而不是更晚。该文件的第一行应该是PID的ASCII表示; 终止换行符是可选的。不检查除第一行以外的任何行。
#project: true
可选,使用IN PLACE的processname,config和pidfile。如果存在,则可以通过运行以下命令来实现正确的重新加载 - 如果必要的循环:
command = $(/ etc / rd.d / init.d / SCRIPT probe)
[ - n“$ command”] && /etc/rc.d/init.d/SCRIPT $ command其中SCRIPT是服务的sysv init脚本的名称。
作为示例,需要执行复杂处理的脚本可以返回“run /var/tmp/<servicename.probe.$$”并实现“run”命令,该命令将执行命名脚本然后将其删除。
请注意,如果不需要执行任何操作使服务与其配置文件同步,则probe命令应该只是“exit 0”。
需要注意以下几点:
1、# chkconfig和# description不能少,必须写。
2、chkconfig的<startpriority> <endpriority>为启动优先级,在man中查询不到,一般end...不用理解,直接100-start...即可。start为开始的顺序,一般系统从小执行到大,数值任意,这个对于依赖启动有很大的帮助,比如控制先启动某个服务,再启动某个服务。以下是查询设置后的命令:
# 查询启动级别
chkconfig --list <servicename>
# 查询启动顺序
grep chkconfig /etc/rc.d/init.d/<servicenaem>
参考:
https://blog.hazrulnizam.com/create-init-script-centos-6/
https://serverfault.com/questions/176055/how-to-change-linux-services-startup-boot-order
http://www.sensi.org/~alec/unix/redhat/sysvinit.html
编写CentOS的System V init启动脚本的更多相关文章
- daemon 启动system V init 和 systemd 配置
先试着写一个udpserver的daemon #include <stdio.h> #include <sys/socket.h> #include <sys/types ...
- Systemd初始化进程/RHEL 6系统中System V init命令与RHEL 7系统中systemctl命令的对比
Linux操作系统的开机过程是这样的,即从BIOS开始,然后进入Boot Loader,再加载系统内核,然后内核进行初始化,最后启动初始化进程.初始化进程作为Linux系统的第一个进程,它需要完成Li ...
- linux系统 initrd.img中init启动脚本分析
概述:这篇文章主体内容来源于网上转载.前面几篇文章倾向于制作initrd.img,这篇文章更倾向于initrd.img的运行过程:加载framebuff驱动 ide驱动和文件系统驱动,最后进入到真正的 ...
- 系统INIT 启动脚本的结构/etc/rc.d/init.d/*
- systemd、upstart和system V
http://blog.csdn.net/kumu_linux/article/details/7653802 systemd是Linux下的一种init软件,由Lennart Poettering ...
- Linux system V
Sysvinit 的小结 Sysvinit 的优点是概念简单.Service 开发人员只需要编写启动和停止脚本,概念非常清楚:将 service 添加/删除到某个 runlevel 时,只需要执行一些 ...
- (转)Ubuntu init启动流程分析
原文 upstart homepage 现行的Linux distros主流的有两种init方式:一种是广为流传的System V initialization,它来源于Unix并且至今仍被各种Lin ...
- ubuntu init启动流程
ubuntu的init方式有两种:一种是System V initialization,一种是Upstart.ubuntu6.10以前的版本是第一种方式,之后的版本是第二种方式. 在旧式的System ...
- System v和posix的IPC对比
之前有一篇关于共享内存的System V和Posix的对比: http://www.cnblogs.com/charlesblc/p/6261469.html POSIX(Portable Opera ...
随机推荐
- MediatR
1.MediatR是什么? 微软官方eshopOnContainer开源项目中使用到了该工具, mediatR 是一种中介工具,解耦了消息处理器和消息之间耦合的类库,支持跨平台 .net Standa ...
- asp.net core 2.0 webapi集成signalr
asp.net core 2.0 webapi集成signalr 在博客园也很多年了,一直未曾分享过什么东西,也没有写过博客,但自己也是汲取着博客园的知识成长的: 这两天想着不能这么无私,最近.N ...
- python全栈开发day77-博客主页
1.文章分类 2.标签 3.归档 1) MySQL的日期格式化函数 DATE_FORMAT(字段名,格式) 2) Django ORM中如何执行SQL原生语句 (1) models.Article.o ...
- Python 经典类和新式类
#!/usr/bin/env python# -*- coding:utf-8 -*-# 作者:Presley# 邮箱:1209989516@qq.com# 时间:2018-10-21# 新式类和经典 ...
- Count the string kmp
问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个 ...
- 003 python中的内置函数
一:如何查看内置函数 1.命令 dir(__builtins__) 2.效果 二:具体的用法 1.input 简单使用: 2.type 返回变量的类型 3.str 将类型转变为字符串 4.isinst ...
- day15 函数的使用方法:递归函数
这里归纳的知识点主要就是: 浮点数,复数, int,整形:float,浮点数:complx,复数: # 实数: # 有理数 有限小数 1.2 # 无限循环小数 1.23232323232323.... ...
- scrapy 日志一般配置
- linux的文件打包与压缩
简介 Linux 上常用的压缩/解压工具,介绍了zip.rar.tar的使用. 文件打包和压缩 Linux 上的压缩包文件格式,除了 Windows 最常见的*.zip.*.rar..7z 后缀的压缩 ...
- AOP编程-理论篇
本节内容主要讲解AOP编程理念,包括概念讲解,使用AOP的优势,如何实现AOP,常见的实现AOP的方法. 1.AOP的概念 AOP是Aspect Oriented Programming的缩写,意思是 ...