LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2008, 10:51 AM   #1
twallstrom
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
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!
 
Old 05-23-2008, 10:55 AM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware 14.2
Posts: 2,815
Blog Entries: 1

Rep: Reputation: 261Reputation: 261Reputation: 261
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
 
Old 05-23-2008, 11:35 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by twallstrom View Post
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"
 
Old 05-24-2008, 08:46 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
given a shell script to register computer, admin out for the weekend Ryzol Linux - Networking 2 05-13-2008 12:55 PM
Is there a way to easily shutdown a remote linux server with a shell script? bdb4269 Programming 7 01-26-2007 04:08 PM
Computer does not shutdown jxrod2000 SUSE / openSUSE 5 12-17-2006 10:19 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Shell Script to shutdown network computers? Brnzwngs Linux - Networking 6 09-11-2005 10:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration