Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

update a running script

by xorl (Deacon)
on Jun 26, 2013 at 15:31 UTC ( [id://1040805]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a best way of having a script check to see if a new version of itself exists and updating it while running?

I'm doing something like this:

  • use LWP::Simple to get a url.
  • slup the contents of $0
  • if contents from the url is the same as the contents from $0, proceed with the actual script.
  • if not, warn the user, give them a chance to abort, then save the contents of the url to $0, do a system($0) and exit.

Is there a better way? Are there any pitfalls I might encounter?

Thanks in advance.

Replies are listed 'Best First'.
Re: update a running script
by Eily (Monsignor) on Jun 26, 2013 at 15:49 UTC

    You could use exec instead of system. If your system call is followed by exit the most "visible" change is that the END blocks won't be executed. It also means that you won't have a stack of calling scripts through which you'll have to go back at the end of the execution. An update will actually mean that your script is replaced by another instance, not that it waits for it to return.

    You could also reload your script partially, ie keep the main package still, and reload modules while running with Module::Reload::Selective. I've never used it though, so I can't tell you much about it.

Re: update a running script
by Anonymous Monk on Jun 27, 2013 at 00:35 UTC

    Is there a best way of having a script check to see if a new version of itself exists and updating it while running?

    Yes, update a module :)

    use App::Fantastic; App::Fantastics->version_check_if_enabled; App::Fantastics->run;

    Is there a better way? Are there any pitfalls I might encounter?

    :) Other than its annoying to be prompted for updates when you're trying to do stuff, offline?

    I imagine there could be a race condition, or dns hijacking if you're not https-ing or something else :)

Re: update a running script
by LanX (Saint) on Jun 27, 2013 at 00:48 UTC
    Depends what your code does.

    Evidently you can't switch while it's executed, but if you have the possibility to safely freeze, you could eval the code into an alternate namespace, and call the "main routine" there. (don't forget to empty the module before populating it)

    You would flip-flop from Module_a to Module_b for each release.

    Frozen data should be put into Module_Data.

    Like this you wouldn't need to stop and start a process.

    HTH!

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-28 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found