设为首页 加入收藏

TOP

CentOS7-启动|重启|停止|状态服务脚本(二)
2023-07-23 13:35:20 】 浏览:52
Tags:CentOS7- 启动 重启 停止
inx-1.10.0]# make && make install

12、修改pid

logs/nginx.pid

13、切换到/etc/init.d/脚本目录下编写Nginx脚本

[root@template ~]# cd /etc/init.d/
[root@template init.d]# cat nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /www/wdlinux/nginx/conf/nginx.conf
# pidfile:     /www/wdlinux/nginx/logs/nginx.pid
# Url http://www.wdlinux.cn
# Last Updated 2010.06.01
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    #service php-fpm start
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    $nginx -s stop
    echo_success
    retval=$?
    echo
    #service php-fpm stop
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    stop
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    $nginx -s reload
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
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
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

14、增加脚本你的执行权限

[root@template init.d]# chmod +x nginx

15、添加到启动服务中

[root@template init.d]# chkconfig --add nginx
[root@template init.d]# chkconfig nginx  on

16、测试

方式二:通过rpm包的脚本修改

[root@node01 system]# cat nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@node01 system]# pwd
/usr/lib/systemd/system

参数说明:

Unit:
Description:描述信息
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反;
Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit即无法激活;
Wants:依赖到的其它units,弱依赖;
Conflicts:定义units间的冲突关系。

Service:
Type:定义影响ExecStart及相关参数功能的unit进程启动类型;

simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中;
forking:由ExacStart启动的程序透过spawns延伸出其它子程序来作为此deamon的主要服务。原生父程序在启动结束后就会终止。
oneshot:与simple类似,不过
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇linux运维常用命令 下一篇第二阶段:高级核心基础知识·第1..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目