通常大家都会选用源码包安装一些软件,比如apache,nginx,但是默认的启动方式有些不习惯,那么我们就自己动手写一个放在/etc/init.d/下吧,下面是我用vsftp举例,大家可以把自己修改成apache源码包的路径,就可以一样使用啦。

#!/bin/bash

if [ -f /etc/init.d/functions ]; then . /etc/init.d/functions

fi

RETVAL=$?
prog=vsftpd

function start() {
        daemon vsftpd >/dev/null 2>&1
        action "Starting ftp:" /bin/true
        return $RETVAL
}

function stop() {
        killproc vsftpd >/dev/null 2>&1
      if  [ $RETVAL -eq 0  ]; then action "Stopping vsftpd:" /bin/true
elif      [ $RETVAL -ne 0  ]; then action "Starting vsftpd:" /bin/false
   else   return $REVTAL
        fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
  *)
   echo "Usage: $0 {start|stop|restart|status|reload}"
        ;;
esac
   exit $RETVAL

 

Shell脚本应用之服务启动脚本_第1张图片

如果大家存疑,欢迎指正!