LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2013, 01:11 PM   #1
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Rep: Reputation: Disabled
Creating an action based event in bash


Hey,
I couldn't find an answer to my question, and I'm sure it is possible:

How can I create an action based on some event in bash:

Lets say I have some file `text.txt`,
I want to do something, like `grep` on this file
If and only if a line was added to this file,
Or the file size got bigger, or md5sum...

Thanks!
 
Old 05-29-2013, 02:31 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
W/o knowing any requirements (when, how much, how long or whatever else) I'd say that sounds like a job for Inotify.
 
Old 05-29-2013, 03:24 PM   #3
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks,

Actually, It suppose to be an action that, lets say, start "gnuplot", every time a line is added to the file `text.txt`
There is no more options given...

Does the question is clearer now ?
Does it gives more information that may offer another solution?
Or should I just use `inotify` ?


Thanks!
 
Old 05-29-2013, 04:36 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Inotify would work for that, yes. However inotify reacts immediately so unless you script carefully and check for already an running gnuplot process you might find you end up running a gazillion processes if the file gets added to continuously. This example effectively only echoes the file name:
Code:
#!/bin/sh --
LANG=C; LC_ALL=C; export LANG LC_ALL
/usr/bin/pgrep gnuplot >/dev/null 2>&1 && exit 0
/usr/bin/inotifywait -m -q -e CLOSE_WRITE /path/to/text.txt --format '%w%f'\
|while read ITEM; do echo "${ITEM}"; done; exit 0
 
Old 05-29-2013, 06:24 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
You could write a small script to track the new lines being added, based on the cmd 'tail -f test.txt' in bash.
Alternatively, Perl has a module designed for this; see definition & example code http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm
 
Old 05-30-2013, 12:46 AM   #6
hudac
LQ Newbie
 
Registered: Oct 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Great, Thanks!
 
  


Reply

Tags
bash, bash scripting, linux, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need help creating bash script that creates directories based on what you answer Profound_Darkness Linux - General 10 03-26-2013 10:34 AM
creating a text based menu structure in bash 1yuppie Programming 4 05-17-2009 10:39 AM
Event based task scheduler? beeblequix Linux - Software 1 01-30-2008 11:49 AM
Action/Event/Change handlers in Java Swing plk Programming 2 06-21-2007 04:01 AM
Perform action based on email txt plisken Linux - General 1 03-06-2007 09:49 PM


All times are GMT -5. The time now is 02:55 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration