在Linux中是通过service命令进行服务管理的,在centos中,service实际上调用了/etc/init.d目录下的shell脚本,即以下两个内容是等价的
/etc/init.d/network restart
等价于
service network restart
如何编写服务管理脚本
例如想要编写一个”nginx”服务
1) 在/etc/init.d/下创建一个nginx脚本
vi /etc/init.d/nginx
2)给脚本赋予权限
chmod +w /etc/init.d/nginx
! /bin/bash
DAEMON=/usr/local/nginx/sbin/nginx case “$1” in start) echo “Starting nginx daemon….” $DAEMON && echo “SUCCESS” ;; stop) echo “Stopping daemon….” $DAEMON -s quit && echo “SUCCESS” ;; reload) echo “Starting nginx daemon….”
$DAEMON -s reload && echo “SUCCESS”
;; restart)
echo “Starting nginx daemon….” $DAEMON -s quit
$DAEMON && echo “SUCCESS”
;; *)
echo “Usage: service nginx {start|stop|restart|reload}”
exit2
;; esac
设置服务开机自启动
chkconfig [—add][—del][—list][系统服务]
第一步:shell脚本中第二行插入以下内容
chkconfig: 35 85 15
第二步:设置开机自启动
chkconfig —add nginx