简体   繁体   English

Linux:在连接的 USB 串行加密狗上启动守护进程

[英]Linux: Start daemon on connected USB-serial dongle

On my Linux (Angstrom distro on BeagleBone Black) I have a USB dongle which presents as a serial port and per default is available as /dev/ttyUSB0在我的 Linux(BeagleBone Black 上的 Angstrom 发行版)上,我有一个 USB 加密狗,它显示为串行端口,默认情况下可用作/dev/ttyUSB0

I want to start a daemon, which will connect to the serial port and make it available as a socket.我想启动一个守护进程,它将连接到串行端口并使其可用作套接字。 I have the code for this USB-to-socket bridge and it works when started by hand.我有这个 USB-to-socket 桥接器的代码,它可以在手动启动时工作。

I want it to start automatically whenever the system boots, supposing the USB dongle is plugged in. How should I do this?我希望它在系统启动时自动启动,假设 USB 加密狗已插入。我该怎么做?

Attempts so far:迄今为止的尝试:

  1. systemd: I created a systemd service with conditions After: remote-fs.target and After:syslog.target , but (it seems) the USB dongle is not ready at that point and the startup of the daemon fails. systemd:我创建了一个 systemd 服务,条件为After: remote-fs.targetAfter:syslog.target ,但是(似乎)USB 加密狗在那时还没有准备好,守护进程的启动失败。

    Are there other systemd targets or services to condition to, so that the daemon is started only when the udev has finished installing devices and the network is ready?是否有其他 systemd targetsservices需要调节,以便仅当 udev 完成设备安装并且网络准备就绪时才启动守护程序?

  2. udev: I created a udev rule like udev:我创建了一个 udev 规则,例如

    KERNEL=="ttyUSB?", RUN+="/path/to/daemon.sh"内核=="ttyUSB?", RUN+="/path/to/daemon.sh"

    which executes successfully.成功执行。 But the daemon (which is started as a background process with a "&" within that script) seems not to execute.但是守护进程(在该脚本中作为带有“&”的后台进程启动)似乎没有执行。 Also it seems to be frowned upon, to fork long running processes from udev rules.从 udev 规则中分出长时间运行的进程似乎也不受欢迎。

What is the correct way to do it?正确的做法是什么?

Create a udev rule like创建一个 udev 规则,如

# cat /etc/udev/rules.d/95-serialdaemon.rules
KERNEL=="ttyUSB0", TAG+="systemd", ENV{SYSTEMD_WANTS}="serialdaemon.service"

Create a systemd service like创建一个 systemd 服务,如

# cat /lib/systemd/system/serialdaemon.service
[Unit]
Description=USB serial to socket bridge
After=remote-fs.target
After=syslog.target

[Service]
ExecStart=/mnt/serialdaemon.sh

Create the executable file创建可执行文件

# cat /mnt/serialdaemon.sh
#!/bin/sh
date +%F-%T >> /var/log/serialdaemon.log
/usr/local/serialdaemon/serialdaemon -serial /dev/ttyUSB0 -port 15789 -baud 38400 >> /var/log/serialdaemon.log 2>&1
date +%F-%T >> /var/log/serialdaemon.log

Since the link in my further comment seems to solve this problem, here is the solution for using udev for starting a daemon when a certain usb device is plugged in:由于我进一步评论中的链接似乎解决了这个问题,以下是在插入某个 USB 设备时使用 udev 启动守护程序的解决方案:

Proper(-ish) way to start long-running systemd service on udev event (device hotplug) 在 udev 事件(设备热插拔)上启动长时间运行的 systemd 服务的正确(-ish)方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM