Ubuntu自启动脚本

CGC Lv4

任务背景

任务背景为在Jetson Orin Nano上使用t265时,需要同时按序开启并执行三个不同终端
如下

1
roslaunch realsense2_camera rs_t265.launch
1
2
3
添加权限
sudo chmod 777 /dev/ttyAMA0
roslaunch mavros acfly.launch fcu_url:=/dev/ttyUSB0:57600
1
2
3
打开t265文件夹,打开终端
source devel/setup.bash
roslaunch vision_to_mavros t265_tf_to_mavros.launch

脚本的写法

使用

1
2
3
4
sh1 = "/home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_1.sh"
subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', sh1, '--hold'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# sh1为需要在该终端执行的命令

其中sh1为事先写好的bash脚本,例如

1
2
3
#!/bin/bash

roslaunch realsense2_camera rs_t265.launch

若有若干命令需要在同一终端中执行,则为

1
2
3
4
5
6
sh2s = ["echo 123456 | sudo -S chmod +x /home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_2.sh",
"sudo chmod 777 /dev/ttyUSB0",
"/home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_2.sh"]
sh2 = "bash -c '{}'".format("; ".join(sh2s))
subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', sh2, '--hold'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

需要注意的是:
在一个终端中执行的需要放在同一个中,不能使用 os.system ,使用 os.system 则会回到最初始的终端中执行
subprocess.Popen 创建的终端,若终端执行结束或出现报错,则该终端会被杀死消失,可使用 --hold 使其保持开启

所以我的最终启动脚本为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def T265_Start():
sh1 = "/home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_1.sh"
sh2s = ["echo 123456 | sudo -S chmod +x /home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_2.sh",
"sudo chmod 777 /dev/ttyUSB0",
"/home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_2.sh"]
sh2 = "bash -c '{}'".format("; ".join(sh2s))
sh3 = "/home/c/Library/Cv_for_Orinnano/utils/bash/T265_Start_3.sh"

subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', sh1, '--hold'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(5)

subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', sh2, '--hold'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(5)

subprocess.Popen(['gnome-terminal', '--', 'bash', '-c', sh3, '--hold'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(5)

自启动的设置

  • Title: Ubuntu自启动脚本
  • Author: CGC
  • Created at: 2023-05-08 18:16:30
  • Updated at: 2023-07-24 10:56:51
  • Link: https://redefine.ohevan.com/2023/05/08/Ubuntu自启动脚本/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
Ubuntu自启动脚本