Find the answer to your Linux question:
Results 1 to 10 of 10
hi.. how can I write a /bin/bash script to check if a file is being read by the system? say.. I have a MP3 music file, how can I check ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2006
    Posts
    8

    how to check if a file is being read?


    hi.. how can I write a /bin/bash script to check if a file is being read by the system? say.. I have a MP3 music file, how can I check its status if it is being played or not?

  2. #2
    Linux User
    Join Date
    Jan 2006
    Posts
    251
    hi,

    execute
    find -atime n
    to know if the file is accessed n days ago
    Also the command ls has an option for access time(option might be u)
    cheers

  3. #3
    Just Joined!
    Join Date
    Apr 2006
    Posts
    8
    thanx nature.. but i want to know if it is 'currently' accessed or not.. can u give me the script? srry i am just newbie to .nix..

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    lsof lists open files, so I think you could probably use that to your advantage.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  6. #5
    Linux Newbie
    Join Date
    Dec 2005
    Posts
    104
    you could use
    Code:
    FILE_NAME="music.mp3"
    FILE_OPEN=`lsof | grep $FILE_NAME`
     
    if [ -z $FILE_NAME ] ;then
        echo "File NOT open"
    else
        echo "File Open"
    end
    That sould work, providing i have the right option for the if statment.
    Sorry im at school and cant check it.
    Cheers,
    Nathan

  7. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I think moonman means:
    Code:
    FILE_NAME="music.mp3"
    FILE_OPEN=`lsof | grep $FILE_NAME`
     
    if [ -z $FILE_OPEN ] ;then
        echo "File NOT open"
    else
        echo "File Open"
    fi
    And yes, presumably that should work.

  8. #7
    Linux User benjamin20's Avatar
    Join Date
    Oct 2004
    Location
    jacksonville beach FL
    Posts
    317
    i dont seem to have lsof. is that a standard unix program?
    nVidia G-Force 6600GT (bfg) pci-e: amd 64 2000+ (939): 1024 corsair ram: 2X 80gb seagate harddisk SATA: plextor cd/dvd-read/write cdrom SATA

  9. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Evidentally not: it's not installed on my home computer (I tried it out at work). At least one repo for it is at:

    ftp://vic.cc.purdue.edu/pub/tools/unix/lsof/

    Cheers!

  10. #9
    Just Joined!
    Join Date
    Jul 2006
    Posts
    5
    I like the solutions presented here. Can you think of a similar way of doing the same thing programatically by making direct C calls (I don't mean to use a system or exec call using "lsof", but using whatever calls lsof may be using to get this information).

  11. #10
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    I assume you can; just look through the lsof source and see how they do it. It probably deals with /proc somehow...
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

Posting Permissions

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