Ubuntu 21.04 设置ai服务开机自启动的两种方法

Ubuntu 21.04 设置ai服务开机自启动的两种方法

  • [方法1 /lib/systemd/system](#方法1 /lib/systemd/system)
  • [方法2 /etc/systemd/system](#方法2 /etc/systemd/system)

方法1 /lib/systemd/system

1.1.修改/lib/systemd/system/rc-local.service,如果没有需要新建,添加[Install]的内容,文件内容为

bash 复制代码
[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
iGuessMainPID=no

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

1.2.创建自启动脚本

bash 复制代码
$ sudo vim /etc/rc.local
bash 复制代码
  1 #!/bin/sh
  2 #/ai/ai_vehicle_peccancy/start.sh
  3 cd /ai/ai_vehicle_peccancy/
  4 nohup ./ai_vehicle_peccancy >/dev/null 2>&1 &
  5 exit 0

1.3.给权限

bash 复制代码
$ sudo chmod 755 /etc/rc.local

1.4.启动服务

bash 复制代码
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

1.5.重启测试

bash 复制代码
sudo reboot

参考文章:
Ubuntu22.04开机自启脚本

方法2 /etc/systemd/system

2.1 新建ai-object.service文件,文件内容为

bash 复制代码
[Unit]
Description=demo
After=network.target nginx.service

[Service]
WorkingDirectory=/home/ubuntu/AIServer/object
Type=forking
ExecStart=/home/ubuntu/AIServer/object/start.sh
ExecStop=/home/ubuntu/AIServer/object/stop.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

其中start.sh为目标程序的启动脚本,stop.sh为目标程序的关闭脚本。

2.2 创建install_service.sh脚本,内容为

bash 复制代码
#! /bin/bash
#  program : start program
chmod a+x start.sh
chmod a+x stop.sh
chmod a+x AIServer-object
cp -n ai-object.service /etc/systemd/system
chmod a+x /etc/systemd/system/ai-object.service
systemctl enable ai-object.service
systemctl start ai-object.service
echo `systemctl status ai-object.service`

2.3 启动服务

运行install_service.sh脚本,完成服务安装和自启动配置。

bash 复制代码
$ ./install_service.sh

注意:install_service.sh脚本只需要在安装时执行一次即可,后续服务器开机后会自动启动AI服务。

启动成功时控制台会输出如下信息:

bash 复制代码
	● ai-objtect.service - demo
 	Loaded: loaded (/etc/systemd/system/ai-objtect.service; enabled; vendor preset: enabled)
 	Active: active (running) since Fri 2022-08-19 10:43:39 CST; 4min 39s ago
	Process: 11563 ExecStart=/home/ubuntu/AIServer/object/start.sh (code=exited, status=0/SUCCESS)
Main PID: 11573 (AIServer)
  	Tasks: 21 (limit: 18744)
	 Memory: 10.6M
 	CGroup: /system.slice/ai-objtect.service
         		└─11573 ./AIServer

8月 19 10:43:39 ubuntu-08 systemd[1]: Starting demo...
8月 19 10:43:39 ubuntu-08 start.sh[11563]: [2022-08-19 10:43:39][INFO]  AIServer start
8月 19 10:43:39 ubuntu-08 systemd[1]: Started demo.

参考文章:
Linux下使用systemctl 开机自动启动项目

相关推荐
不爱学英文的码字机器1 小时前
[操作系统] 进程间通信:system V共享内存
linux·服务器·ubuntu
‍。。。1 小时前
Ubuntu 24.04 中文输入法安装
linux·运维·ubuntu
JHC0000002 小时前
ubuntu 下调用系统麦克风,以及faster-whisper-medium 处理音频转写文本
ubuntu·whisper·音视频
爱吃猫的鱼ouou2 小时前
(八)lerobot开源项目扩展so100的仿真操控(操作记录)
人工智能·ubuntu·机器人·mujoco
曼岛_6 小时前
【零基础实战】Ubuntu搭建DVWA漏洞靶场全流程详解(附渗透测试示例)
ubuntu·网络安全·黑客
worthsen7 小时前
ubuntu自动更新--unattended-upgrades
ubuntu
浪淘沙jkp11 小时前
大模型学习七:‌小米8闲置,直接安装ubuntu,并安装VNC远程连接手机,使劲造
服务器·学习·ubuntu·deepseek
白杨Cc11 小时前
解决华硕主板Z890m下载ubuntu20.04后没有以太网问题
ubuntu
babytiger15 小时前
在 VMware 中为 Ubuntu 24.04 虚拟机设置共享文件夹后,在虚拟机中未能看到共享的内容
linux·运维·ubuntu
浪淘沙jkp16 小时前
大模型学习六:‌小米8闲置,通过Termux安装ubuntu做个随身服务器,纯粹碍眼折腾
学习·ubuntu·termux