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

Hi

I am using the following command to restart my perl script, once its complete(to free the memory),

exec( $^X, $0, $arg)

With this I am able to restart the script, but when I checked the process id with $$, it always gives me the same value. As per my understanding, I should get a different id each time as the process has been restarted.Is my understanding correct.

Kindly let me know your inputs.

Thanks
Manu

Replies are listed 'Best First'.
Re: Restarting perl script by itself
by ChuckularOne (Prior) on Sep 16, 2011 at 17:48 UTC

    I have recently done something similar, but I decided to let cron do the killing.

    My process starts by creating a kill script and then cron executes it once a day and then spawns a new instance of the main script which creates a new kill script and it gets a little like groundhog day.

    ### Create the kill script for cron ### my $killPid = $$; bifFile::writeToFile($killscript, "kill $killPid"); my $mode = '0755'; chmod oct($mode), $killscript;
    $killscript is the path and name of the script (a ksh script in this case) and bifFile is a perl module full of silly little utilities I use all the time. writeToFile uses two parameters, filename and text to put in the file.
Re: Restarting perl script by itself
by kennethk (Abbot) on Sep 16, 2011 at 17:06 UTC
Re: Restarting perl script by itself
by Anonymous Monk on Sep 16, 2011 at 15:54 UTC

    You're not forking a new process, running something in there, and then killing your current process. You're just doing a full-brain transplant on yourself.

      Thanks.
      As per my understanding, exec will launch the new process, which should have a different process id than the parent process and parent process will not wait for the child process to complete.

      And, when the script complete, the parent process will be automatically killed.

      Let me know, what I am doing wrong.
        As per my understanding, exec will launch the new process

        No, exec replaces your current process.... a subtle difference.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

        exec replaces one process with another one, keeping the PID, file descriptors, environment and so on. No single command launches a child process, keeping the parent process paralysed, and finally kills the helpless parent. Simply because that would be very inefficient.

        You should really RTFM, especially fork, exec, and system. Also read wait and waitpid.

        Understanding the concept of fork and exec is essential for writing software on Unix-like systems.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)