The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
gzip a file and append creation date stamp to file jacktravine SUN Solaris 3 09-22-2009 10:09 AM
file generation and mail notification anzie.sharma Shell Programming and Scripting 4 08-26-2008 04:58 AM
mail notification on file generation anzie.sharma UNIX for Dummies Questions & Answers 2 08-25-2008 08:34 AM
FTP and LOg file creation agarwalniru Shell Programming and Scripting 2 03-12-2008 11:10 AM
csv file creation Student37 UNIX for Dummies Questions & Answers 2 02-25-2006 02:38 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
echchatbi echchatbi is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
File Creation notification

Hello,

Please help about writing a process that will be automatically notified by file creation in a predefined directory. Some code will appreciated.

Thanks for all.
  #2 (permalink)  
Old 1 Week Ago
redhead's Avatar
redhead redhead is offline
Registered User
  
 

Join Date: Feb 2002
Location: Denmark
Posts: 66
Code:
#!/bin/sh

while [ 1 ]; do                        # Run for as long as nessesary
  if [ -f /some/where/a/file ]; then   # If filename exists and has the apropriate name
    echo "File is found"               # Notify and stop monitoring
    exit 0
  fi
  sleep 60                             # Else wait a minut and see again
done
  #3 (permalink)  
Old 1 Week Ago
TonyLawrence TonyLawrence is offline
Registered User
  
 

Join Date: Sep 2007
Location: SE Mass
Posts: 133
The brute force methods will work, but many systems today provide better ways to watch for file changes. I've listed some ideas at Monitoring file or directory changes
  #4 (permalink)  
Old 1 Week Ago
echchatbi echchatbi is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
Hello and thank you for these answers.

To clarify a little more the subject, the problem isn't to write a program which wakes up every n seconds to analyze if there is something under a directory. But to make a subscription to be notified by an event and to fall asleep until the arrival of the asynchronous event like "FILE ADDED".
  #5 (permalink)  
Old 1 Week Ago
redhead's Avatar
redhead redhead is offline
Registered User
  
 

Join Date: Feb 2002
Location: Denmark
Posts: 66
Are you talking about writing a program to monitor a FIFO pipe so the program will sleep untill another program activates the pipe then the first program will wake up and do it's bit ?

Here's a small C section to do that
Code:
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(void)
{
  FILE* fp;
  time_t t;
  const char * file = "my_fifo_fil";
  if (mkfifo(file, 0644) == -1)
    {
      printf("Error initializing fifo    %s\n", file);
      return -1;
    }
  if(!(fp = fopen(file, "w")))
    {
      printf("Error opening fifo %s\n", file);
      return -1;
    }
  t = time(NULL);
  if(fprintf(fp,"Program Woken at: %s\n", ctime(&t)) < 0)
    {
      printf("Error printing to fifo %s\n", file);
      return -1;
    }
  printf("You woke me poking my fifo %s\n", file);
  if(close(fp) == -1)
    {
      printf("Error closing fifo %s\n", file);
      return -1;
    }
  if(unlink(file) == -1)
    {
      printf("Error unlinking fifo %s\n", file);
      return -1;
    }
  return 0;
}
It is used from one terminal as:
Quote:
redhead@sindre{56} ~/Programming> ./a.out &
[1] 5035
redhead@sindre{57} ~/Programming> cat my_fifo_fil
You woke me poking my fifo my_fifo_fil
Program Woken at: Tue Nov 10 17:38:40 2009
[1] Done ./a.out

Last edited by redhead; 1 Week Ago at 12:39 PM..
Sponsored Links
Reply

Bookmarks

Tags
filenotification, signal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0