zoukankan      html  css  js  c++  java
  • centos 7.x编写开机启动服务

    在系统服务目录里创建nginx.service文件

    vi /lib/systemd/system/nginx.service
    

    内容如下

    [Unit]
    Description=nginx
    After=network.target
      
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
      
    [Install]
    WantedBy=multi-user.target
    

    [Unit]
    Description=uwsgi
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/uwsgi --ini /usr/local/nginx/uwsgi_temp/uwsgi.ini
    ExecStop=/usr/bin/uwsgi --stop /usr/local/nginx/uwsgi_temp/uwsgi.pid
    ExecReload=/usr/bin/uwsgi --reload uwsgi.pid
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    Description:描述服务
    After:描述服务类别
    [Service]服务运行参数的设置
    Type=forking是后台运行的形式
    ExecStart为服务的具体运行命令
    ExecReload为重启命令
    ExecStop为停止命令
    PrivateTmp=True表示给服务分配独立的临时空间
    注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
    [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
    保存退出。

    设置开机自启动
    systemctl enable nginx.service
    启动nginx服务
    systemctl start nginx.service 
    停止开机自启动
    systemctl disable nginx.service
    查看服务当前状态
    systemctl status nginx.service
    重新启动服务
    systemctl restart nginx.service 
    查看所有已启动的服务
    systemctl list-units --type=service
    

    Systemd 命令和 sysvinit 命令的对照表

    Sysvinit 命令 Systemd 命令 备注
    service foo start systemctl start foo.service 用来启动一个服务 (并不会重启现有的)
    service foo stop systemctl stop foo.service 用来停止一个服务 (并不会重启现有的)。
    service foo restart systemctl restart foo.service 用来停止并启动一个服务。
    service foo reload systemctl reload foo.service 当支持时,重新装载配置文件而不中断等待操作。
    service foo condrestart systemctl condrestart foo.service 如果服务正在运行那么重启它。
    service foo status systemctl status foo.service 汇报服务是否正在运行。
    ls /etc/rc.d/init.d/ systemctl list-unit-files –type=service 用来列出可以启动或停止的服务列表。
    chkconfig foo on systemctl enable foo.service 在下次启动时或满足其他触发条件时设置服务为启用
    chkconfig foo off systemctl disable foo.service 在下次启动时或满足其他触发条件时设置服务为禁用
    chkconfig foo systemctl is-enabled foo.service 用来检查一个服务在当前环境下被配置为启用还是禁用。
    chkconfig –list systemctl list-unit-files –type=service 输出在各个运行级别下服务的启用和禁用情况
    chkconfig foo –list ls /etc/systemd/system/*.wants/foo.service 用来列出该服务在哪些运行级别下启用和禁用。
    chkconfig foo –add systemctl daemon-reload 当您创建新服务文件或者变更设置时使用。
    telinit 3 systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) 改变至多用户运行级别。

    Sysvinit 运行级别和 systemd 目标的对应表

    Sysvinit运行级别 Systemd 目标 备注
    0 runlevel0.target, poweroff.target 关闭系统。
    1, s, single runlevel1.target, rescue.target 单用户模式。
    2, 4 runlevel2.target, runlevel4.target, multi-user.target 用户定义/域特定运行级别。默认等同于 3。
    3 runlevel3.target, multi-user.target 多用户,非图形化。用户可以通过多个控制台或网络登录。
    5 runlevel5.target, graphical.target 多用户,图形化。通常为所有运行级别 3 的服务外加图形化登录。
    6 runlevel6.target, reboot.target 重启
    emergency emergency.target 紧急 Shell
  • 相关阅读:
    表达式树作为条件封装多表连查
    EF之结构进一步优化
    EF之ExecuteSqlCommand更新出现无效的解决方案
    dynamic与匿名对象
    webapi 通过dynamic 接收可变参数
    EF INNER JOIN,LEFT JOIN,GROUP JOIN
    Linq join on 多条件
    Excel 行列转置 解决竖向拉,字母跟着递增的问题
    Windows7 安装vs2015 之后 调试Web项目IIS启动不了 aspnetcore.dll未能加载
    Mysql 服务在本机,需要单机调试Mysql数据库 发生 不认识hostname‘localhost’
  • 原文地址:https://www.cnblogs.com/mrrr/p/10307536.html
Copyright © 2011-2022 走看看