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

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

启动成功

相关推荐
虹科网络安全几秒前
艾体宝新闻|Redis 月度更新速览:2026 年 3 月
数据库·redis·缓存
Nturmoils3 分钟前
DolphinDB 实时时序数据处理踩坑实录:5 类生产故障排查与优化
数据库
乌托邦的逃亡者14 分钟前
CentOS/Openeuler主机中,为一个网卡设置多个IP地址
linux·运维·网络·tcp/ip·centos
csdn2015_28 分钟前
postgresql 拼接字段
数据库
\xin34 分钟前
pikachu自编SQL(POST)
java·数据库·sql
2301_8159019744 分钟前
SQL如何将多行记录聚合成逗号分隔字符串_GROUP_CONCAT技巧
jvm·数据库·python
秋91 小时前
Kingbase下载、安装和使用详解
数据库
拾贰_C1 小时前
【OpenClaw | openai | QQ】 配置QQ qot机器人
运维·人工智能·ubuntu·面试·prompt
念恒123061 小时前
进程控制---自定义Shell
linux·c语言
zjy277771 小时前
Layui tab选项卡如何动态根据ID值进行程序化切换
jvm·数据库·python