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不仅仅是命令的收集,而且是一门非常 ...
随机推荐
- 如何禁止js缓存?
<html> <head> <script type="text/javascript"> document.write("<s ...
- MYSQL 自定义排序
在mysql order by排序中,大多数情况下仅使用默认排序规则就够了:字符串按字典顺序,数字按大小等等.可有时候,某个字段是有自身业务含义的,比如 type(1,2,3)可能表示早/中/晚,如果 ...
- Python知识点入门笔记——特色数据类型(元组)
元组(tuple)是Python的另一种特色数据类型,元组和列表是相似的,可以存储不同类型的数据,但是元组是不可改变的,创建后就不能做任何修改操作. 创建元组 用逗号隔开的就是元组,但是为了美观和代码 ...
- (转)CocoaPods
本文转自http://nshipster.cn/cocoapods/ 文明是建立在道路,桥梁,运河,下水道,管线,电线和光纤这些基础设施之上的.只要设计和施工得当,它们可以帮助社会成倍的发展. 唯一的 ...
- stm32之PWM博客好文收藏
https://www.cnblogs.com/jiwangbujiu/p/5616376.html STM32F103 使用TIM3产生四路PWM https://www.cnblogs.com/c ...
- Codeforces Round #464 (Div. 2) C. Convenient For Everybody
C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...
- sql优化系列1
sql中索引是否会用到,进而影响查询效率. 带通配符(%)的like语句 1.不能用null作索引,任何包含null值的列都将不会被包含在索引中.即使索引有多列这样的情况下,只要这些列中有一列含有nu ...
- 6 json和ajax传递api数据
1 2 3 4 https://swapi.co/ <h1>Hello Reqwest!</h1> <script> var a = {} reqwest({ ur ...
- 1497: [NOI2006]最大获利(最大权闭合子图)
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5503 Solved: 2673 Description 新的技 ...
- TCP/IP网络编程之优于select的epoll(一)
epoll的理解及应用 select复用方法由来已久,因此,利用该技术后,无论如何优化程序性能也无法同时接入上百个客户端.这种select方式并不适合以web服务端开发为主流的现代开发环境,所以要学习 ...