在背景上运行inotifywait

use*_*982 5 bash inotify inotifywait

我从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"
Run Code Online (Sandbox Code Playgroud)

kon*_*box 5

您可以使用screen或运行脚本,nohup但我不确定这有什么用,因为脚本似乎没有将其输出记录到任何文件.

nohup bash script.sh </dev/null >/dev/null 2>&1 &
Run Code Online (Sandbox Code Playgroud)

要么

screen -dm bash script.sh </dev/null >/dev/null 2>&1 &
Run Code Online (Sandbox Code Playgroud)

否则也可能适用:

bash script.sh </dev/null >/dev/null 2>&1 & disown
Run Code Online (Sandbox Code Playgroud)

你应该测试哪一个不允许命令在终端退出时暂停或挂起.

如果要将输出记录到文件,可以尝试以下版本:

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
Run Code Online (Sandbox Code Playgroud)


Roo*_*000 5

我用它做了一个"服务".所以我可以像普通服务一样停止/启动它,并且它会在重启后启动:

这是在Centos发行版上制作的,所以如果它能立即在其他版本上运行,我就不会这样做.

在服务目录中创建一个执行权限为的文件

/etc/init.d/ servicename

#!/bin/bash

# chkconfig: 2345 90 60

case "$1" in
start)
   nohup SCRIPT.SH > /dev/null 2>&1 &
   echo $!>/var/run/SCRIPT.SH.pid
   ;;
stop)
   pkill -P `cat /var/run/SCRIPT.SH.pid`
   rm /var/run/SCRIPT.SH.pid
   ;;
restart)
   $0 stop
   $0 start
   ;;
status)
   if [ -e /var/run/SCRIPT.SH.pid ]; then
      echo SCRIPT.SH is running, pid=`cat /var/run/SCRIPT.SH.pid`
   else
      echo SCRIPT.SH is not running
      exit 1
   fi
   ;;
*)
   echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0
Run Code Online (Sandbox Code Playgroud)

所有内容都应该更改为脚本名称.

该行# chkconfig: 2345 90 60可以在重新引导系统时启动服务.这可能不像在发行版那样在ubuntu中工作.


Ani*_*ota 5

我发现最好的方法是创建一个 systemd 服务。

在以下位置创建 systemd 文件/lib/systemd/system/checkfile.service

sudo vim /lib/systemd/system/checkfile.service
Run Code Online (Sandbox Code Playgroud)

并将其粘贴到那里:

[Unit]
Description = Run inotifywait in backgoround

[Service]
User=ubuntu
Group=ubuntu
ExecStart=/bin/bash /path_to/script.sh
RestartSec=10

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

在 中/path_to/script.sh,你可以这样:

inotifywait -m /path-to-dir -e create -e moved_to |
    while read dir action file; do
        echo "The file '$file' appeared in directory '$dir' via '$action'" >> /dir/event.txt
    done
Run Code Online (Sandbox Code Playgroud)

确保您的文件可由用户执行:

sudo chmod +x /path_to/script.sh
Run Code Online (Sandbox Code Playgroud)

创建两个文件后,使用以下命令重新加载 systemd 管理器配置:

sudo systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

现在您可以对脚本使用启动/停止/启用:

sudo systemctl enable checkfile
sudo systemctl start checkfile
Run Code Online (Sandbox Code Playgroud)

确保在执行之前替换文件/目录/用户/组值。