Linux服务开机自启动设置

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Linux中也有类似于Window中的开机自启动服务,主要是通过chkconfig命令来设置。它主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

Linux中也有类似于Window中的开机自启动服务,主要是通过chkconfig命令来设置。它主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

参数说明:

[root@DB-Server rc2.d]# chkconfig --help
chkconfig version 1.3.30.2 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
 
usage:   chkconfig --list [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig [--level <levels>] <name> <on|off|reset|resetpriorities>
[root@DB-Server rc2.d]# chkconfig
chkconfig version 1.3.30.2 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
 
usage:   chkconfig --list [name]
         chkconfig --add <name>
         chkconfig --del <name>
         chkconfig [--level <levels>] <name> <on|off|reset|resetpriorities>

 

 

--list 显示开机可以自动启动的服务

--add 新增所指定的开机自启动系统服务

--del 删除所指定的系统服务

--level 指定该系统服务要在那个执行等级中开启或关闭。

on/off/reset 在指定的执行登记,开启/关闭/重置该系统服务

 

使用案例:

chkconfig --list      #显示开机可以自动启动的服务
 
chkconfig --add ***   #增加开机自动启动的***服务
 
chkconfig --del ***   #删除开机自动启动的***服务
 
chkconfig --level mysql 2345 on #设置mysql在运行级别为2、3、4、5的情况下都是on(开启)的状态

 

[root@DB-Server ~]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@DB-Server ~]# 

--level<等级代号> 指定系统服务要在哪一个执行等级中开启或关闭

等级0表示:表示关机

等级1表示:单用户模式

等级2表示:无网络连接的多用户命令行模式

等级3表示:有网络连接的多用户命令行模式

等级4表示:未定义

等级5表示:带图形界面的多用户模式

等级6表示:重新启动

 

[root@DB-Server ~]# chkconfig --list mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@DB-Server ~]# chkconfig --del mysql
[root@DB-Server ~]# chkconfig --list | grep mysql
[root@DB-Server ~]# chkconfig --add mysql
You have new mail in /var/spool/mail/root
[root@DB-Server ~]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@DB-Server ~]# 

 

如果我们要在运行级别3上开机自启动mysql服务,在其它运行级别关闭,可以通过下面命令来设置。

[root@DB-Server ~]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@DB-Server ~]#  chkconfig --level 3 mysql on
[root@DB-Server ~]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@DB-Server ~]#  chkconfig --level 2 mysql off
[root@DB-Server ~]#  chkconfig --level 4 mysql off
[root@DB-Server ~]# chkconfig --list | grep mysql
mysql           0:off   1:off   2:off   3:on    4:off   5:on    6:off
[root@DB-Server ~]# 

当然也可以用下面命令来开启,关闭服务的开启自启动.

chkconfig service_name on
chkconfig service_name on --level runlevels
chkconfig service_name off --level runlevels

[root@DB-Server ~]# chkconfig mysql on --level 3
[root@DB-Server ~]# chkconfig mysql off --level 45
[root@DB-Server ~]# 

新增开机自启动MySQL服务,我们会在/etc/rc.d/rcn.d下看到生成了一个对应的文件,例如/etc/rc.d/rc3.d

我们删除开机自动启动的mysql服务,就会看到对应的文件也被删除。

[root@DB-Server rc3.d]# chkconfig --del mysql
 
[root@DB-Server rc3.d]# ls *mysql*
 
ls: *mysql*: No such file or directory
 
[root@DB-Server rc3.d]# 

chkcofig后的三个参数: 第一个参数,它告诉chkconf这个服务以什么样的运行级别开始。第二个参数,它指定的了启动的优先等级。最后一个参数,它指定了停止服务时,它拥有的优先级别。上面的这个实例中,它表示这个服务以2、3、4和5的级别开始,它的启动优先级为64,它的停止优先级是36。

[root@DB-Server rc3.d]# chkconfig --add mysql

 

参考资料:

http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html

http://network810.blog.51cto.com/2212549/1137972

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
10天前
|
NoSQL Linux Redis
linux 下和win下安装redis 并添加开机自启 图文详解
linux 下和win下安装redis 并添加开机自启 图文详解
15 0
|
24天前
|
算法 Linux C++
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
【Linux系统编程】解析获取和设置文件信息与权限的Linux系统调用
29 0
|
24天前
|
Linux API C语言
【Linux系统编程】深入理解Linux 组ID和附属组ID的查询与设置
【Linux系统编程】深入理解Linux 组ID和附属组ID的查询与设置
30 0
【Linux系统编程】深入理解Linux 组ID和附属组ID的查询与设置
|
28天前
|
编解码 数据可视化 Linux
【Shell 命令集合 系统设置 】Linux 设置分辨率和颜色 SVGATextMode命令 使用指南
【Shell 命令集合 系统设置 】Linux 设置分辨率和颜色 SVGATextMode命令 使用指南
32 0
|
28天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】⭐ Linux 取消或删除已设置的环境变量 unset命令 使用指南
【Shell 命令集合 系统设置 】⭐ Linux 取消或删除已设置的环境变量 unset命令 使用指南
34 0
|
3天前
|
存储 关系型数据库 MySQL
Linux服务详解
Linux服务详解
17 0
|
4天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
10天前
|
安全 Unix Linux
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
14 0
|
21天前
|
Linux
linux中服务管理
在Linux服务管理中,从传统的System V init到Upstart,再到广泛采用的systemd,管理方式不断发展。systemd以其强大的功能和依赖解决成为现代Linux的核心。`systemctl`是管理服务的关键命令,如启动(`start`)、停止(`stop`)、重启(`restart`)服务及设置开机启动(`enable`)或取消(`disable`)。了解和服务管理,特别是systemd和`systemctl`的使用,对系统管理员至关重要。其他如xinetd用于控制网络服务,而特定环境下有OpenRC等工具。
17 2
|
28天前
|
算法 Unix Shell
【Shell 命令集合 系统管理 】Linux 显示或设置系统时间和日期 date命令 使用指南
【Shell 命令集合 系统管理 】Linux 显示或设置系统时间和日期 date命令 使用指南
30 0