LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script to shutdown computer (https://www.linuxquestions.org/questions/programming-9/shell-script-to-shutdown-computer-644311/)

twallstrom 05-23-2008 10:51 AM

shell script to shutdown computer
 
I would like to have my home server shutdown every day at a certain time. Today I log into it and issue the shutdown command, but I would like this to be done automatically.
My thought was to write a simple shellscript (shutdown -h 22:30) and have cron run it.
The problem is that only root can initiate shutdown.
Can I solve with with a script, and what needs to be done, or is there a better way of accomplishing this?

Thanks!

weibullguy 05-23-2008 10:55 AM

Why a script to issue a system command? I've never tried shutdown, but can't think of a reason the following wouldn't work. Just add this line to root's crontab.
Code:

30 22 * * * root /sbin/shutdown -h

Hko 05-23-2008 11:35 AM

Quote:

Originally Posted by twallstrom (Post 3162559)
The problem is that only root can initiate shutdown.
Can I solve with with a script, and what needs to be done, or is there a better way of accomplishing this?

No, this can not be solved with a script: Then the script has to be executed by root.

if you add the line:
Code:

twallstrom ALL=NOPASSWD: /bin/cat
to /etc/sudoers, then the user "twallstrom" will be able to run the 'shutdown' command as root by running "sudo /sbin/shutdown -h 22:30"

jlinkels 05-24-2008 08:46 PM

The cron solution seems the best to me. I shut down all workstations in my home every night using that line.

However, I think Weibullguy made a mistake. The line he gave is to be entered in /etc/crontab. When it is in the root's crontab the user name should not be specified. Entering this line in /etc/crontab is the preferred way, because it is readable by all so you can see what happens even when you are an ordinary user.

Don't forget to put an additional blank line in crontab after the last line otherwise it won't work.

jlinkels


All times are GMT -5. The time now is 02:48 PM.