linux(centos)启动时执行指定脚本

我就是我 2022-09-10 14:20 415阅读 0赞

业务需求:服务器重启后,一些操作每次都要重复操作,比如启动某些服务,我们可以写一个脚本帮助我们自动去启动服务,不需要每次重启服务器后都进行配置。
注意:以下演示的代码都是在root用户下执行,如果非root用户可能存在权限问题,需要加sudo执行。

首先在我们的centos中编写一个测试脚本文件,这里我放在/usr/shells文件夹下

  1. mkdir -p /usr/shells
  2. cd /usr/shells
  3. vim start.sh

脚本内容

  1. #脚本中的代码
  2. #!/bin/bash
  3. #这里写了一个简单的测试脚本,具体的操作根据你的需求进行编写
  4. rm -rf /usr/shells/test.txt
  5. #脚本授予执行权限,为所有用户
  6. chmod +x /usr/shells/start.sh
  7. #/usr/shells目录下创建一个文件test.txt,用来测试脚本删除是否成功
  8. touch /usr/shells/test.txt
  9. ls
  10. #输出结果
  11. start.sh test.txt

crontab开机自动执行脚本

方式1:创建一个定时任务

  1. 首先我们采用crontab来实现开机自动运行脚本

    crontab -e 创建一个定时任务

    [root@localhost shells]# crontab -e
    no crontab for root - using an empty one
    crontab: installing new crontab

  2. 定时任务的执行内容,@reboot代表重启后执行,sleep 30重启后30s后执行脚本/usr/shells/start.sh

    @reboot sleep 30; /usr/shells/start.sh

  3. 查看定时任务是否创建成功

    查看定时任务

    [root@localhost shells]# crontab -l
    @reboot sleep 30; /usr/shells/start.sh

  4. 重启

    reboot

  5. 查看文件是否被删除

    ls /usr/shells/

    输出结果 test.txt文件已经被删除

    start.sh

方式2:配置文件方式

通过配置文件去配置定时任务执行脚本

  1. 打开配置文件/etc/crontab

    有权限限制 可能需要使用sudo 去执行

    sudo vim /etc/crontab

  2. 编写需要执行的定时任务

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root

    For details see man 4 crontabs

    Example of job definition:

    .———————— minute (0 - 59)

    | .——————- hour (0 - 23)

    | | .————— day of month (1 - 31)

    | | | .———- month (1 - 12) OR jan,feb,mar,apr …

    | | | | .—— day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

    | | | | |

    * user-name command to be executed

    @reboot root sleep 30; /usr/shells/start.sh
    ~

然后和上面的操作一样,需要提前创建好被删除的文件test.txt,然后重启系统,重启后查看脚本所指定的文件是否被删除。

修改配置,/etc/rc.d/rc.local实现

上述文件的路径根据版本不同可能存在差异,路径可能为/etc/rc.local或者 /etc/rc.d/rc.local,下面我基于/etc/rc.d/rc.local进行演示。

  1. 首先需要授予该文件执行权限

    chmod +x /etc/rc.d/rc.local

  2. 打开文件

    vim /etc/rc.d/rc.local

  3. 编辑文件后保存退出

    !/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

    /usr/shells/start.sh
    exit 0

  4. 查看rc .local状态是否正常

    systemctl status rc-local
    ● rc-local.service - /etc/rc.d/rc.local Compatibility
    Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
    Active: inactive (dead)

    1. Docs: man:systemd-rc-local-generator(8)
  5. 如果状态Active: inactive (dead)括号中为dead,启动rc .local

    systemctl start rc-local.service
    systemctl enable rc-local.service

  1. 再次查看状态,此时 Active: active (exited)

注意/etc/rc.d/rc.local文件如果没有授予执行权限服务无法启动

  1. systemctl status rc-local
  2. rc-local.service - /etc/rc.d/rc.local Compatibility
  3. Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: disabled)
  4. Active: active (exited) since Thu 2021-09-09 10:17:06 CST; 25s ago
  5. Docs: man:systemd-rc-local-generator(8)
  6. Tasks: 0 (limit: 4927)
  7. Memory: 0B
  8. CGroup: /system.slice/rc-local.service
  1. 重启,查看文件是否被删除

    重启

    reboot

    查看文件是否被删除

    ls /usr/shells/

文章到这就结束了,感谢阅读。如有错误之处,感谢指正。

欢迎关注个人公众号:FluencyHome,文章同步更新

发表评论

表情:
评论列表 (有 0 条评论,415人围观)

还没有评论,来说两句吧...

相关阅读