Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: running Perl scripts in the background

by BrowserUk (Patriarch)
on Jul 12, 2007 at 07:51 UTC ( [id://626163]=note: print w/replies, xml ) Need Help??


in reply to running Perl scripts in the background

If you put the list of files in a file, and read that file backwards, and truncate it after you've finished processing each file you read from it, then when you re-start the script, it will pick up the same file you were processing when you crashed.

If you truncate the file just after reading a filename, remember the position of the new EOF, then you could write your current position in the data file at that position in the list file after each read of the data file. A small tweak to check for a position when you read the list file and a you can seek to that position when you re-open it after a re-start.

Something like (untested):

use File::ReadBackwards; my $listname = shift; ## Not sure whether F::RB allows you to specify an open mode? tie *LIST, 'File::ReadBackwards', $listname or die $!; while( <LIST> ) { my( $datafile, $position ) = split ' '; my $writePos = tell LIST; truncate LIST, writePos; open DATA, '<', $datafile or die $!; seek DATA, $position, 0 if defined $position; while( <DATA> ) { seek LIST, $writePos, 0; print LIST ' ', tell( DATA ), "\n"; ## Do stuff with the line from DATA in $_ } close DATA; seek LIST, $writepos, 0; truncate LIST, $writePos; } close LIST; ## Should be empty by the time we get here. unlink $listname;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://626163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-03-29 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found