开机自启动设置 开机自启动

2023-04-28 20:34 • 阅读 181
很多时候,我们需要将一些服务在Linux系统启动时即自动运行,省得每次都要去手动启动一遍,如Redis,MySQL,Nginx等 。本文对CentOS与Ubuntu下开机自启动的配置方法进行整理,供参考查阅 。 CentOS7的开机自启动配置一. rc.local方式 rc.local是CentOS

很多时候,我们需要将一些服务在Linux系统启动时即自动运行,省得每次都要去手动启动一遍,如Redis,MySQL,Nginx等 。本文对CentOS与Ubuntu下开机自启动的配置方法进行整理,供参考查阅 。
CentOS7的开机自启动配

置一. rc.local方式
rc.local是CentOS以前版本的方式,在CentOS7中仍然以兼容的形式存在,虽仍可用,但不推荐(推荐使用systemd service) 。
1、编写需要开机自启动的脚本,并添加执行权限
[root@dev-server-1 ~]# vim test_rclocal.sh
#!/bin/bash
time=`date +%F_%T`
echo $time' from rc.local' >> /tmp/test.log
[root@dev-server-1 ~]# chmod +x test_rclocal.sh复制代码
作为测试,上述脚本打印一个时间到/tmp/test.log文件中
2、在/etc/rc.d/rc.local配置文件中添加脚本运行命令(使用绝对路径)
[root@dev-server-1 ~]# vim /etc/rc.d/rc.local
#!/bin/bash
# …注释部分
touch /var/lock/subsys/local
/root/test_rclocal.sh >/dev/null 2>/dev/null
复制代码
3、添加/etc/rc.d/rc.local文件的执行权限
在centos7中,/etc/rc.d/rc.local没有执行权限,需要手动授权
[root@dev-server-1 ~]# chmod +x /etc/rc.d/rc.local复制代码
以上三步,即可使/root/test_rclocal.sh >/dev/null 2>/dev/null 命令在服务器系统启动时自动运行 。
二. chkconfig方式
1、编写需要开机自启动的测试脚本,并添加执行权限
[root@dev-server-1 ~]# vim test_chkconfig.sh
#!/bin/bash
time=`date +%F_%T`
echo $time' from chkconfig' >> /tmp/test.log
[root@dev-server-1 ~]# chmod +x test_chkconfig.sh复制代码
2、在/etc/rc.d/init.d/目录下添加一个可执行脚本testchkconfig
[root@dev-server-1 ~]# vim /etc/rc.d/init.d/testchkconfig
#!/bin/bash
# chkconfig: 2345 90 10
# description: test chkconfig
/root/test_chkconfig.sh >/dev/null 2>/dev/null
[root@dev-server-1 ~]# chmod 755 /etc/rc.d/init.d/testchkconfig复制代码
上述testchkconfig脚本的头部必须遵循一定的格式 # chkconfig: 2345 90 10,其中2345指定服务在哪些执行等级中开启或关闭,90表示启动的优先级(0-100,越大优先级越低),10表示关闭的优先级 。执行等级包括
0:表示关机1:单用户模式2:无网络连接的多用户命令行模式3:有网络连接的多用户命令行模式4:保留未使用5:带图形界面的多用户模式6:重新启动
3、加入开机启动服务列表
[root@dev-server-1 ~]# chkconfig –add testchkconfig
[root@dev-server-1 ~]# chkconfig –list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole0:off1:off2:off3:off4:off5:off6:off
network0:off1:off2:on3:on4:on5:on6:off
testchkconfig0:off1:off2:on3:on4:on5:on6:off复制代码
使用 chkconfig –list 可查看当前加入开机自启动的服务列表,但如Note部分所述,该命令只显示SysV服务,不包含原生的systemd服务,查看systemd服务可使用systemctl list-unit-files命令 。
以上三步,即可使/root/test_chkconfig.sh >/dev/null 2>/dev/null 命令在服务器系统启动时自动运行 。
chkconfig的其它命令参考
$chkconfig –list # 表示查看所有服务在各个运行级别下的状态 。
$chkconfig testchkconfig on # 表示指定服务在所有的运行级别下都是开启状态 。
$chkconfig testchkconfig off # 表示指定服务在所有的运行级别下都是关闭状态 。

上一页123下一页


风寒感冒多久才能好 风寒感冒是自限性疾病 大自然的指南针有哪些? 怎么样设置开机密码和待机密码 怎么样设置开机密码 乡愿是什么意思? 乡愿出自哪里 excel自动排序 excel自动排序编号 水晶自己在家怎么消磁 高铁自热米饭能带吗 自调面膜配方美白 美白的最快方法 是不是每个人都有体香-为什么自己闻不到自己的体香 溺水时的自救方法五条

本文由本地通发布,如若转载,请注明出处:http://www.bdtong.com.cn/bk/118108.html