说明:

   1.很多时候我们的服务都是通过源码包编译安装,但是有的源码包编译完成后并不提供该服务的sysv风格脚本,我们只能手动执其二进制程序+配置文件

     2.如果服务器宕机或重启,就不能自动完成启动,所以我们需要自己来编写脚本并把它放到/etc/init.d/目录,并使用chkconfig --add $service 加入开机启动列表

     3.以下脚本是httpd的一个sysv脚本,不过所有的基本sysv风格脚本都一个风格,你只需要把对httpd的判断改成对你编译的程序判断即可

     4.此脚本也可以source /etc/init.d/functions 脚本,functions为我们提供了三个常用的功能,不过以下的这个脚本没有source

    a.输出打印显示为统一格式

           b.daemon 函数  Usage: daemon [+/-nicelevel] {program} 可以向其传递选项 常用的 --user, --pidfile

               例: daemon --user=mysql  /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

    c.killproc 函数 Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]

               例:killproc -p /var/run/mysql /usr/local/mysql/bin/mysqld -HUP  # reload 功能

     5.开启服务程序的reload功能需要,此服务的二进制程序支持接SIGHUP,否则向此服务发出sighup,只能关闭进程,而不能重读配置文件,httpd能够接收hup信号

 #!/bin/bash
#
# HTTPD 2.4: Start up the Atlas server daemon
# chkconfig
# discription: Apache web server for linux
# author:
# QQ:
# mail: login_532_gajun@sina.com install_dir=/usr/local/apache24/instance/httpd80
apachectl=$install_dir/bin/apachectl
binfile=$install_dir/bin/httpd
pidfile=$install_dir/run/httpd.pid
confile=$install_dir/etc/httpd24/httpd.conf
lockfile=$install_dir/lock/httpd
prog=${install_dir##*/} function start()
{
if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
echo -e "\033[32mNotice: $prog is running now\033[0m"
return
else
$binfile -f $confile
if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep";then
touch $lockfile
echo -e "\033[32mOK: $prog is started\033[0m"
return
else
echo -e "\033[31mError: Failed to start $prog\033[0m"
return
fi
fi
} function stop()
{ if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '{print $2}' | xargs kill - > /dev/null >&
if [ $? -eq ];then
rm -f $lockfile
echo -e "\033[32mOK: $prog is stopped\033[0m"
return
else
echo -e "\033[31mError: Failed to stop $prog\033[0m"
return
fi
else
echo -e "\033[31mWarning: $prog is not running\033[0m"
return
fi
} function restart()
{
stop
sleep
start
} function status()
{
if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '{print $2}' | while read httpd_pid;do
echo -e "\033[32mOK: $prog is running ($httpd_pid)\033[0m"
done
return
else
echo -e "\033[31mWarning: $prog is not running\033[0m"
return
fi
} function configtest()
{
$apachectl -t -f $confile
} function reload()
{
status &> /dev/null
if [ $? -eq ];then
if $apachectl -t -f $confile &> /dev/null;then
ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '$1 == "root"{print $2}' | while read httpd_root_pid;do
kill -HUP $httpd_root_pid
done
if [ $? -eq ] && ps -ef | grep "$binfile -f $confile" | grep -q -v "grep";then
echo -e "\033[32mOK: reload $prog is successful\033[0m"
return
else
echo -e "\033[31mError: Failed to reload $porg\033[0m"
return
fi
else
echo -e "\033[31mError: not reloading $prog due to configuration syntax error\033[0m"
return
fi
else
start &> /dev/null
if [ $? -eq ];then
echo -e "\033[32mOK: reload $prog is successful\033[0m"
return
else
echo -e "\033[31mError: Failed to reload $porg\033[0m"
return
fi
fi
} case $ in
start)
start
if [ $? -eq ];then
exit
fi
;; stop)
stop
if [ $? -eq ];then
exit
fi
;; restart)
restart
if [ $? -eq ];then
exit
fi
;; reload)
reload
if [ $? -eq ];then
exit
fi
;; status)
status
;; configtest)
configtest
;; *)
echo -e "\033[31mUsage: `basename $0` {start|stop|restart|reload|status|configtest}\033[0m"
exit
esac

Linux 一个sysv 脚本参考模板的更多相关文章

  1. linux init.d脚本编写模板

    #!/bin/bash ### BEGIN INIT INFO # # Provides: location_server # Required-Start: $local_fs $remote_fs ...

  2. 写一个python脚本监控在linux中的进程

    在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ...

  3. Linux Shell系列教程之(二)第一个Shell脚本

    本文是Linux Shell系列教程的第(二)篇,更多shell教程请看:Linux Shell系列教程 通过上一篇教程的学习,相信大家已经能够对shell建立起一个大体的印象了,接下来,我们通过一个 ...

  4. 『.NET Core CLI工具文档』(十四)dotnet-install 脚本参考

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-install scripts reference 翻译:dotnet-install 脚本参考 名称 d ...

  5. 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器

    本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ...

  6. linux下shell脚本执行jar文件

    最近在搞一个shell脚本启动jar文件个关闭jar文件的东东.搞得我都蛋疼了.今天晚上终于弄好了 话说,小弟的linux只是刚入门,经过各方查资料终于搞定了.话不多说,下面开始上小弟写的shell脚 ...

  7. [转]unity3d 脚本参考-技术文档

    unity3d 脚本参考-技术文档 核心提示:一.脚本概览这是一个关于Unity内部脚本如何工作的简单概览.Unity内部的脚本,是通过附加自定义脚本对象到游戏物体组成的.在脚本对象内部不同志的函数被 ...

  8. linux下shell脚本学习

    在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...

  9. linux的shell脚本入门

    Linux shell脚本入门教程 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活 的工具.Shell不仅仅是命令的收集,而且是一门非常 ...

随机推荐

  1. java util - 在java代码中执行javascript代码工具 rhino-1.7.7.jar

    需要 rhino-1.7.7.jar 包 代码示例: package cn.java.mozilla.javascript; import org.mozilla.javascript.Context ...

  2. 微信小程序navigator的open-type跳转问题

    navigator的open-type属性 可选值 'navigate'.'redirect'.'switchTab',对应于wx.navigateTo.wx.redirectTo.wx.switch ...

  3. PHP操作redis的常用例子

    Redis常用的例子 1,connect 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: &l ...

  4. 24.VUE学习之-变异方法filter与regexp实现评论搜索功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. [USACO]Bovine Genomics

    Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...

  6. TI C6000优化手册——让代码看起来像钉子

    DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...

  7. Diycode开源项目 MainActivity分析

    1.分析MainActivity整体结构 1.1.首先看一下这个界面的整体效果. 1.2.活动源代码如下 /* * Copyright 2017 GcsSloop * * Licensed under ...

  8. C# SQL数据库学习时遇到到一些异常

    1一些关于SqlHelper类的异常: DataAdapter.SqlHelper”的类型初始值设定项引发异常. ExecuteNonQuery.SqlHelper”的类型初始值设定项引发异常. Ex ...

  9. Python及其常用模块库下载及安装

    一.Python下载:https://www.python.org/downloads/ 二.Python模块下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...

  10. IOS开发---菜鸟学习之路--(十九)-利用NSUserDefaults存储数据

    利用NSUserDefaults的可以快速的进行本地数据存储,但是支持的格式有限, 至于支持什么格式大家可以再自行脑补 我这边直接讲如何使用 NSUserDefaults 分为两部分 一个是存数据 N ...