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

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

启动成功

相关推荐
yuanlaile9 分钟前
Go全栈_Golang、Gin实战、Gorm实战、Go_Socket、Redis、Elasticsearch、微服务、K8s、RabbitMQ全家桶
linux·redis·golang·k8s·rabbitmq·gin
程序员JerrySUN14 分钟前
驱动开发硬核特训 · Day 22(上篇): 电源管理体系完整梳理:I2C、Regulator、PMIC与Power-Domain框架
linux·驱动开发·嵌入式硬件
IT运维爱好者25 分钟前
Ubuntu 22.04.4操作系统初始化详细配置
linux·运维·服务器·ubuntu
樂50235 分钟前
关于 Web 服务器的五个案例
linux·服务器·经验分享
用户217516114381 小时前
【linux】重定向与缓冲区
linux
一键三联啊1 小时前
【FastJSON】的parse与parseObject
linux·前端·python
Databend1 小时前
大数据是不是凉了?
数据库
学也不会1 小时前
雪花算法
java·数据库·oracle
离凌寒1 小时前
一、linux系统启动过程操作记录
linux
qq_543248521 小时前
正则表达式三剑客之——grep和sed
linux·运维·正则表达式