1、分别为每个编写shell脚本放入/etc/init.d下,添加service服务
2、把每个service服务加入到chkconfig列表
这里我们以php-fpm为例说明下步骤:
php-fpm:
#!/bin/sh # chkconfig: 2345 10 90 //这两句不写添加启动器失败 # description: php-fpm //这两句不写添加自启动失败 # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 phpfpm="/usr/local/php/sbin/php-fpm" //自己php的安装路径 prog=$(basename ${phpfpm}) lockfile=/var/lock/subsys/phpfpm start() { [ -x ${phpfpm} ] || exit 5 echo -n $"Starting $prog: " daemon ${phpfpm} retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc ${phpfpm} -HUP RETVAL=$? echo } force_reload() { restart } configtest() { ${phpfpm} -t } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; status) rh_status ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|configtest}" exit 2 esac
1、把上面的文件命名为php-fpm,放到/etc/init.d下
通过chmod 755 php-fpm对这个文件开启全用户可执行权限
这里很多可以用service操作的服务。
而我们新添加的php-fpm也可以用service servicename stop|start|restart进行操作了
我们来操作一把:
爽!
2、接下里我们把php-fpm加入到开机自启动
结果报错了,查了很多资料,原来是在php-fpm的shell里面少写了两行,我们给补上
再来执行:
好了!
此时我们已经把php-fpm加入到了自启动行列了。关于chkconfig的用法,还有很多
chkconfig --list [name]:显示所有运行级系统服务的运行状态信息(on或off)。如果指定了name,那么只显示指定的服务在不同运行级的状态。
chkconfig --add name:增加一项新的服务。chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立。
chkconfig --del name:删除服务,并把相关符号连接从/etc/rc[0-6].d删除。
chkconfig [--level levels] name:设置某一服务在指定的运行级是被启动,停止还是重置。
比如我要查看chkconfig目前的任务列表:
>>chkconfig --list
就能看到我们刚添加进任务的php-fpm
里面的0-6表示了不同的等级
nginx设置
1、添加进service服务
在/etc/init.d目录下添加nginx文件:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
2、添加进开机自启动
>>chkconfig nginx on