ubuntu-18.04 ubuntu-16.04 设置开机启动脚本

ubuntu-18.04 设置开机启动

ubuntu-16.10 开始不再使用initd管理系统,改用systemd

执行 ls /lib/systemd/system 你可以看到有很多启动脚本,其中就有我们需要的 rc.local.service

#  SPDX-License-Identifier: LGPL-2.1+                                                                                        
#                                                                                                                            
#  This file is part of systemd.                                                                                             
#                                                                                                                            
#  systemd is free software; you can redistribute it and/or modify it                                                        
#  under the terms of the GNU Lesser General Public License as published by                                                  
#  the Free Software Foundation; either version 2.1 of the License, or                                                       
#  (at your option) any later version.                                                                                       
                                                                                                                             
# This unit gets pulled automatically into multi-user.target by                                                              
# systemd-rc-local-generator if /etc/rc.local is executable.                                                                 
[Unit]                                                                                                                       
Description=/etc/rc.local Compatibility                                                                                      
Documentation=man:systemd-rc-local-generator(8)                                                                              
ConditionFileIsExecutable=/etc/rc.local                                                                                      
After=network.target                                                                                                         
                                                                                                                             
[Service]                                                                                                                    
Type=forking                                                                                                                 
ExecStart=/etc/rc.local start                                                                                                
TimeoutSec=0                                                                                                                 
RemainAfterExit=yes                                                                                                          
GuessMainPID=no 

文件内容

一般正常的启动文件主要分成三部分

  • [Unit] 段: 启动顺序与依赖关系
  • [Service] 段: 启动行为,如何启动,启动类型
  • [Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

对于Unit、Service配置不用管,我们如果自己配置开机自启动服务时候,需要配置Install配置。

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

创建rc.local并授权

ubuntu-18.04 默认是没有 /etc/rc.local 这个文件的,需要自己创建,并且给该文件赋予可执行权限(否则文件不能执行,即开机不能运行,即开机自动启动会不成功

sudo touch /etc/rc.local
chmod 755 /etc/rc.local

编辑rc.local 并测试

添加需要开机启动的任务(这里需要注意,一定要有#!/bin/bash,即指定解释此脚本的解释器shell的路径,否则未指定解释器会执行不成功

#!/bin/bash  
echo "test rc " > /usr/test.log  

重启服务器reboot,此事后就会看到/var 目录下面就会有生成的test.log,说明服务自启动成功了,rc.local里面的命令得到执行

添加自己的服务,放到rc.local

#!/bin/bash                                                                                                                  
                                                                                                                             
/opt/zbox/zbox start                                                                                                         
                                                                                                                             
emqx start                                                                                                                   
                                                                                                                             
redis-server 

现在重启reboot,自己的服务就会子机器启动时候自动启动服务了。

参考

ubuntu-16.04 设置开机启动

ubuntu-16.04设置开机启动问题,还是蛮常规操作的,但是也会出现一些问题,比如设置无效等坑。

Ubuntu 16.04 设置 程序开机启动 用/etc/ rc.local开机启动命令/脚本的方法

网上找了好多方法都不好使,其中有几个点需要注意下就行了,这里强调几个重要步骤:

  • 用最简单方法,就在/etc/rc.local 文件中添加开机启动命令
  • /etc/rc.local 文件中的第一行 #!/bin/sh 更改为 #!/bin/bash
  • 配置程序启动命令,也配置一个测试的命令,看rc.local 是否开机启动执行命令了。
sudo /opt/zbox/zbox start

sudo /opt/emqx/bin/emqx start

echo "#define MAIN_H" >> /opt/main111.h

其中,echo “#define MAIN_H” >> /opt/main111.h 如果ubuntu重启后,/opt/main111.h 文件下有了字符串,说明rc.local 开机时执行了的。

  • 注意一个点,执行命令的时候,是需要系统权限的,命令前面一定要加sudo,实测如果不加sudo,怎么都启动不了命令。这里是个坑。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章