SHELL脚本testsrv脚本(init脚本或者SysV脚本)

testsrv脚本(SysV脚本)

一、说明:

? CentOS7已经使用Systemd来管理服务,推荐使用Systemd来管理服务。ubuntu18.04现在也是Systemd管理服务。

? init.d是CentOS6时候常用的,不过现在,CentOS6,CentOS7,ubuntu16.04,ubuntu18.04照样可以用。

? 该脚本一般都放在/etc/rc.d/init.d目录下

? 该脚本可以接收start,stop,status,reload,restart等参数来管理服务

? 基本上都会加载/etc/rc.d/init.d/functions,这个文件中有一些比较好用的函数

? #chkconfig和#describe这两行都要写上,describe这个现在貌似可以不用写了。

? #chkconfig:2345 96 07——必须写明,2345代表在这些模式下,开启testsrv功能,96代表开启编号为96 07代表关闭时的编号,相当于创建了一个软连接,这个不要和已有的编号重复,/etc/rc.d/rc5.d/看已有的编号

? 再说明一下,CentOS7现在不使用init0-6来管理用户模式,也用systemd管理

二、题目

编写服务脚本/root/bin/testsrv.sh,完成如下要求

(1) 脚本可接受参数:start, stop, restart, status

(2) 如果参数非此四者之一,提示使用格式后报错退出

(3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功” 考虑:如果事先已经启动过一次,该如何处理?

(4) 如是stop:则删除/var/lock/subsys/SCRIPT_NAME, 并显示“停止完成” 考虑:如果事先已然停止过了,该如何处理?

(5) 如是restart,则先stop, 再start 考虑:如果本来没有start,如何处理?

(6) 如是status, 则如果/var/lock/subsys/SCRIPT_NAME文件存在,则显示“SCRIPT_NAME is running...”,如果/var/lock/subsys/SCRIPT_NAME文件不存在,则显示“SCRIPT_NAME is stopped...”

(7)在所有模式下禁止启动该服务,可用chkconfig 和 service命令管理 说明:SCRIPT_NAME为当前脚本名

三、脚本

[[email protected] init.d]# cat /etc/init.d/testsrv
#!/bin/bash
# chkconfig: 2345 10 90
# description: testsrv
#
. /etc/init.d/functions

check_running(){
    [ -e /var/lock/subsys/`basename $0` ] &&
        STAT=0 ||
        STAT=1
}

do_status(){
    check_running
    if [ "$STAT" = "1" ];then
        action "`basename $0` is stopped..." false
    else
        action "`basename $0` is running..." true
    fi
}

do_start(){
    check_running
    if [ "$STAT" = "0" ];then
        action "`basename $0` 正在运行,取消操作..." true
        exit 0
    elif [ "$STAT" = "1" ];then
        touch /var/lock/subsys/`basename $0`
    fi
    check_running
    if [ "$STAT" = "0" ];then
        action "`basename $0` 启动成功..." true
    elif [ "$STAT" = "1" ];then
        action "`basename $0` 启动失败..." false
        exit 20
    fi
}

do_stop(){
    check_running
    if [ "$STAT" = "0" ];then
        rm -f /var/lock/subsys/`basename $0`
        check_running
        if [ "$STAT" = "1" ];then
            action "`basename $0` 停止成功..." true
        elif  [ "$STAT" = "0" ];then
            action "`basename $0` 停止失败..." false
            exit 10
        fi
    fi
}

do_restart(){
    check_running
    if [ "$STAT" = "0" ];then
        action "`basename $0` 已经运行,正在重新启动..." true
        do_stop
        do_start
    elif [ "$STAT" = "1" ];then
        action "`basename $0` 没有运行,正在启动..." true
        do_start
    fi
}

case "$1" in
    start|stop|status|restart)
        do_$1
        ;;
    *)
        echo "缺少参数: start|stop|status|restart"
        ;;
esac

四、操作

CentOS7操作

[[email protected] init.d]# mv testsrv.sh testsrv
#将testsrv加入SysV服务
[[email protected] init.d]# chkconfig --add testsrv
[[email protected] init.d]# chkconfig --list testsrv 

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
      查看在具体 target 启用的服务请执行
      ‘systemctl list-dependencies [target]‘。

testsrv         0:关 1:关 2:开 3:开 4:开 5:开 6:关
#上面可以看到testsrv加入到了2345模式下开机启动,和配置文件中写的一样
#将该服务在345模式下关闭
[[email protected] init.d]# chkconfig --level 345 testsrv off
[[email protected] init.d]# chkconfig --list testsrv 

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
      查看在具体 target 启用的服务请执行
      ‘systemctl list-dependencies [target]‘。

testsrv         0:关 1:关 2:开 3:关 4:关 5:关 6:关

启动关闭玩玩

五、脚本详解

? 再说一下,CentOS7推荐systemd来管理服务,不建议用SysV管理

? chkconfig 2345 10 90表示服务在2345四个模式下开启testsrv功能,10表示开启编号为96,07表示关闭的编号。

? 这个编号要注意下,不能和其他重复了,init 1是单用户模式,该模式下绝大部分服务都是开机关闭的(K打头),/etc/rc.d/rc1.d/下面自己看看吧,选一个没用的开启编号。init 5是桌面图形化模式,很多服务是启动的,可以看看/etc/rc.d/rc5.d/下面哪些开启编号(S打头)占用了,选择一个关闭的编号

? 如果不想该服务在任何模式下启动,那么把2345变成-

? 同时再说明一下:CentOS7不采用init0-6来管理启动模式,使用Systemd管理

六、chkconfig用法

这个现在也就是CentOS6用了,CentOS7虽然命令还在,但已经不用来管理服务了

