ubuntu-18.04 设置开机启动脚本

脚本设置流程及解析过程:

脚本设置流程:

ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。


建立rc-local.service文件

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

创建文件rc.local

  1. cat << RainyWang > /etc/rc.local
  2. #!/bin/sh -e
  3. #
  4. # rc.local
  5. #
  6. # This script is executed at the end of each multiuser runlevel.
  7. # Make sure that the script will "exit 0" on success or any other
  8. # value on error.
  9. #
  10. # In order to enable or disable this script just change the execution
  11. # bits.
  12. #
  13. # By default this script does nothing.
  14. /opt/mongodb/bin/mongod --config /etc/mongodb/co_adfs_primary.conf
  15. /opt/mongodb/bin/mongod --config /etc/mongodb/co_disktracker_primary.conf
  16. RainyWang

给rc.local加上权限

  1. chmod +x /etc/rc.local

启用服务

  1. systemctl enable rc-local

启动服务并检查状态

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