arc_of_descent has asked for the wisdom of the Perl Monks concerning the following question:


Hi

I have a process which will/should never die. It basically runs as a daemon, and sleeps for random intervals. A script run thru crontab sends this process a SIGHUP every 5 mins, and my process does the necessary stuff.

It is imperative that this process does not die, as it holds important data in memory. To elaborate more, it basically polls SNMP devices for byte counters and stores the values in a database. If this process dies I lose around 3 polls of data.

Now what I needed was a way to re-execute this program so that I could enter some lines of code, more debugging statements, some bug fixes, etc. If I call exec "program" - maybe on some signal - then yes.. it does include the new code, but it loses the data in memory. Is there any known way of preserving the data and reloading the program from file?

Thanks a lot!

Replies are listed 'Best First'.
Re: reloading a never-ending process
by exussum0 (Vicar) on Jun 11, 2004 at 10:35 UTC
    Why not periodically write the data out somewhere?

    Bart: God, Schmod. I want my monkey-man.

Re: reloading a never-ending process
by BUU (Prior) on Jun 11, 2004 at 10:53 UTC
    use strict; read_data_from_disk(); $SIG{HUP} = sub { write_data_to_disk(); exec $0; } while 1 #deamon
Re: reloading a never-ending process
by Joost (Canon) on Jun 11, 2004 at 10:11 UTC
    What about do or eval ?

    You will probably have to watch where you store the data you want to keep, and that the newly loaded code doesn't re-initialize the variables.

    update: to clarify:

    our $IMPORTANT_DATA; $IMPORTANT_DATA = "Initial value" unless defined $IMPORTANT_DATA;
    or something like that.

    If the data is so important, I'd keep it on disk anyway.

Re: reloading a never-ending process
by pbeckingham (Parson) on Jun 11, 2004 at 11:55 UTC

    Please note that if there should be any memory leak, you will suffer from it, and it will accumulate over time. Programs that exit won't suffer from this as much. Is there a way you can exit periodically also?

      thanks for the warning. Does the above solution where i write data to disk on a SIGHUP - then exec, should take care of the memory leak issue?

      Thanks

        I believe it does, because you run a new instance with a new pid, therefore allowing the OS to clean out the memory from the old pid, so I think so. Other monks may know better.

Re: reloading a never-ending process
by ambrus (Abbot) on Jun 11, 2004 at 15:09 UTC

    I belive that's impossible. You have to write the data somewhere before reexecing itself.

    It's just like you can't load a new OS kernel without ending the running processes (and you can't reinstall windows but save all the settings of the old one:).