CentOS 7继承了RHEL 7的新的特性,例如强大的systemd,而systemd的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的运行效率。但服务的配置和以往也发生了极大的不同,说实在的,变的简单而易用了许多。

systemd:

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

每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],就以nginx为例吧,具体内容如下:

创建service:

在/usr/lib/systemd/system下创建nginx.service文件内容如下(看应用需求也可以在 /usr/lib/systemd/usr下创建):
[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

[Unit]

Description : 服务的简单描述
Documentation : 服务文档

After= : 依赖,仅当依赖的服务启动之后再启动自定义的服务单元

[Service]

Type : 启动类型simple、forking、oneshot、notify、dbus

Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。 Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。 Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。 Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。 Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

PIDFile : pid文件路径,对应程序的,比如nginx默认就放在logs目录下
ExecStartPre :启动前要做什么,上文中是测试配置文件 -t  
ExecStart:启动 
ExecReload:重载 
ExecStop:停止

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

[Install]

WantedBy:服务安装的用户模式,从字面上看,就是想要使用这个服务的有是谁?上文中使用的是:multi-user.target ,就是指想要使用这个服务的目录是多用户。「以上全是个人理解,瞎猜的,如有不当,请大家多多指教」每一个.target实际上是链接到我们单位文件的集合,当我们执行:

 #systemctl enable nginx.service

就会在/etc/systemd/system/multi-user.target.wants/目录下新建一个/usr/lib/systemd/system/nginx.service 文件的链接。

操作Service:

#启动服务
$ systemctl start nginx.service #查看日志
$ journalctl -f -u nginx.service
[root@localhost ~]# journalctl -f -u nginx.service
-- Logs begin at 一 -- :: CST. --
11月 :: localhost.localdomain systemd[]: Starting nginx...
11月 :: localhost.localdomain nginx[]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
11月 :: localhost.localdomain nginx[]: nginx: configuration file /etc/nginx/nginx.conf test is successful
11月 :: localhost.localdomain systemd[]: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid...ument
11月 :: localhost.localdomain systemd[]: Started nginx. #重启
$ systemctl restart nginx.service #重载
$ systemctl reload nginx.service #停止
$ systemctl stop nginx.service

CentOS 7下关于systemd的一些唠叨话二:systemd服务脚本的编写的更多相关文章

  1. CentOS 7下关于systemd的一些唠叨话一:systemd的特点和使用

    摘要 近年来,Linux 系统的 init 进程经历了两次重大的演进,传统的 sysvinit 已经逐渐淡出历史舞台,新的 UpStart 和 systemd 各有特点,越来越多的 Linux 发行版 ...

  2. CentOS 7下启动、关闭、重启、查看MySQL服务

    1.启动命令 [root@xufeng Desktop]# service mysqld startRedirecting to /bin/systemctl start mysqld.service ...

  3. Centos 6 下安装 OSSEC-2.8.1 邮件告警 (二)

    Ossec 配置邮件通知 ## 1 安装软件包: yum install -y sendmail mailx cyrus-sasl cyrus-sasl-plain #安装postfix邮件相关的软件 ...

  4. centos 7 下的 service部署

    在centos 7下部署service一般涉及到jar包部署, service脚本. 数据存贮路径, log存贮路径 jar包部署 /usr/local/app/app1/app1.jar servi ...

  5. CentOS 7下Samba服务器的安装与配置

    文基于<CentOS 6.3下Samba服务器的安装与配置>,参照原博文,自己在CentOS7环境上实现,并按照自己的环境修改博文内容 一.简介 Samba是一个能让Linux系统应用Mi ...

  6. centOS 7下无法启动网络(service network start)错误解决办法

    今天在centOS 7下更改完静态ip后发现network服务重启不了,翻遍了网络,尝试了各种方法,终于解决了. 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行serv ...

  7. Centos 7 下 Corosync + Pacemaker + psc + HA-proxy 实现业务高可用

    一.介绍: 1.本博客Corosync + Pacemaker + psc + HA-proxy 实现业务高可用,以httpd 服务实现高可用为例. 2.架构思路 a.三台web 节点,功能:全部安装 ...

  8. CentOS 7下升级MySQL5.7.23的一个坑

    发现CentOS 7下升级MySQL5.7.23的一个坑,以前面升级到MySQL 5.7.23的一个集群为例 在我们环境下打开文件描述符个数的参数open_files_limit在MySQL 5.6. ...

  9. CentOS 7下搭建配置SVN服务器

    CentOS 7下搭建配置SVN服务器 1. 安装 CentOS通过yum安装subversion. $ sudo yum install subversion subversion安装在/bin目录 ...

随机推荐

  1. spring框架和junit框架结合使用案例

    package ltssh; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.be ...

  2. Openfire 的安装和配置

    1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 下载地址: Exe:htt ...

  3. The Earth Mover's Distance

    The EMD is based on the minimal cost that must be paid to transform one distribution into the other. ...

  4. js中的回调函数的理解和使用方法

    js中的回调函数的理解和使用方法 一. 回调函数的作用 js代码会至上而下一条线执行下去,但是有时候我们需要等到一个操作结束之后再进行下一个操作,这时候就需要用到回调函数. 二. 回调函数的解释 因为 ...

  5. asp.net identity 2.2.0 中角色启用和基本使用(五)

    建立控制器UsersAdminController 第一步:在controllers文件夹上点右键>添加>控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:UsersAdminC ...

  6. jquery 判断时间

    $("#gjlycon1").blur(function () {                 var v = $(this).val();                 v ...

  7. View的drawRect方法

    1)此方法在View第一次在展示时调用,此后都用他的一个shotCut,可用setNeedsDisplay方法强制调用. 1)此方法在ViewDidAppear方法中才会调用.因此要想获得在drawR ...

  8. MYSQL中创建存储过程实现向表中循环插入数据

    首先在test数据库中先创建一个表test: CREATE TABLE test( ID INT PRIMARY KEY AUTO_INCREMENT ,test_name VARCHAR(20),t ...

  9. textkit 研究,mark一下,一个不错的开源库:MLLabel(但是没有文档)

    别人写的一个基于textkit的封装: https://github.com/molon/MLLabel 基于textkit实现的支持富文本的label, 可实现自定义emoji表情等

  10. 《Linux内核分析》第一周 计算机是如何工作的?

    刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK ONE(2. ...