当前位置: 首页 > news >正文

ubuntu设置脚本开机自启动

rc-local.service
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文件,没有手动添加,内容如下:

#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目录下,内容为你需要执行的脚本内容或者你需要执行的脚本启动命令

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脚本文件添加执行权限

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

下面是我在操作过程中的截图:
启动失败:
在这里插入图片描述
启动成功
在这里插入图片描述

http://www.lryc.cn/news/231544.html

相关文章:

  • cobol-简介
  • 使用 JMeter 分布式性能测试
  • 【工具流】WSL2安装
  • OpenGL获取GPU信息
  • 毫米波雷达模块的目标检测与跟踪
  • Linux 下 使用 Ekho 进行TTS文本转语音
  • WiFi protocol 详解
  • llm模拟基本逻辑门
  • Linux学习第42天:Linux RS232/485/GPS 驱动实验:天外来客
  • CSDN每日一题学习训练——Python版(输入起始和结束的正整数,求其两个正整数之间的偶数和、两数相加)
  • 【论文】基于Hadoop的铁路货运大数据平台设计与应用
  • GoF之代理模式
  • post 和get参数 请求
  • RabbitMQ多线程配置和异常解决办法
  • 【原创】java+swing+mysql车辆维修管理系统设计与实现
  • 无法在 DLL“SQLite.Interop.dll”中找到名为”sIb4c632894b76cc1d“
  • linux高级篇基础理论一(详细文档、Apache,网站,MySQL、MySQL备份工具)
  • 周赛370(模拟、树形DP(正难则反)、树状数组优化DP)
  • python实现一个简单的桌面倒计时小程序
  • 解决STM32F429烧录程序后还需复位才能植入程序的bug
  • 使用Golang调用摄像头
  • 【Linux网络】1分钟使用shell脚本完成DNS主从解析服务器部署(适用于centos主机)
  • 基于SSM的校园停车场管理系统设计与实现
  • 块设备 I/O 请求送达到外部设备
  • 【ArcGIS Pro二次开发】(76):面积平差工具
  • 4、智能家居框架设计和代码文件工程建立
  • 网络编程TCP/UDP
  • 移远EC600U-CN开发板 11.15
  • Docker - MySQL Database is uninitialized and password option is not specified
  • Elasticsearch 之聚合分析