首先调整好路径信息,修改配置文件
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
ExecQuit=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

由于进程已经存在,编辑好之后,执行启动命令是失败的。
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:15:54 CST; 51s ago
Process: 2378 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2376 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] still could not bind()
Nov 12 09:15:54 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:15:54 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service failed.

手动执行检查命令,发现是被占用的,也就是由于nginx已经是启动状态,无论怎么弄都不行
[root@mcw1 ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

把nginx服务停掉
[root@mcw1 ~]# ps -ef|grep nginx
root 1925 1 0 08:33 ? 00:00:00 nginx: master process nginx
nginx 2220 1925 0 09:10 ? 00:00:00 nginx: worker process
root 2411 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]# kill 1925
[root@mcw1 ~]# ps -ef|grep nginx
root 2413 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]#

用systemctl启动nginx
[root@mcw1 ~]# systemctl start nginx

这时查看进程是没有问题的
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:03 CST; 3s ago
Process: 2511 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2510 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2514 (nginx)
CGroup: /system.slice/nginx.service
├─2514 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2515 nginx: worker process

Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.

用systemctl停掉nginx,也米有问题
[root@mcw1 ~]# systemctl stop nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)

Nov 12 09:17:01 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Nov 12 09:20:23 mcw1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:26 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'

用systemctl启动和重启nginx,都米有问题
[root@mcw1 ~]# systemctl start nginx
[root@mcw1 ~]# systemctl restart nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:38 CST; 4s ago
Process: 2548 ExecStop=/usr/sbin/nginx -s stop (code=exited, status=0/SUCCESS)
Process: 2554 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2553 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2556 (nginx)
CGroup: /system.slice/nginx.service
├─2556 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2557 nginx: worker process

Nov 12 09:20:38 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:38 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@mcw1 ~]#

用systemctl停掉nginx。然后直接用命令启动nginx。再使用systemctl重启管理,这下子就不行了。
[root@mcw1 ~]# systemctl stop nginx.service
[root@mcw1 ~]# nginx
[root@mcw1 ~]# systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@mcw1 ~]#

总结:也就是如果用systemtl管理的话,后面也用systemctl就行,直接用命令启停服务,也可以。但是两者交叉管理服务,很有可能出问题

