Linux配置开机自启动执行脚本有哪些方法

Linux配置开机自启动执行脚本有哪些方法,第1张

Linux配置开机启动执行脚本的方法有很多,这里分享两种方法,分别是修改/etc/rc.local方法和chkconfig管理设置的方法,均可实现Linux配置开机自启动执行脚本的功能!

设置test.sh为开机要启动的脚本

[root@oldboy scripts]# vim /server/scripts/test.sh

[root@oldboy scripts]# cat /server/scripts/ test.sh

#!/bin/bash

/bin/echo $(/bin/date +%F_%T) >>/tmp/ test.log

方法一:修改/etc/rc.local

[root@oldboy ~]# ll /etc/rc.local

lrwxrwxrwx. 1 root root 13 Mar 30 10:50 /etc/rc.local ->rc.d/rc.local

修改/etc/rc.local文件

[root@oldboy scripts]# tail -n 1 /etc/rc.local

/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

重启系统,查看结果

[root@oldboy ~]# cat /tmp/test.log

2018-03-30_12:00:10

方法二:chkconfig管理

删除掉方法一的配置

[root@oldboy ~]# vim /etc/init.d/test

#!/bin/bash

# chkconfig: 3 88 88

/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null

[root@oldboy ~]# chmod +x /etc/init.d/test

添加到chkconfig,开机自启动

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

[root@oldboy ~]# chkconfig --list test

test 0:off 1:off 2:off 3:on 4:off 5:off 6:off

重启系统,查看结果

[root@oldboy ~]# cat /tmp/test.log

2018-03-30_12:00:10

2018-03-30_12:33:20

*** 作成功

关闭开机启动

[root@oldboy ~]# chkconfig test off

[root@oldboy ~]# chkconfig --list test

test 0:off 1:off 2:off 3:off 4:off 5:off 6:off

从chkconfig管理中删除test

[root@oldboy ~]# chkconfig --list test

test 0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@oldboy ~]# chkconfig --del test

[root@oldboy ~]# chkconfig --list test

service test supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add test')

本文章向大家介绍Linux Ubuntu 20.04 —添加开机启动(服务/脚本),主要包括Linux Ubuntu 20.04 —添加开机启动(服务/脚本)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

系统启动时需要加载的配置文件

一、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)

二、自己写一个shell脚本

将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。

三、通过chkconfig命令设置

四、自定义服务文件,添加到系统服务,通过Systemctl管理

1.写服务文件:如nginx.service、redis.service、supervisord.service

2.文件保存在目录下:以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。

3.设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload

4.其他命令

5.服务文件示例:

基于 Debian 的 Linux 系统默认支持 rc-local.service 服务,这个服务主要用来在系统启动时运行用户自定义的脚本命令等。

要想使用 rc-local.service 服务在系统启动时运行用户自定义的脚本命令,首先需要在 /etc 目录下创建 rc.local 文件(若已创建则跳过创建)并修改可执行权限,然后设置该服务开机自启:

打开 /etc/rc.local 文件,往其中添加运行自定义脚本的命令即可。

【注】 /etc/rc.local 以及自定义脚本中都不能使用系统变量(比如 $HOME ,原因在于其执行自定义脚本时并没有继承系统变量)。

此时会显示开机自启执行脚本的输出结果。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8728420.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-20
下一篇2023-04-20

发表评论

登录后才能评论

评论列表(0条)

    保存