Centos中配置开机自启动的方式汇总(下)

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 Tair(兼容Redis),内存型 2GB
简介: Centos中配置开机自启动的方式汇总(下)

3、Centos7通过systemctl enble配置服务自启动


在Centos7后,更推荐通过systemctl来控制服务。


任务 旧指令 新指令
使某服务自动启动 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-unit-files
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


1、systemctl服务的目录介绍

知道服务的管理是通过 systemd,而 systemd 的配置文件大部分放置于 /usr/lib/systemd/目录内。但是 Red Hat 官方文件指出, 该目录的文件主要是原本软件所提供的设置,建议不要修改!而要修改的位置应该放置于 /etc/systemd/system/目录内。


详情查看:https://wizardforcel.gitbooks.io/vbird-linux-basic-4e/content/150.html


Centos 系统服务脚本目录:


/usr/lib/systemd/


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


/usr/lib/systemd/system/


反之,用户登录后才能运行的程序,存在用户(user)里,服务以.service结尾。


/usr/lib/systemd/user/


2、建立kibana开机服务

1)、建立kibana服务文件


cd /etc/systemd/system/
vim kibana.service


脚本内容:

[Unit]    
Description=nginx    
After=network.target         
[Service]    
Type=forking    
User=nginx
Group=nginx
ExecStart=/etc/init.d/nginx start    
ExecReload=/etc/init.d/nginx restart    
ExecStop=/etc/init.d/nginx  stop    
PrivateTmp=true    
[Install]    
WantedBy=multi-user.target


注意⚠️:

这里ExecStart、ExecReload、ExecStop的命令还是借助了上文在/etc/init.d目录下配置kibana脚本来实现。

[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]服务安装的相关设置,可设置为多用户


参数说明:

Description:描述服务

After:描述服务类别


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

Type=forking是后台运行的形式

User 服务启动用户

Group 服务启动用户组

ExecStart 为服务的具体运行命令

ExecReload 为重启命令

ExecStop 为停止命令

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


2)、赋予执行权限


chmod 754 kibana.service


依照上面的表格,权限组合就是对应权限值求和,如下:

7 = 4 + 2 + 1 读写运行权限

5 = 4 + 1 读和运行权限

4 = 4 只读权限.

这句命令的意思是将filename文件的读写运行权限赋予文件所有者,把读和运行的权限赋予群组用户,把读的权限赋予其他用户。


3)、服务的启动、停止、开机启动


查看服务状态

//重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载
systemctl daemon-reload
//查看服务状态
systemctl status kibana.service



117.png

启动服务

116.png

其中,disabled说明服务还没有开启开机自启动。


开启服务开机启动


systemctl enable kibana.service


115.png


服务状态说明:

systemctl status 服务名称

状态

说明

loaded

系统服务已经初始化完成,加载过配置

active(running) 正有一个或多个程序正在系统中执行, vsftpd就是这种模式
atcive(exited) 僅執行一次就正常結束的服務, 目前並沒有任何程序在系統中執行
atcive(waiting) 正在執行當中,不過還再等待其他的事件才能继续处理
inactive 服务关闭
enbaled 服务开机启动
disabled

服务开机不自启

static 服务开机启动项不可被管理
failed 服务开机启动项不可被管理


常用服务文件


1.nginx.service

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target

2.mysql.service

[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
#ExecReload=/usr/local/mysql/support-files/mysql.server restart
#ExecStop=/usr/local/mysql/support-files/mysql.server stop
#PrivateTmp=true
[Install]
WantedBy=multi-user.target


4.redis.service

[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www
[Install]
WantedBy=multi-user.target


5.supervisord.service

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99
[Install]
WantedBy=multi-user.target


总结


本文主要总结了Centos上配置开机自启动的3种方式


方式一:直接在/etc/rc.d/rc.local中添加服务启动命令

方式二:通过chkconfig配置服务自启动

方式三:Centos7通过systemctl enble配置服务开机自启动


鸟哥的Linux私房菜

最详细的CentOS7设置自定义开机启动服务教程

目录
相关文章
|
5月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
637 87
|
3月前
|
存储 Linux Apache
在CentOS上配置SVN至Web目录的自动同步
通过上述配置,每次当SVN仓库中提交新的更改时,`post-commit`钩子将被触发,SVN仓库的内容会自动同步到指定的Web目录,从而实现代码的连续部署。
149 16
|
3月前
|
NoSQL 安全 Linux
设置Redis在CentOS7上的自启动配置
这些步骤总结了在CentOS 7系统上设置Redis服务自启动的过程。这些命令提供了一个直接且明了的方式,确保Redis作为关键组件在系统启动时能自动运行,保障了依赖于Redis服务的应用的稳定性和可用性。
393 9
|
5月前
|
Linux
Centos6配置阿里云yum源报错
在CentOS 6配置阿里云Yum源时,可能出现EPEL仓库访问报错(404 Not Found)。解决方法:编辑`/etc/yum.repos.d/epel.repo`文件,将`enabled`和`gpgcheck`参数设为0 ``` 此设置可解决仓库无法访问的问题。
1323 29
|
5月前
|
Ubuntu 安全 Linux
CentOS与Ubuntu中防火墙配置命令集汇
有了这些,你就能遨游在 CentOS 和 Ubuntu 的海洋中,频繁地改变你的防火墙设置,快速地应对各种安全威胁,同时也能保证你的系统可以正常工作。出发吧,勇敢的编程者,随着这些命令集的涌动,扬帆起航,走向安全的网络世界!
165 5
|
6月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
450 25
|
6月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
1479 26
|
Linux 虚拟化
CentOS 7.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
1276 0
|
Linux 虚拟化 网络协议
CentOS 6.X配置连接网络
应用场景 Linux虚拟机,系统安装完毕后,无法连接网络,由于是最小化安装,很多命令无法直接yum安装,无法连接外网wget下载资源等等,造成很大的不便,因此需要进行配置连接外网! 操作指南 1. 开启VMware NAT Service 右击“计算机”,选择“管理”,在“服务和应用程序”中,选择“服务”,在右边找到“VMware NAT Service”服务,进行开启。
887 0