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不仅仅是命令的收集,而且是一门非常 ...
随机推荐
- CSS3小知识
1.边框圆角,边框阴影 border-radius:6px; // border-radius:50%; //圆形 box-shadow: 1px 1px 1px #666; //box-shadow ...
- 精通Spring Boot---使用@ControllerAdvice处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- Python 格式化
数字前面补0 字符型: print('23'.zfill(5)) 数字型: print('%011d' % 124) 日期与str互转: datetime 转 str str_date = datet ...
- 遗传算法 | Java版GA_TSP (2)
嗯哼,上一篇博客中用Java实现了遗传算法求解TSP(Java版GA_TSP(我的第一个Java程序)),但明显求解效果不太好,都没太好意思贴出具体的结果,今天捣腾了下,对算法做了一些小改进,求解效果 ...
- IT帮2019年2月线下活动【定义工作,解读自我】之站桩练习
2019年2月IT帮线下活动[定义工作,解读自我] 昨天的活动收获很大,全面的总结周老师会另写一篇来帮助大家回顾.我想说一下其中最打动我的一句话:“只有你能决定你有多优秀!” “工作中把自己当成企业家 ...
- “帮你”app-NABCD
1.你的创意解决了用户的什么需求?(N) 本学校已存在的失物招领.表白墙.二手市场等QQ群普遍存在信息冗杂,时效性差等缺点.不能充分发挥信息有效性的,我们的“帮你”APP能够充分发挥信息的有效性,让失 ...
- SQL语句Not IN优化方案
总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查. select * from emp where emp_no not in (select emp_no from emp ...
- MFC深入浅出读书笔记第三部分1
第八章 Document-View 深入探讨(总结) 1.Document Document 在MFC 的CDocument 里头被具体化.CDocument 本身并无任何具体数据,它只是提供一个空壳 ...
- Mac教程macOS教程 苹果电脑教程
第1 章 初识MacOS 01 菜单栏 02 键盘 03 聚焦(Spotlight)
- Java学习2
final在修饰类时,并不限制用户修改对象包含的变量值,只是限制了对象的主转移,只能针对某一个对象进行操作,中途不可更改对象. 重写父类的方法 重写(Override)和重载(Overload)都是针 ...