根据报错提示查看错误详情。显示有地址已经在使用的错误信息,说明服务已经启动了,但是命令直接启动的,没有用systemctl启动,所以后面的查看,启动,重启,等操作用systemctl会出错。
[root@mcw1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:28:14 CST; 3min 1s ago
Process: 2712 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2711 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
[root@mcw1 ~]# journalctl -xe
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 polkitd[902]: Unregistered Authentication Agent for unix-process:2705:524584 (system bus name :1.122, object path /org/freedesktop/PolicyK
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:30:01 mcw1 systemd[1]: Started Session 23 of user root.
-- Subject: Unit session-23.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has finished starting up.
--
-- The start-up result is done.
Nov 12 09:30:01 mcw1 systemd[1]: Starting Session 23 of user root.
-- Subject: Unit session-23.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has begun starting up.
Nov 12 09:30:01 mcw1 CROND[2718]: (root) CMD (/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1)

使用systemctl管理服务(nginx)的更多相关文章

  1. 二进制编译安装nginx并加入systemctl管理服务

    一.安装nginx所需环境 # yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y 二.安装ngi ...

  2. Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置

    使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...

  3. Linux学习-透过 systemctl 管理服务

    透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...

  4. RHEL7使用systemctl管理服务

    1.   systemctl命令 说明 systemctl    start   [unit type] 启动服务 systemctl    stop   [unit type] 停止服务 syste ...

  5. 使用systemctl管理服务

    系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...

  6. 使用 Centos 7 的 systemctl 管理服务

    CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,存系统服务里即:/usr/lib/ ...

  7. CentOS7下普通账号通过systemctl管理服务需要输入root密码问题

    问题描述: 使用普通账号test通过systemctl启动系统服务提示需要输入root密码: 解决方案: 根据上面提示得知权限由polkit进行管理,对应的是org.freedesktop.syste ...

  8. CentOS7 使用systemctl来管理服务

    1.建立service文件 以tomcat为例 , 建立tomcat.service文件 #服务说明 [Unit] #服务描述 Description=Tomcat Service #前置需要启动的服 ...

  9. 手动编译安装Libvirt之后利用systemctl管理libvirtd服务

    因为要给特殊的虚拟机关联文件指定selinux标签,而默认的Libvirt没有这个功能,所以需要修改LIbvirt源代码,重新编译安装Libvirt,而手动编译安装的LIbvirt,没有办法使用sys ...

  10. 透过systemctl管理mysqld服务

    1. 背景 CentOS 7.x 之前的版本,系统启动时,第一支呼叫的程序是 init ,然后 init 去唤起所有的系统所需要的服务,无论是本地服务还是网络服务.所有的服务启动脚本都放置于 /etc ...

随机推荐

  1. 三步就能在OpenHarmony中实现车牌识别

    介绍 本车牌识别项目是基于开源项目 EasyPR(Easy to do Plate Recognition)实现.EasyPR 是一个开源的中文车牌识别系统,基于 OpenCV 开源库开发. 本项目使 ...

  2. C# 数据类型与类型转换:包含教程与示例

    C# 数据类型 C# 中的变量必须是指定的数据类型: int myNum = 5; // 整数(整数) double myDoubleNum = 5.99D; // 浮点数 char myLetter ...

  3. 你真会判断DataGuard的延迟吗?

    这是一个比较细节的知识点,但必须要理解这个才能准确判断Oracle ADG的延迟情况. 以前做运维工作时,记得是要同时重点关注v$dataguard_stats视图中的几个字段的值,分别是:NAME. ...

  4. Hive设置map和reduce数量

    一.控制hive任务中的map数: 通常情况下,作业会通过input的目录产生一个或者多个map任务. 主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文件块大小(目前为1 ...

  5. Avalonia 中的样式和控件主题

    在 Avalonia 中,样式是定义控件外观的一种方式,而控件主题则是一组样式和资源,用于定义应用程序的整体外观和感觉.本文将深入探讨这些概念,并提供示例代码以帮助您更好地理解它们. 样式是什么? 样 ...

  6. 重新整理.net core 计1400篇[六] (.net core 一个简易版的依赖注入容器 )

    前言 我们了解到一个依赖注入的形式是: 注入依赖服务:var root = new Cat().Register<IFoo, Foo>(Lifetime.Transient); 获取对应的 ...

  7. c# 如何在一般处理程序中返回json

    前言 迁移以前的笔记. 正文 无论是一般处理程序,还是其他程序,处理事项,肯定在于HttpResponse. 这种情况就可以操作,至于字符是自己转换还是由HttpResponse中的信息默认转换,都没 ...

  8. FFmpeg开发笔记(十六)Linux交叉编译Android的OpenSSL库

    ​<FFmpeg开发实战:从零基础到短视频上线>一书的例程主要测试本地的音视频文件,当然为了安全起见,很多网络视频都采用了https地址.FFmpeg若要访问https视频,就必须集成第三 ...

  9. 【Oracle】PL/SQL制作唯一标识,固定字段+日期+流水码,流水码每日重置

    [Oracle]PL/SQL制作唯一标识,固定字段+日期+流水码,流水码每日重置 首先创建序列 create sequence sequence_name minvalue 0 maxvalue 99 ...

  10. 【hibernate】使用HQL对页面进行时间校验操作(预约)

    [hibernate]使用HQL对页面进行时间校验操作(预约) 预约系统中的时间校验 正好接了一个预约的需求,还需要用java 7和hibernate 1.时间冲突,时间段不能重复,在保存前对数据库进 ...