systemd 服务在重新启动时不会触发 tmux 命令

systemd 服务在重新启动时不会触发 tmux 命令

我正在使用基于linux-mainline内核的 Yocto 映像。我有systemd我创建的嵌入式操作系统。

目的

我有一个连接到主板的 UMTS 加密狗,它将用于usb_modeswitch识别加密狗并wvdial连接到 3G 基础设施。

方法

我写了一个简单的bash脚本/usr/umts.sh如下

#!/bin/bash

sleep 1;
usb_modeswitch --default-vendor 12d1 --default-product 1446 -J

sleep 1;
/usr/bin/tmux new-session -d -s Cloud
/usr/bin/tmux set-option set-remain-on-exit on
/usr/bin/tmux new-window -d -n 'wvdial' -t Cloud:1 'sleep 1; /usr/bin/wvdialconf; /usr/bin/wvdial';

usb_modeswitch将配置 USB 加密狗,下一部分将在其中创建tmux会话和触发器wvdial

我的systemd脚本如下所示/etc/systemd/system/enable-umts.service

GNU nano 2.2.5 文件:/etc/systemd/system/enable-umts.service

[Unit]
Description=Enable UMTS Dongle for Cloud Connectivity

[Service]
Type=oneshot
ExecStart=/usr/umts.sh

[Install]
WantedBy=default.target

(我已使用 授予脚本执行权chmod +x /usr/umts.sh

我重新加载了守护进程systemctl daemon-reload并启用了该服务systemctl enable enable-umts.service

并重新启动主板(注意:我只有root在主板上,没有其他user

事实上,从上面的脚本中dmesg触发了,我看到了相关消息usb_modeswitch

[  OK  ] Started Enable UMTS Dongle for Cloud Connectivity.
[   13.051247] scsi host2: usb-storage 1-1:1.5
[   13.067326] usb-storage 1-1:1.6: USB Mass Storage device detected
[   13.074645] scsi host3: usb-storage 1-1:1.6
[   13.158627] usbcore: registered new interface driver usbserial
[   13.165501] usbcore: registered new interface driver usbserial_generic
[   13.174754] usbserial: USB Serial support registered for generic
[   13.202356] usbcore: registered new interface driver option
[   13.208714] usbserial: USB Serial support registered for GSM modem (1-port)
[   13.216468] option 1-1:1.0: GSM modem (1-port) converter detected
[   13.229840] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
[   13.238774] option 1-1:1.3: GSM modem (1-port) converter detected
[   13.248906] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
[   13.256172] option 1-1:1.4: GSM modem (1-port) converter detected
[   13.264467] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB2
[   14.069960] scsi 2:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
[   14.088684] scsi 3:0:0:0: Direct-Access     HUAWEI   TF CARD Storage       PQ: 0 ANSI: 2
[   14.127686] sd 3:0:0:0: [sda] Attached SCSI removable disk
GSM modem (1-port) converter detected
] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
[   13.238774] option 1-1:1.3: GSM modem (1-port) converter detected
[   13.248906] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
[   13.256172] option 1-1:1.4: GSM modem (1-port) coPassword

但是登录后我尝试列出会话tmux ls,它表明没有创建任何会话。

`脚本实际上并没有失败,服务状态如下:

● enable-umts.service - Enable UMTS Dongle for Cloud Connectivity
   Loaded: loaded (/etc/systemd/system/enable-umts.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2018-05-17 12:52:10 UTC; 4min 51s ago
  Process: 214 ExecStart=/usr/umts.sh (code=exited, status=0/SUCCESS)
 Main PID: 214 (code=exited, status=0/SUCCESS)

May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Set up interface 0
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Use endpoint 0x01 for message sending ...
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Trying to send message 1 to endpoint 0x01 ...
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  OK, message successfully sent
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Reset response endpoint 0x81
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Reset message endpoint 0x01
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  Could not reset endpoint (probably harmless): -99
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  Device is gone, skip any further commands
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: -> Run lsusb to note any changes. Bye!
May 17 12:52:10 phyboard-mira-imx6-3 systemd[1]: Started Enable UMTS Dongle for Cloud Connectivity.

如果将脚本/usr/umts.sh作为独立脚本执行,那么实际上它会执行它应该执行的所有操作(打开一个新tmux会话并触发wvdial

这里有什么问题吗?我尝试增加更多sleep时间,但这也不起作用。遗憾的是,我正在处理的 yocto 图像没有rc.local或可用。cron

答案1

RemainAfterExit=yes|true在我的systemd脚本中添加如下:

[Unit]
Description=Enable UMTS Dongle for Cloud Connectivity

[Service]
Type=oneshot
ExecStart=/usr/umts.sh
RemainAfterExit=true
[Install]
WantedBy=default.target

添加了脚本的完整路径,umts.sh如下所示:

#!/bin/bash

    sleep 1;
    /usr/bin/tmux new-session -d -s Cloud
    /usr/bin/tmux set-option set-remain-on-exit on
    /usr/bin/tmux new-window -d -n 'usb_modeswitch' -t Cloud:2 '/usr/sbin/usb_modeswitch --default-vendor 12d1 --default-product 1446 -J';

    /usr/bin/tmux new-window -d -n 'wvdial' -t Cloud:1 'sleep 10; /usr/bin/wvdialconf; /usr/bin/wvdial';

重新加载守护进程

systemctl daemon-reload

并重新启动主板。并发现tmux会话创建并运行完美

相关内容