编写开机启动服务

centos 7.x编写开机启动服务

centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提升系统的启动速度。关于Systemd的详情介绍在这里linux

Systemd服务文件以.service结尾,好比如今要创建nginx为开机启动,若是用yum install命令安装的,yum命令会自动建立nginx.service文件,直接用命令nginx

设置开机启动便可。
在这里我是用源码编译安装的,因此要手动建立nginx.service服务文件。
开机没有登录状况下就能运行的程序,存在系统服务(system)里,即:vim

1.在系统服务目录里建立nginx.service文件centos

内容以下xcode

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

保存退出。ui

2.设置开机启动spa

3.其它命令
启动nginx服务设计

设置开机自启动3d

中止开机自启动

查看服务当前状态

从新启动服务

查看全部已启动的服务

4.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) 改变至多用户运行级别。

5.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
相关文章
相关标签/搜索