傅令江的光影色彩世界
留住记忆的点滴
  • 首页
  • 文学
    • 诗词鉴赏
    • 美文共读
    • 原创
  • 编程
    • php
    • asp
    • .net
    • VB
    • C/C++
    • 易语言
    • js
    • 其他
    • 逆向
  • 运维
    • windows
    • linux
  • 光影色彩
    • 电影
    • 音乐
  • 科技
    • 互联网
    • 手机
  • 生活
    • 情感
  • 微语
10月252017

CentOS 7.x设置自定义开机启动,添加自定义系统服务

作者:令狐江   发布:2017-10-25 17:43   分类:linux   阅读:6534次   评论:0条  

Centos 系统服务脚本目录:

[plain] view plain copy
 print?
  1. /usr/lib/systemd/    
有系统(system)和用户(user)之分,

如需要开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:

[plain] view plain copy
 print?
  1. lib/systemd/system/    
反之,用户登录后才能运行的程序,存在用户(user)里

服务以.service结尾。

这边以nginx开机运行为例 IT

1.建立服务文件

[plain] view plain copy
 print?
  1. vim /lib/systemd/system/nginx.service   
[plain] view plain copy
 print?
  1. [Unit]    
  2. Description=nginx    
  3. After=network.target    
  4.      
  5. [Service]    
  6. Type=forking    
  7. ExecStart=/www/lanmps/init.d/nginx start    
  8. ExecReload=/www/lanmps/init.d/nginx restart    
  9. ExecStop=/www/lanmps/init.d/nginx  stop    
  10. PrivateTmp=true    
  11.      
  12. [Install]    
  13. WantedBy=multi-user.target   

[Unit]:服务的说明

Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户 Linux学习,http:// linux.it.net.cn

2.保存目录

以754的权限保存在目录:

[plain] view plain copy
 print?
  1. /lib/systemd/system  

3.设置开机自启动

[plain] view plain copy
 print?
  1. systemctl enable nginx.service  

4.其他命令

任务 旧指令 新指令
使某服务自动启动 chkconfig --level 3 httpd  on              systemctl enable httpd.service
使某服务不自动启动 chkconfig --level 3 httpd off systemctl disable httpd.service
检查服务状态 service httpd status systemctl status httpd.service (服务详细信息)  
systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务 chkconfig --list systemctl list-units --type=service
启动某服务 service httpd start systemctl start httpd.service
停止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service
启动nginx服务 I
[plain] view plain copy
 print?
  1. systemctl start nginx.service  

设置开机自启动

[plain] view plain copy
 print?
  1. systemctl enable nginx.service  
停止开机自启动

[plain] view plain copy
 print?
  1. systemctl disable nginx.service  
查看服务当前状态

[plain] view plain copy
 print?
  1. systemctl status nginx.service  
重新启动服务

[plain] view plain copy
 print?
  1. systemctl restart nginx.service   
查看所有已启动的服务
[plain] view plain copy
 print?
  1. systemctl list-units --type=service  

 

 

 

systemctl  -- 系统服务管理器

systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

直接运行命令可以列出所有正在运行的服务,输出列表具有更详细的信息,比如:


[root@beyes   command]# systemctl
... ...
sendmail.service          loaded active exited        LSB: start and stop sendmail
sshd.service              loaded active running       LSB: Start up the OpenSSH server daemon
udev.service              loaded active running       udev Kernel Device Manager
... ...



这里,还有一个 systemd-cgls 命令可以以树状的形式列出正在运行的进程信息。

如果要启动 httpd 服务,那么运行下面命令:

[root@beyes   command]# systemctl start httpd.service


注意,上面的 httpd 后面的 .service 是不能少的。

同理,停止服务和重启服务可以分别如下运行命令:

# systemctl stop httpd.service       #停止服务
# systemctl restart httpd.service              #重启服务



如果我们要查看服务的运行状态,那么如下运行:

[root@beyes   command]# systemctl status httpd.service
httpd.service - LSB: start and stop Apache HTTP Server
      Loaded: loaded (/etc/rc.d/init.d/httpd)
      Active: active (running) since Wed, 22 Feb 2012 10:37:30 +0800; 2s ago
     Process: 2573 ExecStop=/etc/rc.d/init.d/httpd stop (code=exited, status=0/SUCCESS)
     Process: 2589 ExecStart=/etc/rc.d/init.d/httpd start (code=exited, status=0/SUCCESS)
    Main PID: 2594 (httpd)
      CGroup: name=systemd:/system/httpd.service
          ├ 2594 /usr/sbin/httpd
          ├ 2596 /usr/sbin/httpd
          ├ 2597 /usr/sbin/httpd
          ├ 2598 /usr/sbin/httpd
          ├ 2599 /usr/sbin/httpd
          ├ 2600 /usr/sbin/httpd
          ├ 2601 /usr/sbin/httpd
          ├ 2602 /usr/sbin/httpd
          └ 2603 /usr/sbin/httpd



如果我们打算让服务可以随机启动,那么如下运行:

[root@beyes   command]# systemctl enable httpd.service
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd on



用 chkconfig 命令检测一下服务是否运行成功

[root@beyes   command]# chkconfig --list |grep httpd

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

