当前位置:   article > 正文

在Ubuntu 18.04 中设置开机自动执行脚本_如何在ubuntu18上自动执行bash脚本

如何在ubuntu18上自动执行bash脚本

Ubuntu18.04 不能像16.04 那样可以直接使用 /etc/rc.local 文件,需要设置

1.建立 rc-local.service 文件

sudo vim /etc/systemd/system/rc-local.service

2.复制下列内容到 rc-local.service 文件中

  1. [Unit]
  2. Description=/etc/rc.local Compatibility
  3. ConditionPathExists=/etc/rc.local
  4. [Service]
  5. Type=forking
  6. ExecStart=/etc/rc.local start
  7. TimeoutSec=0
  8. StandardOutput=tty
  9. RemainAfterExit=yes
  10. SysVStartPriority=99
  11. [Install]
  12. WantedBy=multi-user.target

3.创建文件 rc.local

sudo vim /etc/rc.local

4.复制下列内容到 rc.local 文件中

  1. #!/bin/sh -e
  2. #
  3. # rc.local
  4. #
  5. # This script is executed at the end of each multiuser runlevel.
  6. # Make sure that the script will "exit 0" on success or any other
  7. # value on error.
  8. #
  9. # In order to enable or disable this script just change the execution
  10. # bits.
  11. #
  12. # By default this script does nothing.
  13. echo "测试脚本执行成功" > /usr/local/test.log
  14. exit 0

5.给 rc.local 加上权限,启用服务

  1. sudo chmod 755 /etc/rc.local
  2. sudo systemctl enable rc-local

6.启动服务并检查状态

  1. sudo systemctl start rc-local.service
  2. sudo systemctl status rc-local.service

7.重启后检查test.log文件是否已经存在

参考博客:https://www.jianshu.com/p/79d24b4af4e5

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55210
推荐阅读
相关标签
  

闽ICP备14008679号