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. 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null

    建议53:必要时应将不再使用的对象引用赋值为null 在CLR托管的应用程序中,存在一个“根”的概念,类型的静态字段.方法参数.以及局部变量都可以作为“根”的存在(值类型不能作为“根”,只有引用类型的 ...

  2. IOS 防坑指南

    1. 读写文件 1. IOS 8 中  stringWithContentsOfFile 已被移除 2. 创建文件必须放到 应用下 Documents 下面 // // FileHelper.swif ...

  3. wp socket tcp链接

    using System; using System.Net; /// <summary> /// 客户端通过TCP/IP连接服务端的方法,包含连接,发送数据,接收数据功能 /// < ...

  4. MySql8.0后密码认证方式问题[caching-sha2-password]

    这个问题通常在laravel中表现为类似下边的异常: local.ERROR: SQLSTATE[HY000] [2006] MySQL server has gone away {"exc ...

  5. 一条命令深度清理你的mac

    一条命令深度清理你的mac mac 用了一段时间后很快发现硬盘空间不够了,就想找一些磁盘清理的工具,但是发现居然都是收费的. 就手工操作吧.方法其实非常简单. 就一条命令, cd / du -hd 5 ...

  6. rsync实时备份备份服务搭建和使用指南

    一.Rsync企业工作场景说明: 1.利用定时任务+rsync方式实现数据同步 对于网站内部技术人员创建的数据,可以采取定时任务的方式 2.利用实时任务+rsync方式实现数据同步 对于网站外部访问用 ...

  7. 汇编工具安装二:RadASM的安装!

    已经配置好的汇编工具下载地址:http://download.csdn.net/detail/sunylat/9189543 RadASM也是一款汇编开发工具,网址:http://www.oby.ro ...

  8. 1221: Fibonacci数列 [数学]

    1221: Fibonacci数列 [数学] 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 36 统计 题目描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn- ...

  9. linux下文件权限的介绍

    linux操作系统下,使用ll查看该目录下所有文件及其文件权限,以下是对文件权限的介绍 d代表的是目录(或称之为文件夹)   红框内的这3个是代表3个组的权限每组都是3个 第一组rwx代表是本用户的权 ...

  10. 题解 P3252 【[JLOI2012]树】

    \(\Huge{[JLOI2012]树}\) 题目描述 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节点1是根节点 ...