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不仅仅是命令的收集,而且是一门非常 ...
随机推荐
- 利用PHPExcel 实现excel数据的导入导出(源码实现)
利用PHPExcel 实现excel数据的导入导出(源码实现) 在开发过程中,经常会遇到导入导出的需求,利用phpexcel类实现起来也是比较容易的,下面,我们一步一步实现 提前将phpexcel类下 ...
- ThinkPHP邮件发送S(Smtp + Mail + phpmailer)
三种邮件发送介绍:(Smtp,Mail以及phpmailer)ThinkPhp 框架下开发. 邮件发送配置先前准备(用该账号做测试用):(这里用新浪邮箱服务器)将自己的新浪邮箱开通 POP3/SMTP ...
- Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码
题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...
- shell脚本中的交互式输入自动化
shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了. 1 利用重定向 重定向的方法应该是最简单的 例: 以下的te ...
- P3387 【模板】缩点
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- 6、python中的元组
元组(tuple)是python中有序.不可变的数据结构.元组还是python四种数据结构中唯一一种不可变的数据结构. 一.前言 元组在很多方面都变现得跟列表一样,除了列表储存得对象是可变得,而元组储 ...
- CyclicBarrier源码分析
CyclicBarrier是通过ReentrantLock(独占锁)和Condition来实现的.下面,我们分析CyclicBarrier中3个核心函数: 构造函数, await()作出分析. 1. ...
- 产生指定时间区间序列、按指定单位变化时间 python实现
示例1:给定起始日期和结束日期,如何得到中间的时间序列 import datetime def dateRange(beginDate, endDate): dates = [] dt = datet ...
- ogre3D学习基础1 -- 核心对象与脚本技术
一.核心对象介绍1.命名空间 Ogre3d使用了C++的特性--命名空间,可以防止命名混淆.使用方法也简单,using namespace Ogre;或者直接在使用时加上“Ogre::”的前缀,如Og ...
- day02_01.能被3整除的数
第1题 能被3整除的数 编程思想的初步形成 把人的正常思维放大化,用放大镜去放大你的每个思考过程 你会发现,原来编程没有你想象的那么难 题目:输出100以内(不含100)能被3整除的所有整数 < ...