您的当前位置:首页正文

Linux配置开机自启动的两种方法

来源:个人技术集锦
Linux配置开机⾃启动的两种⽅法

⼀、通过rc.local该⽂件实现开机⾃启

1:编写测试脚本

[root@host1 ~]# vim test.sh#!/bin/bash

/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log##开机启动打印当前时间输出到test.log⽂本⾥

2:测试脚本完成之后,更改rc.local配置⽂件[root@host1 ~]# vim /etc/rc.d/rc.local #!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES#

# It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.#

# In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.#

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.

touch /var/lock/subsys/local

/bin/bash /tmp/test.sh >/dev/null 2>/dev/null ##加上这⼀段配置,让其开机执⾏ 保存退出

3:在centos7中,/etc/rc.d/rc.local没有执⾏权限,需要授权,然后就可以重启机器验证就⾏了[root@host1 ~]# chmod +x /etc/rc.d/rc.local

4:重启之后查看结果

[root@host1 ~]# cat /tmp/test.log 2019-06-02_16:44:53

⼆、通过chkcongfig开机启动服务来实现

1:在/etc/init.d/编辑⼀个测试脚本[root@host1 ~]# vim /etc/init.d/test#!/bin/bash

# chkconfig: 3 88 88

/bin/bash /tmp/test.sh >/dev/null 2>/dev/null 保存退出

2:赋予执⾏权限

[root@host1 ~]# chmod 755 /etc/init.d/test

3:加⼊开机启动服务列表

[root@host1 ~]# chkconfig --add test

4:查看开机启动服务列表

[root@host1 ~]# chkconfig --list

注:该输出结果只显⽰ SysV 服务,并不包含原⽣ systemd 服务。SysV 配置数据可能被原⽣ systemd 配置覆盖。

要列出 systemd 服务,请执⾏ 'systemctl list-unit-files'。查看在具体 target 启⽤的服务请执⾏'systemctl list-dependencies [target]'。

netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关network 0:关 1:关 2:开 3:开 4:开 5:开 6:关test 0:关 1:关 2:关 3:开 4:关 5:关 6:关

5:重启系统之后查看结果

[root@host1 ~]# cat /tmp/test.log 2019-06-02_16:44:532019-06-02_16:48:45

因篇幅问题不能全部显示,请点此查看更多更全内容