Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait for file existence

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
0
0
US
Inside my perl script, I need to wait for the existence of a file before continuing. Is there a good way to do this?

Thanks in advance!
 
if it's not production level code, you can do away with this:
Code:
sleep 1 while ( !(-e "c:/tmp.txx") );

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
What does this statement do exactly:

sleep 1 while ( !(-e "c:/tmp.txx") );

I know what the "while ( !(-e "c:/tmp.txx") );" does, but what exactly does the "sleep 1" do? Is it basically:

If the file does not exist, wait a second then check again?

Thanks!!
 
yes, that is correct. which is why i would suggest not using it for a production server.

The reason for using sleep inside the while loop is because sleep suspends the process and hence there is no noticable CPU utilization until the next check.

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
133tcamel, do you know if using syscall...with a call to something like WAIT...suspends the process? I would like to "sleep" for less than 1 second, but I don't think I can do that with the sleep command.

Thanks again!!!
 
if you'd like to sleep for less than a second, try using Time::HiRes. It allows you to sleep in microseconds.

There is also something about file triggers (linux), i once browsed in a book which could be a TIMTOWTDI. But I wouldn't go that route unless really neccessary.

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Something else you can try:

Code:
while (1) {
   last if -e "C:/tmp.txt";
}

I'm not sure what effect this has on CPU usage, but it doesn't use sleeps, it just breaks free of the loop as soon as the file exists.
 
Kirsle, Yes, that takes up CPU...I KNOW because that is similar to what I originally tried, and I took down the system!?!?!?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top