LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 09-18-2018, 06:14 AM   #1
ubin10
LQ Newbie
 
Registered: Sep 2015
Posts: 28

Rep: Reputation: Disabled
inotifywatch usage


Hi..
I need to check a file if it is modified and will append that file into another file if so. 'inotifywatch' , should do the script, however when I tested the command :
Code:
inotifywait  -e modify  ~/test.txt --format '%w' |cat >test2.txt
then i modified test.txt from other console, seems inotify did the job, quit without any message (no error nor event report), and when i checked test2.txt, file was empty.
what i did wrong, help ?
edited : I'm using debian stretch on i386/32 bit

Thanks and regards

Last edited by ubin10; 09-18-2018 at 06:21 AM. Reason: adding information
 
Old 09-18-2018, 06:27 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Use xargs to execute the cat.
 
Old 09-18-2018, 09:29 AM   #3
ubin10
LQ Newbie
 
Registered: Sep 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Use xargs to execute the cat.
Thanks, I tested it :
Code:
inotifywait  -e modify  ~/test1.txt --format '%w''%f' | xargs cat >>test.txt
still no output on test.txt
interestingly, if I tried with 'access' event instead of modify, then it behave as what I expect (test1.txt is appended to test.txt)
Code:
inotifywait  -e modify  ~/test1.txt --format '%w''%f' | xargs cat >>test.txt
The problem is I only want to change test.txt when test1 is written/modified.
Is 'modify' event not implemented on inotifywait debian ?
 
Old 09-18-2018, 11:12 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
It depends on how test1.txt is written/modified. Here are all the events when you run the following command.

echo "hello" >> test1.txt

Quote:
/tmp/test.txt OPEN
/tmp/test.txt MODIFY
/tmp/test.txt CLOSE_WRITE,CLOSE
But if you modify test1.txt using a text editor the modify event is not triggered but instead you get these.
Quote:
/tmp/test1.txt OPEN
/tmp/test1.txt ACCESS
/tmp/test1.txt CLOSE_NOWRITE,CLOSE
/tmp/test1.txt OPEN
/tmp/test1.txt CLOSE_WRITE,CLOSE
/tmp/test1.txt ATTRIB
The events above were from the command
Quote:
inotifywait -m /tmp/test1.txt

Last edited by michaelk; 09-18-2018 at 11:15 AM.
 
Old 09-18-2018, 12:13 PM   #5
ubin10
LQ Newbie
 
Registered: Sep 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
It depends on how test1.txt is written/modified. Here are all the events when you run the following command.
Ouch, well noticed. It definitely work using echo and cat,thanks
I plan to use php/web interface to modify the file, not sure if it will trigger event ?

Thank
 
Old 09-18-2018, 01:25 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
If you are using a web php script to modify the file, what is the purpose of using inotifywait?

Yes, you should get a modify event if use standard PHP file commands to open,write,close.
 
Old 09-18-2018, 01:58 PM   #7
ubin10
LQ Newbie
 
Registered: Sep 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
Hi michaelk..php to write a single configuration file to make it user friendly, and inotify will further parsing it to triger script to restart services if the file is modified

regards
 
Old 09-25-2018, 01:29 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
How about using entr instead of inotifywatch?

Code:
echo ~/test.txt | entr -s 'cat ~/test.txt >> test2.txt'

Last edited by dugan; 09-25-2018 at 01:37 PM.
 
2 members found this post helpful.
Old 09-25-2018, 05:00 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
dugan, you find (and expose) some wonderful nuggets ...
 
1 members found this post helpful.
Old 09-25-2018, 07:01 PM   #10
ubin10
LQ Newbie
 
Registered: Sep 2015
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
How about using entr instead of inotifywatch?

Code:
echo ~/test.txt | entr -s 'cat ~/test.txt >> test2.txt'
Perfect ! Thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for CPU usage, memory usage, disk partition space and service status reetesh.amity Linux - Server 6 10-12-2015 07:51 PM
Squid 3 near 100% cpu usage and high RAM usage piman Linux - Software 1 11-16-2013 02:20 AM
X hangs after prolonged usage ... memory usage goes upto 195MB duryodhan Slackware 18 05-10-2007 06:16 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration