侧边栏壁纸
博主头像
Monkey部落博主等级

Monkey部落,分享技术、经验、遇到的问题及解决方法,欢迎大家互相讨论分享。

  • 累计撰写 59 篇文章
  • 累计创建 36 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Ubuntu18.04配置开机自启动

Monkey部落
2022-01-01 / 0 评论 / 0 点赞 / 23 阅读 / 528 字

Ubuntu18.04配置开机自启动

步骤

  1. 在/etc/systemd/system/文件夹中新建xxx.service文件

    cd /etc/systemd/system/
    vim xxx.service
    
  2. 也可以在不同目录下新建xxx.service文件,然后通过ln -s 软连接到系统目录

    cd xxx/xxx/
    vim xxx.service
    ln -s xxx.service /etc/systemd/system/xxx.service
    
  3. 在xxx.service中输入一下内容

    [Unit]
    # 服务描述
    Description=XXX Service
    # 指定服务在哪个系统目标之后启动,例如network.target
    After=network.target
    
    [Service]
    # 服务类型,simple表示是一个简单的后台程序
    Type=simple
    # 执行之前需要进行的操作,这里使用sleep命令进行延时
    ExecStartPre=/bin/sleep 10
    # 需要执行的脚本或者命令,这里使用minio为例
    ExecStart=/xxx/xxx/start.sh
    
    [Install]
    # 指定服务所属的目标,这个设置为多用户模式
    WantedBy=multi-user.target
    
    
  4. 启动服务,使其在系统启动时自动启动

    systemctl enable xxx.service
    
  5. 如果修改了xxx.service文件,需要重新加载服务文件

    systemctl daemon-reload
    
  6. 启动服务

    systemctl start xxx.service
    
  7. 查询服务状态

    systemctl status xxx.service
    
  8. 停止服务

    systemctl stop xxx.service
    
  9. 禁用服务

    systemctl disable xxx.service
    
  10. 重新启动服务

    systemctl restart xxx.service
    
  11. 查询服务信息

    journalctl -u xxx.service
    
  12. start.sh

    #!/bin/sh
    /xxx/minio server /xxx/minio/data --console-address ":9099"
    

常见报错

  1. 自动报错Failed at step EXEC spawning xxx: Exec format error

    在start.sh脚本开头加上#!/bin/sh或者#!/bin/bash

  2. Failed to add /run/systemd/ask-password to directory watch: No space left on device?

    添加监控空间

    echo 1048576 > /proc/sys/fs/inotify/max_user_watches
    

    或者修改配置文件,使其长期有效

    vim /etc/sysctl.conf
    # 在文件末尾添加
    fs.inotify.max_user_watches=1048576
    
  3. 启动之后没有任何信息,但是服务停止

    可能是start.shs脚本中增加了nohup,写成了这样

    #!/bin/sh
    nohup /xxx/minio server /xxx/minio/data --console-address ":9099" &
    

    这样的脚本在单独执行时没有问题,但是如果是使用systemctl开机自启动,不能使用nohup,直接去掉nohup和&。写成上方步骤中的命令即可。

0

评论区