您只需要生成该过程并等待所需的单词。就这样。
#!/usr/bin/expect
# Monitoring '/tmp/' directory
set watchRootDir "/tmp/"
# And, monitoring of folder named 'demo'
set watchFolder "demo"
puts "Monitoring root directory : '$watchRootDir'"
puts "Monitoring for folder : '$watchFolder'"
spawn inotifywait -m -r -e create,delete /tmp
expect {
timeout {puts "I'm waiting ...";exp_continue}
"/tmp/ CREATE,ISDIR $watchFolder" {
puts "Folder created"
# run active.sh here ...
exp_continue
}
"/tmp/ DELETE,ISDIR $watchFolder" {
puts "Folder deleted"
# run inactive.sh here ...
}
}
# Sending 'Ctrl+C' to the program, so that it can quit
# gracefully.
send "\003"
expect eof
输出 :
dinesh@myPc:~/stackoverflow$ ./Jason
Monitoring root directory : '/tmp/'
Monitoring for folder : 'demo'
spawn inotifywait -m -r -e create,delete /tmp
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
I'm waiting ...
I'm waiting ...
/tmp/ CREATE,ISDIR demo
Folder created
I'm waiting ...
/tmp/ DELETE,ISDIR demo
Folder deleted
在生成时inotifywait
,我添加了更多选项。该-m
标志用于持续监控,因为默认情况下inotifywait
将在第一个事件时退出,并且-r
意味着递归或检查子目录。
我们需要指定-e
标志以及我们想要通知的事件列表。所以,在这里,我们要监控文件夹的create
和delete
事件。
参考:inotifywait