linux如何添加服务为系统服务快速启动或关闭
Posted 别安驹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux如何添加服务为系统服务快速启动或关闭相关的知识,希望对你有一定的参考价值。
当在linux系统安装了一些服务比如apache,mysql,iptables等等后想快速启动或者重启
但是在使用系统启动或者关闭服务时候发现输入指令
>service httpd restart
是不起作用的这个时候就要用到添加当前httpd服务为系统服务,如何添加为系统服务
cd /etc/init.d/
ls
列出当前系统服务
比如cp network httpd
复制当前的网络服务脚本更名为httpd
vi /etc/init.d/httpd
复制下面代码保存
case "$1" in
start)
echo $"bieanju notice: Apache now is Starting ……"
/bin/systemctl start httpd.service
echo -e $"bieanju notice:Start is \033[32m[OK]\033[0m";;
stop)
echo $"bieanju notice:iptabels now is Stop"
/bin/systemctl stop httpd.service
echo -e $"bieanju notice:Stop is OK";;
restart)
echo $"bieanju notice:iptabels now is Restarting"
/bin/systemctl restart httpd.service
echo $"bieanju notice:restart is OK";;
esac
exit
start)
echo $"bieanju notice: Apache now is Starting ……"
/bin/systemctl start httpd.service
echo -e $"bieanju notice:Start is \033[32m[OK]\033[0m";;
stop)
echo $"bieanju notice:iptabels now is Stop"
/bin/systemctl stop httpd.service
echo -e $"bieanju notice:Stop is OK";;
restart)
echo $"bieanju notice:iptabels now is Restarting"
/bin/systemctl restart httpd.service
echo $"bieanju notice:restart is OK";;
esac
exit
shell脚本代码解释记录
通过判断输入的指令start,stop,restart执行对应的操作并输出提示语echo -e 可以输出内容带颜色显示以上设置为字体绿色
#小笔记echo 使用参数
字颜色:30—–37
echo -e “\033[30m 黑色字 \033[0m” echo -e “\033[31m 红色字 \033[0m” echo -e “\033[32m 绿色字 \033[0m” echo -e “\033[33m 黄色字 \033[0m” echo -e “\033[34m 蓝色字 \033[0m” echo -e “\033[35m 紫色字 \033[0m” echo -e “\033[36m 天蓝字 \033[0m” echo -e “\033[37m 白色字 \033[0m”
字背景颜色范围:40—–47
echo -e “\033[40;37m 黑底白字 \033[0m” echo -e “\033[41;37m 红底白字 \033[0m” echo -e “\033[42;37m 绿底白字 \033[0m” echo -e “\033[43;37m 黄底白字 \033[0m” echo -e “\033[44;37m 蓝底白字 \033[0m” echo -e “\033[45;37m 紫底白字 \033[0m” echo -e “\033[46;37m 天蓝底白字 \033[0m” echo -e “\033[47;30m 白底黑字 \033[0m”
\33[0m 关闭所有属性 \33[1m 设置高亮度 \33[4m 下划线 \33[5m 闪烁 \33[7m 反显 \33[8m 消隐 \33[30m — \33[37m 设置前景色 \33[40m — \33[47m 设置背景色 \33[nA 光标上移n行 \33[nB 光标下移n行 \33[nC 光标右移n行 \33[nD 光标左移n行 \33[y;xH设置光标位置 \33[2J 清屏 \33[K 清除从光标到行尾的内容 \33[s 保存光标位置 \33[u 恢复光标位置 \33[?25l 隐藏光标 \33[?25h 显示光标
到这一步就大功告成了
service httpd restart
最后基本学会了那我们就做一个lamp环境的重启,关闭系统服务指令
case "$1" in
start)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Starting ……"
/bin/systemctl start httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Starting ……"
/bin/systemctl start mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m is \033[32m[OK]\033[0m";;
stop)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Stopping……"
/bin/systemctl stop httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m Stop is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Stopping……"
/bin/systemctl stop mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m Stop is \033[32m[OK]\033[0m";;
restart)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Restarting……"
/bin/systemctl restart httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m Restartting is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Restarting……"
/bin/systemctl restart mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m Restartting is \033[32m[OK]\033[0m";;
esac
exit
start)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Starting ……"
/bin/systemctl start httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Starting ……"
/bin/systemctl start mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m is \033[32m[OK]\033[0m";;
stop)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Stopping……"
/bin/systemctl stop httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m Stop is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Stopping……"
/bin/systemctl stop mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m Stop is \033[32m[OK]\033[0m";;
restart)
echo -e $"bieanju notice: \033[32mApache\033[0m now is Restarting……"
/bin/systemctl restart httpd.service
echo -e $"bieanju notice: \033[32mApache\033[0m Restartting is \033[32m[OK]\033[0m"
echo -e $"bieanju notice: \033[32mMysql\033[0m now is Restarting……"
/bin/systemctl restart mysqld.service
echo -e $"bieanju notice: \033[32mMysql\033[0m Restartting is \033[32m[OK]\033[0m";;
esac
exit
o阿什顿发k
以上是关于linux如何添加服务为系统服务快速启动或关闭的主要内容,如果未能解决你的问题,请参考以下文章
NO19 优化Linux系统--重要开机自启动服务--关闭自启动项