View previous topic :: View next topic |
Author |
Message |
rev138 l33t
Joined: 19 Jun 2003 Posts: 848 Location: Vermont, USA
|
Posted: Sun Aug 03, 2008 3:23 am Post subject: Grabbing inotifywait output in perl? |
|
|
Invoking inotifywait with the -m flag makes it run indefinitely and print to stdout whenever an event is triggered.
I'd like to be able to invoke this from a perl script and have the script do something every time inotifywait writes to stdout, but I can't figure out how to accomplish this.
Any help is appreciated. _________________ Vermont Free PC
http://www.vtfreepc.org |
|
Back to top |
|
|
widremann Veteran
Joined: 14 Mar 2005 Posts: 1314
|
Posted: Sun Aug 03, 2008 4:07 am Post subject: |
|
|
You'll want to use the IPC::Open2 module (perldoc IPC::Open2) to open a filehandle to your inotifywait process. You can then read from it in a loop in your script. |
|
Back to top |
|
|
rev138 l33t
Joined: 19 Jun 2003 Posts: 848 Location: Vermont, USA
|
Posted: Sun Aug 03, 2008 4:37 am Post subject: |
|
|
Thanks, but I'm afraid I don't have a ton of experience with file handles in perl, and that doc talks just slightly over my head.
Could I trouble you for a short code example?
Thanks again. _________________ Vermont Free PC
http://www.vtfreepc.org |
|
Back to top |
|
|
zeek Guru
Joined: 16 Nov 2002 Posts: 480 Location: Bantayan Island
|
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21672
|
Posted: Sun Aug 03, 2008 4:47 pm Post subject: |
|
|
Using an inotify module is a better solution in this case. For the more general question, consider this trivial script that runs /usr/bin/tac to print your /etc/make.conf in reverse, and sleeps as it goes: Code: | open (CHILD, "/usr/bin/tac /etc/make.conf |") or die "Unable to open child for reading: $!";
while (<CHILD>) {
sleep 1;
print;
}
close(CHILD);
|
The key point is the presence of the pipe at the end of the open statement, which directs Perl to spawn a child process, with the child's stdout set to the write end of a pipe and the read end of the pipe stored in CHILD. If you need to both read and write the child, then a more complex construct is required. |
|
Back to top |
|
|
rev138 l33t
Joined: 19 Jun 2003 Posts: 848 Location: Vermont, USA
|
Posted: Sun Aug 03, 2008 9:00 pm Post subject: |
|
|
Thanks all! _________________ Vermont Free PC
http://www.vtfreepc.org |
|
Back to top |
|
|
|