httpd              0:off    1:off    2:on    3:on    4:on    5:on    6:off


可见服务已经在 第2 到 第5 运行等级打开。

同理禁止服务随机启动可以如下运行:

[root@beyes   command]# systemctl disable httpd.service
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd off


用 chkconfig 检测一下:

[root@beyes   command]# chkconfig --list |grep httpd

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

httpd              0:off    1:off    2:off    3:off    4:off    5:off    6:off


已经关闭成功。

此外,我们还可以直接检测服务是否随机启动或否,如下运行命令:

[root@beyes   command]# systemctl is-enabled httpd.service; echo $?
httpd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig httpd --level=5
0


上面,0 表示该服务已经使能随机启动;如果返回 1 那么表示该服务不随机启动

 




本文固定链接: https://www.fulingjiang.cn/linux/141.html

blogger
该日志由 令狐江 于2017-10-25 17:43 Wednesday发表在 linux 分类下。
版权所有:《傅令江的光影色彩世界》 → 《CentOS 7.x设置自定义开机启动,添加自定义系统服务》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:
上一篇::【MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.
下一篇:No space left on device错误解决笔记

热门文章

  • 兄弟二周年祭

相关文章

  • CentOS/RHEL 安装Install Subversion 1.8.9 ( SVN Client ) 安装最新版本的svn客户端
  • 利用CloudFlare的api获取本机IP和地区
  • vsftpd:500 OOPS: vsftpd: refusing to run with writable root inside chroot ()错误的解决方法
  • 增加/删除 SWAP分区空间的方法
  • Linux shell快速查找PHP木马
取消回复

发表评论

亲,头像对么?

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


  • 日历

  • 存档

    • 2024年10月(1)
    • 2023年2月(1)
    • 2022年11月(1)
    • 2022年10月(10)
    • 2022年9月(13)
    • 2022年8月(2)
    • 2022年7月(14)
    • 2022年6月(2)
    • 2022年5月(8)
    • 2022年4月(7)
    • 2022年3月(13)
    • 2022年2月(2)
    • 2022年1月(9)
    • 2021年12月(2)
    • 2021年11月(4)
    • 2021年10月(2)
    • 2021年9月(6)
    • 2021年7月(4)
    • 2021年6月(3)
    • 2021年5月(3)
    • 2021年4月(11)
    • 2021年3月(13)
    • 2021年2月(2)
    • 2021年1月(1)
    • 2020年12月(1)
    • 2020年4月(5)
    • 2019年9月(1)
    • 2019年8月(1)
    • 2019年5月(3)
    • 2018年3月(1)
    • 2017年10月(1)
    • 2016年7月(1)
    • 2016年4月(1)
    • 2015年12月(1)
    • 2015年11月(3)
    • 2015年9月(1)
    • 2015年8月(10)
    • 2015年7月(1)
    • 2015年6月(1)
    • 2015年4月(1)
    • 2015年3月(3)
    • 2015年2月(8)
    • 2015年1月(4)
    • 2014年12月(1)
    • 2014年11月(27)
    • 2014年10月(13)
    • 2014年9月(14)
    • 2014年8月(26)
    • 2014年7月(21)
  • 最新评论

    • 令狐江:
      喜欢这首歌是因为可以引起共鸣!
  • 链接

    • 演讲稿网
    • Recollect
    • 演讲稿
    • 祁阳人生活网
    • 我爱演讲稿网
  • 搜索

  • 标签

      函数 自定义方法 SEO 分页 分页函数 分页方法 nginx重新的一些规则
  • 分类

    • 文学(0)
    • 编程(0)
    • 运维(0)
    • 光影色彩(0)
    • 科技(0)
    • 生活(0)
    • 诗词鉴赏(3)
    • 美文共读(1)
    • 原创(10)
    • php(111)
    • asp(1)
    • .net(0)
    • VB(0)
    • C/C++(0)
    • 易语言(0)
    • js(8)
    • 其他(9)
    • 逆向(2)
    • windows(11)
    • linux(121)
    • 电影(0)
    • 音乐(1)
    • 互联网(4)
    • 手机(0)
    • 情感(2)
  • 最新文章热门文章随机文章

    • 兄弟二周年祭
    • openai给的ionCube 解密代码,应该是老版本可以这样
    • WordPress – 5秒盾防CC(PHP通用代码)
    • 我高中最好的朋友今天猝死了-伤心得不行
    • Linux系统中 systemd-journaldCPU占用异常的解决方法
    • SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法
    • 解决Linux读写nfs共享盘速度慢的问题
    • php 获取302跳转后的地址
    • 让vsftp显示隐藏文件的办法,比如显示 .htaccess
    • 添加自签名https证书到centos系统信任的问题
    • PHP获取页面的JS和CSS的总数和文件
    • php采集文章过滤版权信息
    • 提取google搜索数据,遭遇屏蔽的解决办法用php_Curl获取Cookie!
    • php smtp发送邮件类,带使用示例
    • 不带www的跳转到www的域名上,重写规则!
Copyright © 2001-2025 傅令江的光影色彩世界. Powered by www.fulingjiang.cn ICP备案:京ICP备14015190号-5