CentOS 7 sytemctl 自定义服务开机启动

原文:http://blog.csdn.net/ithomer/article/details/51766319

CentOS 7继承了RHEL 7的新的特性,例如强大的systemctl,而systemctl的使用也使得系统服务的/etc/init.d的启动脚本的方式发生重大改变,也大幅提高了系统服务的运行效率。但服务的配置和以往也发生了极大的不同,变的简单而易用了许多(仁者见仁,米扑博客)。

systemd提供更优秀的框架以表示系统服务间的依赖关系,实现系统初始化时服务的并行启动,同时达到降低Shell的系统开销的效果。
systemd 目标是:尽可能启动更少进程;尽可能将更多进程并行启动,systemd尽可能减少对shell脚本的依赖。

systemd单位类型

systemctl –type=单位类型,用来过滤单位,

例如: systemctl –type=service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@mimvp_usa ~]# systemctl --type=service
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  aegis.service                      loaded active running LSB: aegis update.
  agentwatch.service                 loaded active exited  SYSV: Starts and stops guest agent
  aliyun.service                     loaded active running auto run aliyunservice or agent
  crond.service                      loaded active running Command Scheduler
  dbus.service                       loaded active running D-Bus System Message Bus
  getty@tty1.service                 loaded active running Getty on tty1
  httpd.service                      loaded active running The Apache HTTP Server
  kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
  lvm2-lvmetad.service               loaded active running LVM2 metadata daemon
  lvm2-monitor.service               loaded active exited  Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress
  mariadb.service                    loaded active running MariaDB database server
● mongod.service                     loaded failed failed  SYSV: Mongo is a scalable, document-oriented database.
  mongodb.service                    loaded active running mongodb
  network.service                    loaded active exited  LSB: Bring up/down networking
  nscd.service                       loaded active running Name Service Cache Daemon
  ntpd.service                       loaded active running Network Time Service
  polkit.service                     loaded active running Authorization Manager
  rc-local.service                   loaded active exited  /etc/rc.d/rc.local Compatibility

服务(service):管理着后台服务;
挂载(mount)自动挂载(automount):用来挂载文件系统;
目票(target):运行级别;
套接字(socket):用来创建套接字,并在访问套接字后,立即利用依赖关系间接地启动另一单位;

开机服务管理

CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,

/usr/lib/systemd/system/
/usr/lib/systemd/user/

像需要开机不登陆就能运行的程序,存在系统服务,即:/usr/lib/systemd/system/ 目录下
每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]、[Install]

[Unit] 主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Service] 是服务的关键,是服务的一些具体运行参数的设置,

Type=forking是后台运行的形式,

PIDFile为存放PID的文件路径,

ExecStart为服务的具体运行命令,

ExecReload为重启命令,

ExecStop为停止命令,

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

[Install] 是服务安装的相关设置,可设置为多用户的
 
服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system/目录下,这时就可以利用systemctl进行配置

示例:

自定义 mongodb 开机启动

vim  /usr/lib/systemd/system/mongodb.service

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=mongodb
After=auditd.service systemd-user-sessions.service time-sync.target network.target
 
[Service] 
Type=forking 
PIDFile=/var/run/mongodb/mongod.pid
ExecStart=/root/script/mongo_server_start.sh
ExecStop=/root/script/mongo_server_stop.sh
PrivateTmp=true 
 
[Install]
WantedBy=multi-user.target

systemctl 配置命令

systemctl status mongodb.service            // 查看mongodb启动状态

systemctl start mongodb.service             // 启动 mongodb

systemctl stop mongodb.service              // 关闭 mongodb

systemctl enable mongodb.service         // 开机启动 mongodb 服务

systemctl disable mongodb.service         // 开机关闭 mongodb 服务

示例:

