ubuntu18设置开机自启动

项目需求:机器人开机上电后工控机首先运行机械臂控制代码,再运行算法代码

1.终端执行以下代码

bash 复制代码
gnome-session-properties

2.设置开机自启动选项

在弹出界面点击添加,名称随便填,命令填入要启动的脚本,注释随便填。

bash 复制代码
gnome-terminal -- /home/txz/start.sh

3.开机自启动脚本start.sh

开机后首先ping机械臂的ip地址,每隔5s ping一次,能ping通后执行机械臂蜷缩操作命令,然后再打开一个终端窗口,启动算法代码。

bash 复制代码
#!/bin/bash 
echo "$(date "+%Y-%m-%d %H:%M:%S") ceshila" >> /home/txz/screen.log
while true;do
    if ping -c 1 10.7.5.18 &> /dev/null;then
	echo "$(date "+%Y-%m-%d %H:%M:%S") Ping successul. Starting Python script."
    echo "$(date "+%Y-%m-%d %H:%M:%S") Ping successful. Starting Python script." >> /home/txz/screen1.log
    python3 /home/txz/arm_server/quansuo_position.py
	sleep 3
	python3 /home/txz/arm_server/quansuo_position.py
	sleep 1
	gnome-terminal -- /home/txz/8.17/401_serch-tcp_4/1_main_frame/build/test_demo
	sleep 1
	python3 /home/txz/arm_server/server_nano.py
	break
    else
	echo "$(date "+%Y-%m-%d %H:%M:%S") Ping unsuccessful.Retrying in 5 seconds."
	echo "$(date "+%Y-%m-%d %H:%M:%S") Ping unsuccessful.Retrying in 5 seconds." >> /home/txz/screen2.log
	sleep 5
    fi
done

4.关机前操作代码stop.sh

找到开机启动的代码命令,然后kill掉,再让机械臂恢复零位

bash 复制代码
#!/bin/bash

# 查询命令的ID
command_name="python3"  # 替换为你要查询的命令名称
command_pid=$(pgrep "$command_name")

if [ -z "$command_pid" ]; then
    echo "未找到运行的命令: $command_name"
else
    echo "找到运行的命令: $command_name (PID: $command_pid)"
    # 杀死命令
    kill -9 "$command_pid"
    echo "已杀死命令: $command_name (PID: $command_pid)"
fi
python3 /home/txz/arm_server/zero_position.py
sleep 3 
python3 /home/txz/arm_server/zero_position.py
相关推荐
梦想很大很大6 小时前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair7 小时前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主7 小时前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
0xDevNull14 小时前
Linux切换JDK版本详细教程
linux
进击的丸子14 小时前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
齐生114 小时前
iOS 知识点 - IAP 是怎样的?
笔记
字节逆旅1 天前
ubuntu应用深度守护
ubuntu
tingshuo29171 天前
D006 【模板】并查集
笔记
甲鱼9291 天前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
Johny_Zhao2 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw