1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Linux Remote shutdown script?

Discussion in 'Software' started by WhiskeyAlpha, 30 Jul 2008.

  1. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    With the advent of ludicrously high electricity bills, I've decided to turn off my much used (and loved) 24/7 Ubuntu server (ala Glider's excellent guides).

    I've been playing around with wake on lan and found it to be a very viable alternative to leaving the thing running all day. It allows me to turn on the server whenever I need it, do what I need to do and then shut it down when I'm finished.

    The only problem is that I'm not the only person in the house that uses it. It's main role is as a file server and as such people in the house use it for backing up important files, as well as streaming audio and video to any of the machines in the house. They're all perfectly happy to use a WOL utility to switch it on, when they need to use it and wait the extra 20secs or so for it to boot. The problem comes with shutting it down when they are finished.

    For me I can just log in via ssh, issue a "sudo halt", followed by my password and the job's done. For everyone else though it's no so easy.

    First and foremost, they're not the most technically minded people and as a result, I'm not about to give them the root password (let alone spend hours showing them how to dial in via ssh and shut it down).

    I wondered if there was anyway of writing a script to perform a shutdown so that the other users could just "run the shutdown script". I know that the very essence of linux security is based on not elevating normal users to root privallages without a password so I worry that this might not be possible. Is there any other solutions you guys can think of?
     
  2. Kode

    Kode What's a Dremel?

    Joined:
    27 Jan 2008
    Posts:
    322
    Likes Received:
    2
    look into sudoers, you could give the users root access to halt without a password
     
  3. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
  4. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Thanks Kode. The only problem there is that I would still need to get them to actually shut it down (i.e. ssh in and run the "Halt" command or shutdown script).

    I can't believe I hadn't thought of this as an alternative. This post particularly interests me. From what I can make of the script it compares the network activity every 20mins to see if any "significant" packet transfers have taken place and if not, puts the computer into sleep mode.

    This script however, seems to use "user activity" to check what to do and then initiates a shutdown accordingly.

    A combination of those two scripts sounds like it would be absolutely perfect tbh. By that, I mean monitoring network traffic every 20mins and then initiating a shutdown (rather than sleep) if nothing significant has happened/is happening.

    I would need a little help and guidance to combine these scripts though, as my linux skills are novice-level (at best ;)). I know people don't appreciate being asked to hold your hand and guide you step by step through the necessary functions but there are a couple of bits I really don't get:

    - combining the two scripts. As I say, novice user so maybe I should have a go at making my own script and letting you guys point out the mistakes?

    - "cron"? - some sort or scheduling software I assume? Would it come with a default install of Hardy or would I need to d/l it? How easy is it to configure?

    - how do I run the script (or make cron run the script) automatically at startup?

    - wouldn't the script/cron still need to be run with root privaleges to be able to perform a shutdown? If so, how do I get around that?

    Thanks so much for the help so far guys, it's really very much appreciated :thumb:
     
  5. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
    cron is a scheduler built into linux - 'man cron' for moar info! i would be very, VERY surprised if it wasn't already on your system. it's pretty much part of linux.

    you have to be root to modify the crontab - the stuff that cron runs. therefore cron can run stuff as root, yes. not sure whether or not it ALWAYS runs commands as root - it might need a 'sudo' in front of the commands.

    as for merging them, look for the command that makes it sleep, and replace it with the shutdown command.
     
  6. airchie

    airchie What's a Dremel?

    Joined:
    22 Mar 2005
    Posts:
    2,136
    Likes Received:
    2
    I'm a linux n00b too but I think cron would be included in most distros by default.
    This search has some useful links, particularly the 2nd one. ;)
     
  7. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Thanks again Fod :)

    So.. how does this look? (I made the line I changed bold)

    Code:
    #!/bin/bash
    #
    # This is scheduled in CRON.  It will run every 20 minutes
    # and check for inactivity.  It compares the RX and TX packets
    # from 20 minutes ago to detect if they significantly increased.
    # If they haven't, it will force the system to shutdown.
    #
    
    log=~/Scripts/idle/log
    
    # Extract the RX/TX
    rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
    tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
    
    #Write Date to log
    date >> $log
    echo "Current Values" >> $log
    echo "rx: "$rx >> $log
    echo "tx: "$tx >> $log
    
    # Check if RX/TX Files Exist
    if [ -f ~/Scripts/idle/rx ] || [ -f ~Scripts/idle/tx ]; then
    	p_rx=`cat ~/Scripts/idle/rx`  ## store previous rx value in p_rx
    	p_tx=`cat ~/Scripts/idle/tx`  ## store previous tx value in p_tx
    	
    	echo "Previous Values" >> $log
    	echo "p_rx: "$p_rx >> $log
    	echo "t_rx: "$p_tx >> $log
    
    	echo $rx > ~/Scripts/idle/rx    ## Write packets to RX file
    	echo $tx > ~/Scripts/idle/tx    ## Write packets to TX file
    	
    	# Calculate threshold limit 
    	t_rx=`expr $p_rx + 1000`
    	t_tx=`expr $p_tx + 1000`
    
    	echo "Threshold Values" >> $log
    	echo "t_rx: "$t_rx >> $log
    	echo "t_tx: "$t_tx >> $log
    	echo " " >> $log
    	 
    	if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then  ## If network packets have not changed that much
    		echo "Suspend to Ram ..." >> $log
    		echo " " >> $log
    		rm ~/Scripts/idle/rx
    		rm ~/Scripts/idle/tx
                   sudo /etc/acpi/sleep.sh force  ## Force Sleep [B]<--The original line that will be removed![/B]
    		[B]sudo halt  ## Force Shutdown[/B]
    	fi
    	
    #Check if RX/TX Files Doesn't Exist
    else 
    	echo $rx > ~/Scripts/idle/rx ## Write packets to file
    	echo $tx > ~/Scripts/idle/tx
    	echo " " >> $log
    fi
    Cheers Airchie, that's helpful :)

    Okay guys so presuming that script is correct, if I just plonk it in say /usr/local/bin and call it activityCheck (feel free to suggest a more sensible location to save it) what permissions would i need to set to make it executable? And what would my "cron" command need to be?

    I'm guessing:
    Code:
    sudo cron 20 * * * * etc/sbin/activityCheck
    Would I need to run that command everytime the computer restarted though? Or would the job "stick" after a reboot?
     
    Last edited: 30 Jul 2008
  8. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Quick update, I worked out the whole crontab thing (I think).

    ---------------------------------------
    Code:
    sudo crontab -l
    showed me the current crontab for "root"
    ---------------------------------------
    ---------------------------------------
    Code:
    sudo crontab -e
    allows you to edit said crontab.
    ---------------------------------------
    ----------------------------------------------------
    I edited it so that it now has a new entry:
    Code:
    20 * * * * /usr/local/bin/activityCheck
    ----------------------------------------------------
    When I tried a "practise run" of the script by simply running it (i.e. "/usr/local/bin/activityCheck") it complained about the missing directories that the script points towards ("Scripts/idle" under my user area) so I added those and when I ran it again it didn't throw any errors. Just keeping my fingers crossed that it works now. :D
     
  9. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
    the crontab should be a permanent file containing tasks that are scheduled to run. it should persist over reboots.
    the script looks OK. tell me how it goes.
     
  10. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Hi Fod, having a few problems here unfortunately.

    The script works okay (tested it by calling it manually) but doesn't seem to be being performed, as scheduled by crontab.

    Little bit lost at the mo :(
     
  11. Glider

    Glider /dev/null

    Joined:
    2 Aug 2005
    Posts:
    4,173
    Likes Received:
    21
    call it with:
    Code:
    */20 * * * * sh <path to script>
    */20 means every 20 minutes, 20 means every 20'th minute of every hour, every day,...

    EDIT: Also, cut all the sudo's from the script, root is running it and you don't need to elevate the priviledges...

    And also, don't place it in /etc, place it in /root or something... /etc is for config files, not executables
     
  12. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Thanks Glider, I'll give those things a go.

    Not sure whether or not this makes any difference but I checked the /var/log/syslog and it actually shows crontab calling the script at the correct time. As I say, the script works fine if called manually (i.e. calling it twice with no/little network traffic in between causes the system to shutdown, whereas if you seperate the two calls with a large file upload/download it does not). :confused:
     
  13. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
    sorry, reached the limit of my knowledge. perhaps you can ask on the ubuntu forums?
     
  14. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Hey, no problem Fod. Thanks again for all the help. :thumb:

    @ Glider:

    Hey dude, I did what you said, but unfortunately I'm still in the same situation. The script will run just fine if I call "sudo sh /root/checkActivity". But despite crontab leaving logs in the "/var/log/syslog" file on the correct times, it just doesn't seem to work. It seems like it's not "really" running the script at all, as the "Scripts/idle/log" file never gets updated unless I manually call the script.

    NB I changed the time to "*/5" (every 5 mins) just for testing.

    Any ideas?
     
    Last edited: 30 Jul 2008
  15. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
    oh

    make sure the script has execute privileges for all all users. sudo chmod a+x filename.
     
  16. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    Just tried that and still a no-go unfortunately :(
     
  17. Cinnander

    Cinnander What's a Dremel?

    Joined:
    19 Apr 2007
    Posts:
    393
    Likes Received:
    2
    Try adding some extra debugging logging piped to an absolute file name (i.e. /var/logs/somewhere.log - no variables in the name)
    echo "I am shutting down as activity is $X and $Y which are less than $A and $B"
    and see what happens? Debugging bash scripts is a pain in the arse without hundreds of test echos, I find :|
    (Remove them afterwards, ofc)

    Also where does ~ turn out to be? That might differ from running it under sudo and cron.
    In the former which case it will be the same as non-sudo ~ for whichever user runs the command*, probably /home/youruser/. Cron, which *probably* doesn't use a login shell might not even have a value of $HOME and ~? I'm guessing here though.

    * Try: "sudo echo ~ && echo ~" it should be the same both times.
     
    Last edited: 31 Jul 2008
  18. Elv13

    Elv13 What's a Dremel?

    Joined:
    26 Apr 2006
    Posts:
    107
    Likes Received:
    0
    I will admit that i did not read the topic, but here are few infos.

    chmod u+s /sbin/halt give the right to everybody to call shutdown

    you can generate and ssh key to call shutdown over the lan without password with a 1 line ssh command.
     
  19. Glider

    Glider /dev/null

    Joined:
    2 Aug 2005
    Posts:
    4,173
    Likes Received:
    21
    I went over your script and found an error (marked in bold)

    Code:
    # Check if RX/TX Files Exist
    if [ -f ~/Scripts/idle/rx ] || [ -f ~Scripts/idle/tx ]; then
    =>
    Code:
    # Check if RX/TX Files Exist
    if [ -f ~/Scripts/idle/rx ] || [ -f ~[B]/[/B]Scripts/idle/tx ]; then
    I have some time in the afternoon, I'll write up something a bit easier and more logical and add 'install' instructions too...
     
  20. Fod

    Fod what is the cheesecake?

    Joined:
    26 Aug 2004
    Posts:
    5,802
    Likes Received:
    133
    hah, sorry i missed that. ~Scripts would be a shortcut for the user Scripts' homedir.
     
Tags:

Share This Page