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

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

启动成功

相关推荐
zzb15807 小时前
RAG from Scratch-优化-query
java·数据库·人工智能·后端·spring·mybatis
一只鹿鹿鹿7 小时前
信息安全等级保护安全建设防护解决方案(总体资料)
运维·开发语言·数据库·面试·职场和发展
堕2747 小时前
MySQL数据库《基础篇--数据库索引(2)》
数据库·mysql
wei_shuo7 小时前
数据库优化器进化论:金仓如何用智能下推把查询时间从秒级打到毫秒级
数据库·kingbase·金仓
朱包林7 小时前
Python基础
linux·开发语言·ide·python·visualstudio·github·visual studio
雷工笔记8 小时前
Navicat Premium 17 软件安装记录
数据库
wenlonglanying8 小时前
Ubuntu 系统下安装 Nginx
数据库·nginx·ubuntu
数据库小组8 小时前
10 分钟搞定!Docker 一键部署 NineData 社区版
数据库·docker·容器·database·数据库管理工具·ninedata·迁移工具
biubiubiu07068 小时前
Linux / Ubuntu systemd 服务使用说明
linux·运维·ubuntu
爬山算法8 小时前
MongoDB(38)如何使用聚合进行投影?
数据库·mongodb