查看所有服务的开机启动情况

[[email protected] init.d]# chkconfig --list

查看atd服务的开启启动情况

#2 3 4 5模式下开机启动
[[email protected] init.d]# chkconfig --list atd
atd             0:off   1:off   2:on    3:on    4:on    5:on    6:off

禁止atd服务2 3 4 5 模式下启动

[[email protected] init.d]# chkconfig --list atd
atd             0:off   1:off   2:off   3:off   4:off   5:off   6:off

新写了个testsrv,加入SysV

chkconfig --add testsrv

删除应该是del

原文地址:https://blog.51cto.com/14012942/2433081

时间: 2024-08-11 09:53:28

SHELL脚本testsrv脚本(init脚本或者SysV脚本)的相关文章

Shell 脚本小试牛刀(5) -- 超便捷脚本之快速ssh 登录其他主机

如果你也是以Linux 为工作环境的童鞋,那么此文真是捷报!因为我的学习/工作中(特别是最近玩耍树莓派)经常会使用到ssh 登录其他主机,而每次使用ssh 登录都需要输入老长一大串让我很烦,所以我写了这个脚本来简化我的ssh 过程. 使用这个脚本,如果我想登录一台机器,我只要使用 $~/easy_ssh.sh 然后选择一项就可登录,即使当前没有我要登录的机器选项,我只要输入一次并保存,以后我也可以很快的登录到这台电脑. #!/bin/bash # (C) 2014 Yunlong Zhou <[

《使用shell位置变量进行目录文件的备份小脚本》

今天才发现原来位置变量也可以玩的这么爽!! 这是使用位置变量进行文件目录备份:#!/bin/bashDATE=`date +%F`  //日期以年月日输出tar czf $1.$DATE.tar.gz $1 > /dev/null 2>> /opt/$1.bak.log //打包$1变量包,将错误追加到日志中,tar打包会保留原目录,比较好if [ $? -eq 0 ]   返回值为0代表打包ok,不为0,则不okthen   #包 时间 打包ok 追加到日志        echo &

shell脚本解析7(练习1)-----脚本参数

#!/bin/bash a=$1         #将第一个命令行参数传递给变量a,第二个命令行参数传递给b b=$2 if [ -z $a ] || [ -z $b ]          #判断a 和 b 是否为空,只要有一个为空就打印提示语句并退出. then echo "please enter 2 no" exit 1 fi if [ $a -eq $b ];then               #判断a和b的大小,并根据判断结果打印语句 echo "number a

shell 脚本实战笔记(10)--spark集群脚本片段念念碎

前言: 通过对spark集群脚本的研读, 对一些重要的shell脚本技巧, 做下笔记. *). 取当前脚本的目录 sbin=`dirname "$0"` sbin=`cd "$sbin"; pwd` 代码评注:# 以上代码为获取执行脚本所在的目录的常用技巧# sbin=$(dirname $0) 返回可能是相对路径, 比如./ # sbin=$(cd $sbin; pwd) 采用pwd, 来返回脚本所在目录的绝对路径 *). 循环遍历脚本参数 while (( &q

shell编程概念介绍及变量定义--关于脚本运行与shell进程之间的关系

shell是用户与内核进行交互操作的一种接口,目前最流行的shell称为bash shell shell也是一门编程语言<解释型的编程语言>,即shell脚本<就是用 linux 的 shell 命令编程> 一个系统可以存在多个shell,可以通过 cat/etc/shells 命令查看系统中安装的shell,不同的shell 可能支持的命令语法是不相同的 基本格式 代码写在普通文本文件中,通常以 .sh为后缀名 例vi hello.sh: #!/bin/bash      ##表

20.27分发系统介绍;20.28expect脚本远程登录;20.29expect脚本远程执行命令;20.30expect脚本传递参数

20.27 分发系统介绍 shell项目-分发系统-expect 20.28 expect脚本远程登录 1. 安装expect [[email protected] ~]# yum install -y expect 自动远程登录 2. 创建配置1.expect脚本(远程登录) [[email protected] ~]# vim 1.expect 添加内容(自动远程登录hao2机器并执行命令) #! /usr/bin/expect set host "192.168.211.129"

20.27 分发系统介绍;20.28 expect脚本远程登录;20.29 expect脚本远程执行

20.27 分发系统介绍 shell项目-分发系统-expect 20.28 expect脚本远程登录 1. 安装expect : [[email protected] ~]# yum install -y expect 自动远程登录 2. 创建配置1.expect脚本(远程登录) : [[email protected] ~]# vim 1.expect 添加内容(自动远程登录hao2机器,并执行命令): #! /usr/bin/expect set host "192.168.211.129

20.31 expect脚本同步文件;20.32 expect脚本指定host和要同步的文件;

20.31 expect脚本同步文件:20.32 expect脚本指定host和要同步的文件:20.33 构建文件分发系统:20.34 批量远程执行命令 20.31 expect脚本同步文件 自动同步文件 1. 同步远程机器hao2上/tmp/12.txt文件 到本机/tmp/下 : [[email protected] ~]# vim 4.expect 添加内容: #!/usr/bin/expect set passwd "admin" spawn rsync -av [email 

12_Shell语言———脚本的书写规范以及检查和调试脚本

一.脚本的书写规范 在前文中写过一个脚本first.sh,用来创建一个用户,这里新创建一个脚本useradd.sh,用来创建多个用户,用户名和密码相同: # nano useradd.sh #! /bin/bash # Author: Mickey // 指定作者 # Date:2014-5-14 // 指定日期 # Version:0.0.1 // 指定版本 # Description:Add Users // 描述脚本的功能 # 上述内容为规范的脚本格式 useradd userA echo