Linux Remote shutdown script?

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

  1. Glider

    Glider /dev/null

    Joined:
    2 Aug 2005
    Posts:
    4,173
    Likes Received:
    21
    There, finished v0.1 ;)

    First, remove all of the previous script (crontab, log, script itself, the various files it created,...)

    And then, issue this:
    Code:
    sudo su
    cd
    mkdir -p scripts
    wget http://glider.sin.khk.be/ActivityShutdown.sh -O /root/scripts/ActivityShutdown.sh
    chmod u+x /root/scripts/ActivityShutdown.sh
    
    crontab -e
    Add to the crontab file:

    Code:
    */20 * * * * sh /root/scripts/ActivityShutdown.sh
    Save and exit and hope for the best...
     
  2. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    EDIT: I was writing this post before Glider's reply and as such didn't see it until afterwards :(

    Hi guys, thanks again for the help.

    Um, the typo with the script might have been where I was playing around getting it to work. :blush:

    But, good news:

    I got up early this morning, went for a run and came back to it with a fresh mind. I deleted my script, my entry in the crontab and basically anything that I'd edited so far and started afresh. I'm glad to say it seems to be working fine now! I'll just go through the steps quickly for anyone who's interested and maybe Glider or Fod can help me tidy it up a bit :). My understanding of user rights is I think what, might have been the cause so if I list the exact steps, as I say, Glider and Fod might help me put it straight :).

    I first ran the command:
    Code:
    sudo nano powerOff
    This creates a new blank script called "powerOff". I then entered the following bash script:

    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=/root/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 /root/scripts/idle/rx ] || [ -f /root/scripts/idle/tx ]; then
            p_rx=`cat /root/scripts/idle/rx`  ## store previous rx value in p_rx
            p_tx=`cat /root/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 > /root/scripts/idle/rx    ## Write packets to RX file
            echo $tx > /root/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 "System shutting down, due to lack of network activity ..." >> $log
                    echo " " >> $log
                    rm /root/scripts/idle/rx
                    rm /root/scripts/idle/tx
                    /sbin/halt ## Shutdown
            fi
    
    #Check if RX/TX Files Doesn't Exist
    else
            echo $rx > /root/scripts/idle/rx ## Write packets to file
            echo $tx > /root/scripts/idle/tx
            echo " " >> $log
    fi
    
    With this entered I then pressed "ctrl+x" and saved the script.

    I did:
    Code:
    sudo chmod 700 /root/powerOff
    which I believe makes the script only able to run as root.

    A quick:
    Code:
    sudo chmod a+x /root/powerOff
    makes the script executable.

    If you now run the script with:
    Code:
    sudo sh /root/powerOff
    it will complain about the missing "script" and "idle" directories, as they haven't been created yet.

    A quick:
    Code:
    sudo mkdir /root/scripts
    followed by
    Code:
    sudo mkdir /root/script/idle
    sorts that out.

    I then did:
    Code:
    sudo chmod 755 -R /root/scripts
    this means make the file accessible to all users (the -R means recursively or, in laymans terms, apply this rule to all subdirectories and files inside of /scripts)

    Finally, I added the entry to the root crontab:
    Code:
    sudo crontab -e
    open the existing crontab.

    Simply add this to a new line:
    Code:
    */30 * * * * sh /root/powerOff
    and hit "ctrl+x" to save. (feel free to change the "30" for any other value fom 0-60, depending on how often you want it to check)

    eh voila! Hopefully this can be of use to somebody else (Once Glider and Fod have done their thing to it :D), especially in light of the ludicrous increase in energy prices recently :)
     
    Last edited: 31 Jul 2008
  3. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    OMG! I feel awful :blush:

    I'm really sorry you put all that work into it for me dude but I'm sure it's a damn site prettier than mine huh?

    Your advice has been priceless for me (not just on this matter) and the amount of work/help that you're willing to supply to people on these forums should be applauded.

    *tips hat* Thanks again Glider :thumb:
     
  4. Glider

    Glider /dev/null

    Joined:
    2 Aug 2005
    Posts:
    4,173
    Likes Received:
    21
    This is more or less the same,yet different... chmod 700 sets Read, Write and eXecute for 'user' on the file, and a+x adds eXecute for all (user, group, others). So you'll end up with rwx--x--x as rights, rwx for user, and x for group and other.

    Don't ;) I don't mind writing scripts... And it just took me 10 minutes to write it... I think it's more logical (1 logfile for the entire script instead of lots of files scattered around...) but if it isn't broken, don't fix it and just use yours ;)
     
  5. WhiskeyAlpha

    WhiskeyAlpha What's a Dremel?

    Joined:
    5 May 2006
    Posts:
    838
    Likes Received:
    4
    It is more logical tbh (I just downloaded it and took a look). I have a habit of using others' code/scripts and being scared to change too much in case I "break" it. The thing is, I can write fairly good code (java/C/C++) so the bash script stuff really isn't that complicated and I should be more confident with it. There is definitely an element of "if it ain't broke, don't fix it" to that mentality but what about "if it ain't broke clean it up and make it more efficient" ;).

    I'll leave it for now but when I come to setting my new file server (some 1TB drives on Raid 5) I'll be sure to dig up this post and use your (clearly superior) effort :)

    Thanks again dude :thumb:
     
Tags:

Share This Page