LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I reboot system from perl script? (http://www.linuxquestions.org/questions/linux-general-1/how-do-i-reboot-system-from-perl-script-684077/)

RavenLX 11-17-2008 11:31 AM

How do I reboot system from perl script?
 
I have a perl script that runs from a root cron job and tests a few things and if those tests fail, then to reboot the whole system (Ubuntu 8.04 LTS server). Thing is, it's not rebooting. It does nothing.

I've tried each of these:

exec("reboot -f");

exec("shutdown -r now");

system("reboot -f");

system("shudown -r now");

None of these will reboot the system. Yet, in an automated install script the 2nd method WORKED. (I set it up though so you had to press a key to reboot). This time, I *don't* want any keypresses. It should be automatic as I need this to work as a self-healing option.

Anyone know how to do this?

trickykid 11-17-2008 11:35 AM

You probably need to specify the full path to the commands issued and make sure it's getting run as root user.

pwc101 11-17-2008 11:43 AM

Rebooting a server seems a bit drastic; can't you just restart the relevant services, if they're the root of the problem?

RavenLX 11-17-2008 12:04 PM

pwc101 - The script does attempt to restart servers, but in the event that servers still can't be restarted, then a reboot will commence.

BTW, I figured it out. My path was right, it was that it needed the right permissions. Doing this worked from cron job:

exec("sudo shutdown -r now");

Whenever you want to use system-type commands, even within the root cron job (ie. crontab -e), you have to specify 'sudo' for some odd reason.

trickykid 11-17-2008 01:00 PM

Quote:

Originally Posted by FuzzieDice (Post 3345049)
pwc101 - The script does attempt to restart servers, but in the event that servers still can't be restarted, then a reboot will commence.

BTW, I figured it out. My path was right, it was that it needed the right permissions. Doing this worked from cron job:

exec("sudo shutdown -r now");

Whenever you want to use system-type commands, even within the root cron job (ie. crontab -e), you have to specify 'sudo' for some odd reason.

Actually your path as root might have been right but cron doesn't see that path usually. You can probably remove sudo from your script and either put PATH=/paths/here;/path/here within you're crontab so it knows where to look for such commands.


All times are GMT -5. The time now is 01:45 PM.