Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Someone is uploading a huge file to my server (by request) and i want to monitor the file. but the only thing i want to monitor is the file size ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2004
    Posts
    3

    File change monitoring


    Someone is uploading a huge file to my server (by request) and i want to monitor the file. but the only thing i want to monitor is the file size changeing, and i want to see it realtime, so if it changes, it changes the number, i dont want to keep typing ls -l to see the size. So is there anyway I can do this?

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    How about this:
    Code:
    while true; do clear; ls -l /path/to/the/file; sleep 1; done

  3. #3
    Just Joined!
    Join Date
    Jul 2004
    Posts
    3
    Woah, thats one freaking fast reply, like 10 seconds?, well it works, thanks a lot, *bookmarks forum*

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Posts
    3

    How do i check a file for change

    Hi,
    My requirement is as follows:
    A certain process "x" writes some data to a file.
    I have read permissions to that file.
    my process needs to know whenever "x" writes to the file so that i can fetch information from it.

    Is there any way to do it?

  5. #5
    Just Joined! dragonauta's Avatar
    Join Date
    Aug 2006
    Location
    right here
    Posts
    13
    Like said Dolda2000, you can use a script like that, just a little adjustment:
    Code:
    while true; do clear; ls -t /path/to/the/file; sleep 1; done
    the -t modifier brings the most recently modified first.


  6. #6
    Just Joined!
    Join Date
    Nov 2006
    Posts
    3
    hI..
    THANKS FOR THE SPEEDY REPLY...
    but i want a method ..where i can be notified that the file is changed..i am using a select call to monitor some events..and i need to monitor if the file is updated by the other process..?
    is there some way to do this

  7. #7
    Linux Newbie burntfuse's Avatar
    Join Date
    Nov 2006
    Location
    Laurel, MD
    Posts
    158
    I think there's a feature in the 2.6 kernel which lets you select a file for monitoring, and it'll send a message through a socket or something when the file changes (indexing daemons use this). Sorry I don't know any details, but I thought a little info would be better than nothing.
    I have sold my soul to the penguin

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Indeed. Assuming that this is a C program, you can use inotify to be informed whenever a file is changed.

    A guide on inotify:
    http://www.linuxjournal.com/article/8478

    EDIT:
    I can't seem to find the glibc 2.4 documentation online anywhere, so the best documentation may just be 'man inotify'.

  9. #9
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    i dont want to keep typing ls -l to see the size. So is there anyway I can do this?
    You can also do this using the watch program. (Should work on most GNU/Linux systems.)

    Code:
    watch -n 2 ls -l file-here
    The -n option allows you to set the seconds interval when it refreshes.

    (No comment on the question from the c programmer.)

  10. #10
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I hacked up this example for you, as well:
    Code:
    #include <stdio.h>			// For 'puts'
    #include <stdlib.h>			// For 'exit'
    #include <unistd.h>			// For 'read'
    #include <sys/inotify.h>	// For the inotify functions
    
    // This is the size of a given inotify_event
    #define EVENT_SIZE (sizeof(struct inotify_event))	
    #define BUF_LEN (1024 * (EVENT_SIZE + 16))	
    
    // The approximate size of 1024 events.  The '+ 16' is for the (optional)
    // name field.  Because this field can vary, we make no promises that this
    // number is exact.
    
    int main(int argc, char *argv[])
    {
    	if(argc == 1)
    		exit(1);
    
    	char *file = argv[1];
    
    	// Initialize our file descriptor
    	int fd = inotify_init();
    
    	// Obtain the watch descriptor.  This value doesn't need to be stored,
    	// but you need it if you want to use inotify_rm_watch() or if you
    	// have multiple watches.
    	//
    	// Note that we are requesting two events: IN_ACCESS and IN_ATTRIB.
    	// From the man page for inotify, we learn that:
    	// IN_ACCESS - The file was accessed for reading
    	// IN_ATTRIB - The file's attributes (permissions, etc.) were changed
    	int wd = inotify_add_watch(fd, file, IN_ACCESS | IN_ATTRIB);
    
    	char buf[BUF_LEN];
    
    	int len, i = 0;
    	
    	// Read in some events.  Note that this will block if there are no events.
    	// len indicates the total size of events that are taken in.
    	len = read(fd, buf, BUF_LEN);
    
    	// As long as we have events to process, loop
    	while(i < len)
    	{
    		struct inotify_event *e;
    
    		// Obtain an event from our buffer
    		e = (struct inotify_event *) &buf[i];
    
    		// Determine what event actually happened (since we're looking for 2)
    		// and output the appropriate message.
    		switch(e->mask)
    		{
    			case IN_ACCESS:
    				puts("File was accessed!");
    				break;
    			case IN_ATTRIB:
    				puts("File's attributes were changed!");
    				break;
    			default:
    				puts("ERROR");
    				break;
    		}
    
    		// Increase i by the size of the event + the size of the optional len field.
    		i += EVENT_SIZE + e->len;
    	}
    
    	// Remove our watch from the fd that we have.
    	inotify_rm_watch(fd, wd);
    
    	// Close the fd.  When all fd's from a particular inotify instance are
    	// closed, the resources are automatically freed by the kernel.
    	close(fd);
    
    	return 0;
    }

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •