• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

using inotify-tools to trigger events

inotify-tools

What are Inotify-tools?

Inotify-tools is a C libary that interface with the inotify kernel function. Inotify-tools are available on kernels later than 2.6.13.  These tools can monitor or act based on file system events, this is particularly useful for scripting out actions you would like to happen if an event takes place.

Install on Fedora / CentOS / RedHat:

Install the epel repo:

yum install epel-release

Install inotify tools:

yum install inotify-tools

Install on Debian / Ubuntu:

apt-get install inotify-tools

After installing you have access to inotifywait

Inotifywait this watches for inotify events, it can watch a specific, directory, or a recursive directory tree. By default inotifywait checks for all events to the monitored file or directory

The Inotify sytanx is:

inotify filename
Inotify example:
 [root@server ~]$ inotifywait /test
 Setting up watches.
 Watches established.
 /tmp/ MODIFY test

List of Inotifywait events you can watch for:

access – A file being monitored was ready from.
modify – A file being monitored was written to.
attrib – An attribute (timestamps, file permissions, attributes) of a monitored file were changed
close_write –  A file being monitored was closed after being in a writable state
close – A file being monitored was closed
open – A file being monitored was opened
moved_to –  A file was moved into a monitored directory
moved_from – A file or directory was moved
delete – A file or directory within a watched directory was deleted
delete_self  – A monitored file or directory was deleted, after this the file and or directory is no longer being monitored.
umount – the file sytem was unmounted that the monitored file/folder was on.

Full Example of Inotifywait:
## script to detect new files
WATCHDIR='/tmp/';
OUTDIR='/tmp-new';
inotifywait -m -q -e close_write --format %f ${WATCHDIR} | while IFS= read -r file; do
cp -p ${WATCHDIR}/"$file" ${OUTDIR}/
done

-m stands for monitor(by default inotifywait will close after the first event)
-r means recursively

A script like this, could also be placed in /etc/rc.local to start at boot.

Apr 21, 2017LinuxAdmin.io
0 0 votes
Article Rating
Journalctl Introduction and ExamplesCompiling PHP from source on CentOS
You Might Also Like
 
KVM Virsh Console Access on CentOS 7
 
How To Use Percona Xtrabackup To Create A MySQL Slave
Subscribe
Notify of
guest

guest

0 Comments
Inline Feedbacks
View all comments
8 years ago Linux Tutorials, Scripting 6,440
Recent Posts
  • Configuring Firewalld to Work Seamlessly with Docker on CentOS
  • Mastering NGINX Tuning: Optimizing Web Server Performance
  • Exploring eBPF: Revolutionizing Network and System Monitoring
  • Kubernetes: Installation and Overview on CentOS 8
  • Install Docker on Centos 8
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
76,149 views
Zend Opcache
Setup and Optimize Zend OpCache
47,105 views
Install ffmpeg
FFMpeg Install On CentOS 7
21,675 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz