Linux 一个sysv 脚本参考模板
说明:
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 脚本参考模板的更多相关文章
- linux init.d脚本编写模板
		#!/bin/bash ### BEGIN INIT INFO # # Provides: location_server # Required-Start: $local_fs $remote_fs ... 
- 写一个python脚本监控在linux中的进程
		在虚拟机中安装Linux中的CentOS7系统 https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for= ... 
- Linux Shell系列教程之(二)第一个Shell脚本
		本文是Linux Shell系列教程的第(二)篇,更多shell教程请看:Linux Shell系列教程 通过上一篇教程的学习,相信大家已经能够对shell建立起一个大体的印象了,接下来,我们通过一个 ... 
- 『.NET Core CLI工具文档』(十四)dotnet-install 脚本参考
		说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-install scripts reference 翻译:dotnet-install 脚本参考 名称 d ... 
- 详解Linux交互式shell脚本中创建对话框实例教程_linux服务器
		本教程我们通过实现来讲讲Linux交互式shell脚本中创建各种各样对话框,对话框在Linux中可以友好的提示操作者,感兴趣的朋友可以参考学习一下. 当你在终端环境下安装新的软件时,你可以经常看到信息 ... 
- linux下shell脚本执行jar文件
		最近在搞一个shell脚本启动jar文件个关闭jar文件的东东.搞得我都蛋疼了.今天晚上终于弄好了 话说,小弟的linux只是刚入门,经过各方查资料终于搞定了.话不多说,下面开始上小弟写的shell脚 ... 
- [转]unity3d 脚本参考-技术文档
		unity3d 脚本参考-技术文档 核心提示:一.脚本概览这是一个关于Unity内部脚本如何工作的简单概览.Unity内部的脚本,是通过附加自定义脚本对象到游戏物体组成的.在脚本对象内部不同志的函数被 ... 
- linux下shell脚本学习
		在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ... 
- linux的shell脚本入门
		Linux shell脚本入门教程 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活 的工具.Shell不仅仅是命令的收集,而且是一门非常 ... 
随机推荐
- HDU 3333 Turing Tree 莫队算法
			题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ... 
- laravel5.2总结--composer使用和自动加载介绍
			首先看下phpcomposer官方的定义,composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer ... 
- 【网易严选】iOS持续集成打包(Jenkins+fastlane+nginx)
			本文来自网易云社区 作者:孙娇 严选iOS客户端的现有打包方式是通过远程连接打包机执行脚本去打包,打完包会输出相应的ipa的二维码,扫一扫二维码可以安装,但是随着测试队伍的壮大,外包同学越来越多,在打 ... 
- Python+Selenium中级篇之-封装一个自己的类-浏览器引擎类
			前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动那个浏览器.这里我们暂时,支持三 ... 
- Python+Selenium练习篇之20-处理Alert弹窗
			本文来介绍如何通过Selenium方法去处理网页Alert弹窗,和处理iframe类似,都是通过switch_to方法.这里还是没有找到合适的alert弹窗网站,我们就自己创建一个吧,前面文章介绍了如 ... 
- 第1章 HTML基础
			1.1 HTML概述 1.1.1 什么是HTML HTML(Hyper Text Markup Language,超 文本 标记 语言)是纯文本类型的语言,它是Internet上用于编写网页的主要语言 ... 
- c++ primer plus 第6版   部分一    1-4章
			c++ primer plus 第6版 源代码 ---编译器---目标代码---连接程序(启动代码--库代码)---可执行代码 源代码扩展名:c cc cxx C cpp ... 
- 二维数组的动态分配(new)、初始化(memset)和撤销(delete)
			来自http://blog.csdn.net/maverick1990/article/details/22829135 一维数组 动态分配,int *array = new int[10] 初始化, ... 
- Unity开发VR——Oculus Rif_将Oculus接入Unity
			该文档基于 Unity2018.3.12f1 1. 搭建简单场景 2. 设置,选择 Edit - Project Setting(若已经勾选,就去掉在勾选一次) 完成该步骤之后,可以带上Oculus头 ... 
- 声卡(Sound Card)基本概念
			声卡 (Sound Card)是实现声音的模拟/数字信号相互转换.信号处理的一种硬件. 声卡的基本功能是把来自话筒.磁带.光盘的原始声音信号加以转换(模数转换或者数模转换),输出到耳机.扬声器.扩音机 ... 
