bash 在后台运行 inotifywait

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18901622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:38:05  来源:igfitidea点击:

run inotifywait on background

bashinotifyinotifywait

提问by user1396982

I have this code copied from linuxaria.com as example and work just fine on my case the problem is when I exit from terminal inotifywait stop. I want run on back ground even after exit the terminal. how I can do that?

我从 linuxaria.com 复制了这段代码作为示例,并且在我的情况下工作得很好,问题是当我从终端 inotifywait stop 退出时。即使在退出终端后,我也想在后台运行。我该怎么做?

#!/bin/sh

# CONFIGURATION
DIR="/tmp"
EVENTS="create"
FIFO="/tmp/inotify2.fifo"


on_event() {
  local date=
  local time=
  local file=

  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"

采纳答案by konsolebox

You can run the script with screenor nohupbut I'm not sure how that would help since the script does not appear to log its output to any file.

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

nohup bash script.sh </dev/null >/dev/null 2>&1 &

Or

或者

screen -dm bash script.sh </dev/null >/dev/null 2>&1 &

Disown could also apply:

Disown 也可以适用:

bash script.sh </dev/null >/dev/null 2>&1 & disown

You should just test which one would not allow the command to suspend or hang up when the terminal exits.

您应该只测试哪一个在终端退出时不允许命令挂起或挂断。

If you want to log the output to a file, you can try these versions:

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

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

回答by Rooie3000

I made a 'service' out of it. So I could stop/start it like a normal service and also it would start after a reboot:

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

This was made on a Centos distro So I'm not if it works on others right away.

这是在 Centos 发行版上制作的,所以如果它立即适用于其他发行版,我不会。

Create a file with execute right on in the service directory

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

/etc/init.d/servicename

/etc/init.d/中SERVICENAME

#!/bin/bash

# chkconfig: 2345 90 60

case "" 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)
   ##代码## stop
   ##代码## 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: ##代码## {start|stop|status|restart}"
esac

exit 0

Everything in caps you should change to what your script name.

大写的所有内容都应该更改为脚本名称。

The line # chkconfig: 2345 90 60makes it possible to start the service when the system is rebooted. this probably doens't work in ubuntu like distro's.

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

回答by KK Tan

replace -m with

用 -m 替换

-d -o /dev/null

-d -o /dev/null

ie:

IE:

inotifywait -d -o /dev/null -e "$EVENTS" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' >"$DIR" > "$FIFO" & INOTIFY_PID=$!

inotifywait -d -o /dev/null -e "$EVENTS" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' >"$DIR" > "$FIFO" & INOTIFY_PID=$!

You can check the inotifywait help manual at:

您可以在以下位置查看 inotifywait 帮助手册:

https://helpmanual.io/help/inotifywait/

https://helpmanual.io/help/inotifywait/