ubuntu设置脚本开机自启动

rc-local.service
sql 复制代码
flexmi@td1:~$ cd /lib/systemd/system/
flexmi@td1:/lib/systemd/system$ ls |grep rc-local.service
rc-local.service
rc-local.service.d
flexmi@td1:/lib/systemd/system$ pwd
/lib/systemd/system
flexmi@td1:/lib/systemd/system$

确保有rc-local.service文件,没有手动添加,内容如下:

sql 复制代码
#flexmi@td1:/lib/systemd/system$ cat ./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
rc.local

找到rc.local文件,没有就创建在/etc目录下,内容为你需要执行的脚本内容或者你需要执行的脚本启动命令

sql 复制代码
flexmi@td1:/lib/systemd/system$ cd /etc
flexmi@td1:/etc$ ls |grep rc.local
rc.local
flexmi@td1:/etc$ cat ./rc.local
#!/bin/bash
cd /home/flexmi/cmss
sudo ./start.sh
exit 0
flexmi@td1:/etc$

注意要给rc.local脚本文件添加执行权限

sql 复制代码
sudo chmod +x ./rc.local
启动服务
复制代码
sudo systemctl enable rc-local

sudo systemctl start rc-local.service

sudo systemctl status rc-local.service
注意
  1. 如果服务启动失败,看看是不是rc.local的问题,脚本的开头一定要是 #!/bin/bash
  2. rc.local脚本的后面一定要加上exit 0

下面是我在操作过程中的截图:
启动失败:

启动成功

相关推荐
太理摆烂哥4 分钟前
MySQL库的操作
数据库·mysql
Dreamboat-L5 分钟前
日志级别基础 + Spring Boot 日志实践指南
java·服务器·数据库·java-ee·springboot
野生数据人19 分钟前
ES DSL 转 SQL 速查表:20 条子句对照 + 一条能直接跑的改写示例
大数据·数据库
GeW19 分钟前
新质生产力路线图Linux和数据库自主可控实践指南
linux
yunwei3719 分钟前
eBPF 教程:BPF 迭代器用于内核数据导出
linux·后端·性能优化
野生数据人19 分钟前
采集链路改投 Doris 速查手册:三条路径对照、output 配置项清单、报错处理表
大数据·数据库
救救孩子把32 分钟前
AI面试八股文-07_6-向量数据库索引详解
数据库·人工智能·面试·向量数据库·hnsw
别动我齐刘海1 小时前
实时控制学习2
linux·c++·人工智能·学习·算法
行者-全栈开发1 小时前
【容器/虚拟化】CVE-2026-53359:Linux KVM Januscape 虚拟机逃逸漏洞修复指南(潜伏16年的云安全噩梦)
linux·运维·服务器·多租户隔离·cve-2026-53359·kvm虚拟机逃逸·shadow mmu
专注学习1 小时前
《深入理解Linux进程与内存》第 3 章 · 进程与线程的对比 · 重点总结
linux