你一定要产卵的过程,并等待所需的字。就这样。
#!/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
输出:
[email protected]:~/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