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

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

启动成功

相关推荐
Code_Dragon2 分钟前
最近遇到的bug
linux·前端
risc-v@cn4 分钟前
【在ubuntu下使用vscode打开c++的make项目及编译调试】
c++·vscode·ubuntu
武文斌776 分钟前
计算机网络:网络基础、TCP编程
linux·网络·网络协议·tcp/ip·计算机网络
秋天枫叶3524 分钟前
【AI应用】修改向量数据库Milvus默认密码
运维·服务器·数据库·ubuntu·milvus·milvus_cli
EnigmaCoder38 分钟前
【Linux】Vim编辑器:从入门到高效使用
linux·运维·编辑器·vim
王伯爵1 小时前
go语言中的select的用法和使用场景
开发语言·数据库·golang
凯子坚持 c1 小时前
Redis 数据类型:List 列表的深度解析与应用
数据库·redis·list
DarkAthena1 小时前
【GaussDB】使用gdb定位GaussDB编译package报错
数据库·gaussdb
我科绝伦(Huanhuan Zhou)1 小时前
Linux服务器性能优化总结
linux·服务器·性能优化
m0_571372821 小时前
关于linux软件编程9——网络编程1
linux·服务器·网络