I'm writing a bash script to use inotifywait
to monitor a directory and kick off actions when changes are detected. Something like:
inotifywait -m ... | while read f; do something; done
Since inotifywait
doesn't terminate by itself, this script will not halt.
So my plan was to get the PID of the inotifywait process, save it to a file and have a different process kill it later, say like:
inotifywait -m ... | { echo ??PID?? > pid-file; while ... }
But I don't know how to get the PID. Is there a simple way to achieve this? Another way is just to save the PID of the shell-script $$
to the file and kill the entire shell-script but I wanted to do some cleanup after the while loop.
I have tried using coproc
and I think it will work but it seems like more complication than necessary.
ps -ef | grep processName | grep -v grep | awk '{print $2}' | xargs kill -9
– Kiwy Dec 03 '13 at 09:13pgrep inotifywait
. That will give you the PID,to kill,pkill inotifwait
. – slm Dec 03 '13 at 12:42grep -v grep
, insteadps -ef | grep [p]rocessname...
would do the same. – slm Dec 03 '13 at 13:10inotifywait
I believe that's only a Linux technology or at the very least not present on AIX. I'm not trying to be insulting and if I did I apologize, just trying to be concise. – slm Dec 03 '13 at 15:52pgrep
won't do the job. Refer to http://askubuntu.com/questions/157075/why-does-ps-aux-grep-x-give-better-results-than-pgrep-x -pgrep
only looks at the first 15 characters and so ifinotifywait
is listed as/usr/local/bin/inotifywait
for example,pgrep
won't work while Kiwy's method will work. – Davidson Chua Dec 04 '13 at 01:06-f
switch if you need to match against more of the executables name. – slm Dec 04 '13 at 01:08pgrep -f inotifywait
? – Davidson Chua Dec 04 '13 at 01:11