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

I am very new to perl, so this may seem simple to some, but my goal is to be able to reboot my linux box from a web site. The perl script works when I run it from the command line, but when I run it from a web site I get: "Premature end of script headers:". Below is my small script, does anyone have any ideas on why this won't work? system("shutdown -g0 -r"); print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser);

Replies are listed 'Best First'.
Re: Simple reboot from perl?
by moritz (Cardinal) on Nov 04, 2011 at 14:25 UTC

    The CGI script probably has insufficient permissions to call shutdown.

    See system for how to check the executed command for errors.

    Oh, and make sure the URL is password-protected, otherwise googlebot will reboot your machine...

Re: Simple reboot from perl?
by keszler (Priest) on Nov 04, 2011 at 15:55 UTC

    I'd take a step further back and ask why you want to reboot the linux box at all? Mine are rebooted only when I upgrade the kernel or a power outage lasts longer than the UPS batteries.

    What problem exists that a reboot cures?

Re: Simple reboot from perl?
by johnny_carlos (Scribe) on Nov 04, 2011 at 14:57 UTC
    Try: system("shutdown -g0 -r") or die "shutdown failed: $?";

    It might give you some more information.

    I'm thinking it's the user than is running your webserver. That user will need permission to execute the shutdown command. You can run your webserver as root, probably not a recommended thing to do but it should work. Or opening up permissions to the shutdown command. You'll also want to make sure your webserver is configured to start back up automatically after a reboot.

      Unlike other IO commands, system returns 0 on sucess, making the system() or die ... construct rather useless.

      the documentation elaborates quite a bit on the correct way to check for errors. Read it (both of you :-).

      Also I'd never, ever consider running the web server as root, not even for testing. A much saner approach would be to add a sudo rule that allows the www or www-data user to execute just that one command. But the whole idea seems flawed to me. Remote management systems exist.

        ++'d. Yes that was rather silly of me, wasn't it? 'Tis better to go with what's in the moritz documentation link.
Re: Simple reboot from perl?
by mojotoad (Monsignor) on Nov 08, 2011 at 09:46 UTC
    ssh

    Oh, but wait. You're probably trying to build a web-based console for an entire population of machines. In which case, 'rebooting' is the least of your worries.

    Seriously, what problem are you attempting to solve?

    Cheers,
    Matt

      Not OP, but In my case I've got a wedged kernel module that won't respond to rmmod.