systemctl status mongodb.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
● mongodb.service - mongodb
   Loaded: loaded (/usr/lib/systemd/system/mongodb.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2016-06-15 19:05:41 CST; 2min 24s ago
  Process: 1143 ExecStop=/root/script/mongo_server_stop.sh (code=exited, status=1/FAILURE)
  Process: 1158 ExecStart=/root/script/mongo_server_start.sh (code=exited, status=0/SUCCESS)
 Main PID: 1161 (mongod)
   CGroup: /system.slice/mongodb.service
           └─1161 /usr/bin/mongod -f /etc/mongod.conf
 
Jun 15 19:05:41 mimvp_usa systemd[1]: Starting mongodb...
Jun 15 19:05:41 mimvp_usa mongo_server_start.sh[1158]: about to fork child process, waiting until server is ready for con...ions.
Jun 15 19:05:41 mimvp_usa mongo_server_start.sh[1158]: forked process: 1161
Jun 15 19:05:41 mimvp_usa mongo_server_start.sh[1158]: child process started successfully, parent exiting
Jun 15 19:05:41 mimvp_usa systemd[1]: Started mongodb.
Hint: Some lines were ellipsized, use -l to show in full.

systemctl 虽然比较陌生,但是其实比init.d那种方式简单不少,而且使用简单,systemctl能简化的操作还有很多,现在也有不少的资料,看来RHEL/CentOS比其他的Linux发行版还是比较先进的,此次更新也终于舍弃了Linux 2.6内核,无论是速度还是稳定性都提升不少。

(转)CentOS 7 sytemctl 自定义服务开机启动的更多相关文章

  1. centos设置服务开机启动

    Linux CentOS设置服务开机启动的方法 by 天涯 · 2013/07/26 CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别 在CentOS或者RedHa ...

  2. CentOS设置服务开机启动的方法

    CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后 ...

  3. centos 6 与 centos 7 服务开机启动、关闭设置的方法

    简单说明下 centos 6 与 centos 7 服务开机启动.关闭设置的方法: centos 6 :使用chkconfig命令即可. 我们以apache服务为例: #chkconfig --add ...

  4. 【CentOS】centos7上查看服务开机启动列表

    centos7上查看服务开机启动列表 命令: systemctl list-unit-files; 点击回车,可以向下翻页查询

  5. linux系统设置服务开机启动3种方法,Linux开机启动程序详解

    linux系统设置服务开机启动 方法1:.利用ntsysv伪图形进行设置,利用root登陆 终端命令下输入ntsysv 回车:如下图     方法2:利用命令行chkconfig命令进行设置 简要说明 ...

  6. CentOS6和CentOS7服务开机启动

    CentOS 6和CentOS 7都可以定义开机启动哪些服务,但CentOS 6的命令是chkconfig,CentOS 7是systemctl. 本文将介绍两种命令的实现方式. 一.CentOS 6 ...

  7. linux系统chkconfig使用方法及服务开机启动

    一.基础知识 有关linux系统开机过程.运行等级,执行权限请看另一篇:linux系统启动过程及运行等级详解. 本篇文章实践的系统:centos6.5 二.创建服务 通过之前的说明,我们知道了如果需要 ...

  8. Centos搭建nginx环境,编译,添加服务,开机启动。

    首先安装所需的安装库,yum -y install gcc gcc-c++ autoconf libtool* openssl openssl-devel 编译的时候,若有提示错误,提示缺少某个库,y ...

  9. CentOS设置服务开机启动的两种方法

    一.通过服务的方式设置自启动 1.  在/etc/init.d 下建立相关程序的启动脚本 2.  chkconfig --add mysqld(添加服务到chkconfig列表中) chkconfig ...

随机推荐

  1. MongoDB整理笔记の安装及配置

    1.官网下载 地址:http://www.mongodb.org/downloads mongodb-linux-x86_64-2.4.9.tgz (目前为止,64位最新版本) 2.解压 切换到下载目 ...

  2. 服务器控件数据回发实现IPostBackDataHandler需注意的

    我写的服务器控件(未完,模型如此) using System; using System.Collections.Generic; using System.Collections.Specializ ...

  3. 手机打车APP的机遇与挑战

    所谓打车APP,就是个能安装在手机上的打车软件.原理是通过GPS进行定位,能够搜索附近的空车信息然后反馈给用户.同样的,空车信息也会反馈给用户.一般这种啊APP都是跟地图类软件一起的.比如百度地图,谷 ...

  4. MVC上的jsonp扩展,解决跨域访问问题

    总是有人会遇到跨域问题,然后有个jsonp的解决方案,MVC中代码如下: public class JsonpResult : System.Web.Mvc.JsonResult { object d ...

  5. 接上一篇,Springcloud使用feignclient远程调用服务404 ,为什么去掉context-path后,就能够调通

    一.问题回顾 如果application.properties文件中配置了 #项目路径 server.servlet.context-path=/pear-cache-service 则feigncl ...

  6. mybatis 学习笔记(二):mybatis SQL注入问题

    mybatis 学习笔记(二):mybatis SQL注入问题 SQL 注入攻击 首先了解下概念,什么叫SQL 注入: SQL注入攻击,简称SQL攻击或注入攻击,是发生于应用程序之数据库层的安全漏洞. ...

  7. [AGC002D] Stamp Rally 整体二分+并查集

    Description 给你一个n个点m个条边构成的简单无向连通图,有Q组询问,每次询问从两个点x,y走出两条路径,使这两条路径覆盖z个点,求得一种方案使得路径上经过的变的最大编号最小. Input ...

  8. Unity---动画系统学习(6)---Avatar Mask动画融合、Layers动画分层、IK反向动力学

    1. 介绍 Avatar Mask(动画融合) 前面我们一直介绍的都是动画混合,一般用于解决边跑边转弯的问题.而动画融合一般用于解决例如边跑边挥手的问题. 简单说就是让跑步去控制腿的骨骼,挥手控制手的 ...

  9. Linux 使用echo向文件末尾追加命令

    //echo后边用单引号包围要添加的内容 echo 'add content'>>/home/data/test.sh 注意>>表示在原来的文件末尾上进行追加,如果使用的是&g ...

  10. Chrome浏览器如何完美实现滚动截图技巧

    一.前言 我们平时在浏览网页时,想把碰到好的网页内容或者文章截屏保存,但是网页的长度常常会超出屏幕高度,一般的截屏功能只能截取显示在屏幕上的内容,那我们该如何方便快捷截取全部内容?今天就分享一个如何利 ...