小编eyb*_*yid的帖子

拔掉网络时退出脚本

我正在尝试使用 Raspberry Pi 通过 eth0 记录来自传感器的数据。当我运行我的代码(以及 sigterms、kills、exit 等大约 100 个变体)时,集合在拔掉插头后不会消失,并且我的文件不会刷新。这是我的(大部分)代码的当前迭代。

blink是一个我已排除的函数,但它可以工作并且不能以编程方式交互。

有谁知道如何成功地使用断开网络电缆作为信号来刷新文件并终止我的脚本?

#!/bin/bash

# Function to check if the Ethernet interface is active
eth_active() {
    if ip link show eth0 up >/dev/null 2>&1; then
        return 0
    else
        return 1
    fi
}


dt=$(date '+%Y_%m_%d_%H_%M')
umask 0111
touch $dt.pcap

sleep 5

# Main script
if eth_active; then
# Ethernet active, execute the command
    dumpcap -i eth0 -P -w $dt.pcap &
    pid=$!
    if eth_active; then
        echo "working"
        sleep 2
    else
        kill -9 …
Run Code Online (Sandbox Code Playgroud)

shell-script raspberry-pi

6
推荐指数
1
解决办法
701
查看次数

标签 统计

raspberry-pi ×1

shell-script ×1