在Linux中重新启动文件更改过程 [英] Restart process on file change in Linux
问题描述
是否存在一个简单的解决方案(通过大多数发行版提供的util使用常见的shell utils,或一些简单的python/...脚本)在某些文件更改时重新启动进程?
Is there a simple solution (using common shell utils, via a util provided by most distributions, or some simple python/... script) to restart a process when some files change?
像watch -cmd "./the_process -arg" deps/*
那样简单地调用sth会很好.
It would be nice to simply call sth like watch -cmd "./the_process -arg" deps/*
.
更新: 一个简单的shell脚本以及建议的inotify-tools(不错!)可以满足我的需要(适用于不带参数的命令):
Update: A simple shell script together with the proposed inotify-tools (nice!) fit my needs (works for commands w/o arguments):
#!/bin/sh
while true; do
$@ &
PID=$!
inotifywait $1
kill $PID
done
推荐答案
是的,您可以使用 inotifywait
将在检测到事件时退出.传递选项-r
来递归监视目录.例如:inotifywait -r mydirectory
.
inotifywait
will exit upon detecting an event. Pass option -r
to watch directories recursively. Example: inotifywait -r mydirectory
.
您也可以指定要监视的事件,而不是监视所有事件.要仅等待文件或目录内容更改,请使用选项-e modify
.
You can also specify the event to watch for instead of watching all events. To wait only for file or directory content changes use option -e modify
.
这篇关于在Linux中重新启动文件更改过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!