service命令其实是去/etc/init.d目录下,去执行相关程序
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl
vim /etc/init.d/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: /usr/local/tengine/conf/nginx.conf【如果开启的话,换成自己的安装路径】 # pidfile: /usr/local/tengine/logs/nginx.pid【如果开启的话,换成自己的安装路径】 # 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/tengine/sbin/nginx" prog=$(basename $nginx) #【换成自己的安装路径】 NGINX_CONF_FILE="/usr/local/tengine/conf/nginx.conf" 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 [ $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 $nginx -HUP 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
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start service nginx stop service nginx reload service nginx status
vim /usr/lib/systemd/system/nginx.service
[Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking是后台运行的形式 ExecStart为服务的具体运行命令 ExecReload为重启命令 ExecStop为停止命令 PrivateTmp=True表示给服务分配独立的临时空间 注意:启动、重启、停止命令全部要求使用绝对路径 [Install]服务安装的相关设置,可设置为多用户
路径配置不正确会导致目录报错呦!
# 对服务的说明 [Unit] # 描述服务 Description=nginx # 描述服务类别 After=network.target # 服务的一些具体运行参数的设置 [Service] # 后台运行的形式 Type=forking PIDFile=/usr/local/tengine/logs/nginx.pid # 启动准备 ExecStartPre=/usr/local/tengine/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf # 启动命令 ExecStart=/usr/local/tengine/sbin/nginx ExecReload=/usr/local/tengine/sbin/nginx -s reload ExecStop=/usr/local/tengine/sbin/nginx -s stop ExecQuit=/usr/local/nginx/sbin/nginx -s quit # Stop quit 是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求。 # Nginx在退出前完成已经接受的连接请求 PrivateTmp=true [Install] # 给服务用户的模式 WantedBy=multi-user.target
chmod 755 /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service # 启动 Nginx systemctl stop nginx.service # 停止 Nginx # 重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效) systemctl restart nginx.service # 重启 Nginx systemctl reload nginx.service # 重新加载 Nginx,用于修改配置后 systemctl enable nginx.service # 设置开机启动 Nginx systemctl disable nginx.service # 关闭开机启动 Nginx systemctl status nginx.service # 查看 Nginx 运行状态
systemctl reload nginx Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. nginx -t 查一下 # 报错信息 "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/temgine/conf/nginx.conf:52 nginx: configuration file /usr/local/temgine/conf/nginx.conf test failed # 成功的 nginx: the configuration file /usr/local/temgine/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/temgine/conf/nginx.conf test is successful
建议了解下systemd这个这个内容 https://coding.imooc.com/class/223.html
一样的是:都启动了同一个Nginx进程。 不一样的是: 1、Systemd 是一系列工具的集合,其作用也远远不仅是启动操作系统,它还接管了后台服务、结束、状态查询。 2、Systemd更方便作服务管理,启动管理Nginx更佳方便。 3、Systemd可以作开机服务管理,让服务跟随系统启动