Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

(tye)Re: Restarting script without losing handles

by tye (Sage)
on Aug 21, 2002 at 19:33 UTC ( [id://191850]=note: print w/replies, xml ) Need Help??


in reply to Restarting script without losing handles

Yes, by default, exec closes all of your file handles except for STDIN, STDOUT, and STDERR. See $^F in perlvar. But that will probably only be part of the solution. You may also have to turn off the close-on-exec flag on some file handles directly. See F_GETFD and F_SETFD in Fcntl.

The file descriptors are what are not closed so you have to do the equivalent of fdopen() to get Perl file handles reassociated with them:     open( FILE, ">&=$fd" )  or  die ... where $fd is 0 for STDIN, 1 for STDOUT, 2 for STDERR (and Perl already reopened those for you) and you have to pass the new instance of the script the values for the file descriptors you want to reopen, for example:     exec( $^X, $0, fileno(SOCK), fileno(LOG) ); and

open( SOCK, "<&=$ARGV[0]" ) or die ... open( LOG, ">>&=$ARGV[1]" ) or die ...
Update: Restarting a long-running process from time to time can be very useful (reduces memory footprint, clears likely subtle internal corruptions due to low-profile bugs, etc.). And if you go with catching signals and not restarting the process, then be sure to use Perl v5.8 or later.

        - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found