我从 linuxaria.com 复制了这段代码作为示例,并且在我的情况下工作得很好问题是当我从终端 inotifywait 停止退出时。即使退出终端后,我也想在后台运行。我该怎么做?
#!/bin/sh
# CONFIGURATION
DIR="/tmp"
EVENTS="create"
FIFO="/tmp/inotify2.fifo"
on_event() {
local date=$1
local time=$2
local file=$3
sleep 5
echo "$date $time Fichier créé: $file"
}
# MAIN
if [ ! -e "$FIFO" ]
then
mkfifo "$FIFO"
fi
inotifywait -m -e "$EVENTS" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' "$DIR" > "$FIFO" &
INOTIFY_PID=$!
while read date time file
do
on_event $date $time $file &
done < "$FIFO"
最佳答案
您可以使用 screen
或 nohup
运行脚本,但我不确定这会有什么帮助,因为脚本似乎没有将其输出记录到任何文件。
nohup bash script.sh </dev/null >/dev/null 2>&1 &
或者
screen -dm bash script.sh </dev/null >/dev/null 2>&1 &
断绝关系也适用:
bash script.sh </dev/null >/dev/null 2>&1 & disown
您应该只测试哪个不允许命令在终端退出时挂起或挂断。
如果你想将输出记录到一个文件中,你可以试试这些版本:
nohup bash script.sh </dev/null >/path/to/logfile 2>&1 &
screen -dm bash script.sh </dev/null >/path/to/logfile 2>&1 &
bash script.sh </dev/null >/path/to/logfile 2>&1 & disown
关于bash - 在后台运行 inotifywait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901622/