Linux_CentOS 7 사용자 정의 부팅 자동 시작

17589 단어 CentOSLinux
1、nginx
vim /etc/init.d/nginx
#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC="nginx"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME 
set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
 $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
 kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}

do_reload() {
 kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}

case "$1" in
 start)
 echo -n "Starting $DESC: $NAME"
 do_start
 echo "."
 ;;
 stop)
 echo -n "Stopping $DESC: $NAME"
 do_stop
 echo "."
 ;;
 reload|graceful)
 echo -n "Reloading $DESC configuration..."
 do_reload
 echo "."
 ;;
 restart)
 echo -n "Restarting $DESC: $NAME"
 do_stop
 do_start
 echo "."
 ;;
 *)
 echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
 exit 3
 ;;
esac

exit 0
vim /lib/systemd/system/nginx.service
[Unit]  
Description=nginx  
After=network.target  

[Service]  
Type=forking  
ExecStart=/etc/init.d/nginx start  
ExecReload=/etc/init.d/nginx restart  
ExecStop=/etc/init.d/nginx stop  
PrivateTmp=true  

[Install]  
WantedBy=multi-user.target  

2、mysql
vim /lib/systemd/system/mysql.service
[Unit]  
Description=mysql  
After=network.target  

[Service]  
Type=forking  
ExecStart=/etc/init.d/mysql start  
ExecReload=etc/init.d/mysql restart  
ExecStop=etc/init.d/mysql  stop  
PrivateTmp=true  

[Install]  
WantedBy=multi-user.target  

3、php-fpm
vim /etc/init.d/php-fpm
#!/bin/sh  
# DateTime: 2016-09-20

# 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"  
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
vim /lib/systemd/system/php-fpm.service
[Unit]  
Description=php-fpm  
After=network.target  

[Service]  
Type=forking  
ExecStart=/etc/init.d/php-fpm start  
ExecReload=/etc/init.d/php-fpm restart
ExecStop=/etc/init.d/php-fpm stop  
PrivateTmp=true  

[Install]  
WantedBy=multi-user.target  

4、redis
vim /etc/rc.d/init.d/redis
#!/bin/sh
###########################  
PATH=/usr/local/bin:/sbin:/usr/bin:/bin  

REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
REDIS_CLI=/usr/local/redis/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/redis.conf"   

case "$1" in  
    start)  
        if [ -f $PIDFILE ]  
        then  
                echo "$PIDFILE exists, process is already running or crashed"  
        else  
                echo "Starting Redis server..."  
                $EXEC $CONF  
        fi  
        if [ "$?"="0" ]   
        then  
              echo "Redis is running..."  
        fi  
        ;;  
    stop)  
        if [ ! -f $PIDFILE ]  
        then  
                echo "$PIDFILE does not exist, process is not running"  
        else  
                PID=$(cat $PIDFILE)  
                echo "Stopping ..."  
                $REDIS_CLI -p $REDISPORT SHUTDOWN  
                while [ -x ${PIDFILE} ]  
               do  
                    echo "Waiting for Redis to shutdown ..."  
                    sleep 1  
                done  
                echo "Redis stopped"  
        fi  
        ;;  
   restart|force-reload)  
        ${0} stop  
        ${0} start  
        ;;  
  *)  
    echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
        exit 1  
esac  
##############################  
vim /lib/systemd/system/redis.service
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/redis start
ExecReload=/etc/init.d/redis restart
ExecStop=/etc/init.d/redis stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

5. 설명
1. 시작 스 크 립 트 를 만 든 후 실행 가능 한 권한 을 부여 합 니 다. 예 를 들 어:
chmod a+x /etc/init.d/nginx 
chmod a+x /etc/init.d/php-fpm

2. 서비스 문서 설명
[Unit]:     
Description:    
After:      

[Service]         
Type=forking        
ExecStart          
ExecReload     
ExecStop     
PrivateTmp=True              
  :[Service]   、  、              

[Install]         ,       

3. 서비스 파일 디 렉 터 리:
/lib/systemd/system  

4. 명령 설명:
  nginx  
systemctl start nginx.service
       
systemctl enable nginx.service
       
systemctl disable nginx.service
        
systemctl status nginx.service
      
systemctl restart nginx.service
          
systemctl list-units --type=service

참고:http://www.centoscn.com/CentOS/config/2015/0507/5374.html

좋은 웹페이지 즐겨찾기