centos7 添加自定义脚本开机启动

centos7 添加自定义脚本开机启动

文件列表
#系统文件
/usr/lib/systemd/system/rc-local.service
/etc/rc.d/rc.local

#自定义脚本
/root/mk_dir.sh

[root@localhost ~]# cat /root/mk_dir.sh 
#!/bin/bash

cd /etc/;
mkdir cs;
[root@localhost ~]# 

#查看配置文件是否存在

[root@localhost ~]# ls -ltr /usr/lib/systemd/system | grep rc-local
-rw-r--r--. 1 root root  736 Mar 16  2021 rc-local.service
[root@localhost ~]# 

#查看/rc.local

[root@localhost ~]# cat /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

#添加文件可执行权限

[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ls -ltr /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 499 Nov  1 20:29 /etc/rc.d/rc.local
[root@localhost ~]# 

接下来就可以将自定义的脚本写入rc.local

[root@localhost ~]# cat /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 /root/mk_dir.sh
[root@localhost ~]# 

#测试配置文件是否可用

[root@localhost ~]# systemctl start rc-local
[root@localhost ~]# systemctl status rc-local
● rc-local.service - /etc/rc.d/rc.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/rc-local.service; status; vendor >
   Active: active (exited) since Mon 2021-11-01 20:42:26 PDT; 6h ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 1029 ExecStart=/etc/rc.d/rc.local start (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4770)
   Memory: 0B
   CGroup: /system.slice/rc-local.service

Nov 01 20:42:25 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compati>
Nov 01 20:42:26 localhost.localdomain systemd[1]: Started /etc/rc.d/rc.local Compatib>

#文件生成成功
[root@localhost ~]# ls -ltr /etc/cs
total 0
[root@localhost ~]# 

#设置rc-local的服务开机自启动(PS.可能这里会出现提示信息,但不影响)

[root@localhost ~]# systemctl enable rc-local
[root@localhost ~]# reboot
[root@localhost ~]# systemctl status rc-local
● rc-local.service - /etc/rc.d/rc.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled-runtime; vendor >
   Active: active (exited) since Mon 2021-11-01 20:42:26 PDT; 6h ago
     Docs: man:systemd-rc-local-generator(8)
    Tasks: 0 (limit: 4770)
   Memory: 0B
   CGroup: /system.slice/rc-local.service

Nov 01 20:42:25 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compati>
Nov 01 20:42:26 localhost.localdomain systemd[1]: Started /etc/rc.d/rc.local Compatib>
[root@localhost ~]# 

版权声明:本文为asdd_aa原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。