Results 1 to 11 of 11

Thread: Ubuntu: Auto shut down on idle

  1. #1
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Ubuntu: Auto shut down on idle

    Just checking that this is not crazily wrong...

    Code:
    00,05,10,15,20,25,30,35,40,45,50,55 * * * * root /home/sim/poweroff.sh
    (I am guessing this make the script automatically run every 5 minutes of every hour and every day)

    I added that line using nano into /etc/crontab and then made a new file in /home/sim/ called poweroff.sh using nano containing:

    Code:
    #!/bin/bash
    threshold=55
    username=sim
    inactive=`who -a | grep $username | cut -c 45-46 | sort | grep [0-9] | head -n1 | sed 's/ //g'`
    
    if [ "$inactive" != "" ]; then
    
    echo "Idle time is: " $inactive
    
    if [ "$inactive" -ge "$threshold" ]; then
    echo "Threshold met so issuing shutdown command"
    /sbin/shutdown -h now
    else
    echo "Below threshold"
    fi
    else
    echo "Idle time is: 0"
    fi
    echo "Ending"
    (this is the script - blinedly copied)

    Then I ran
    Code:
    sudo chmod +x poweroff.sh
    So to test, I have turned the server on and not touched anything. I will check in an hour to see if it has turned itself off

  2. #2
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: Ubuntu: Auto shut down on idle

    try */12 instead of 00,05,10,15,20,25,30,35,40,45,50,55

    of course, generally speaking, better to let one of the system power management daemons deal with idle shutoff, no?

  3. Received thanks from:

    SiM (17-07-2008)

  4. #3
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: Ubuntu: Auto shut down on idle

    ++

    And even if the cron method is more applicable for your needs, for whatever reason, I'd *really* move that poweroff.sh into /usr/local/bin, then chown root:root and chmod 755.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  5. #4
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Re: Ubuntu: Auto shut down on idle

    OK it didn't work as I had it above... Do you have a link explaining how to use the system power management daemons? I'll try that before fiddling with this more... Thanks

  6. #5
    I R Toff Pandi! TAKTAK's Avatar
    Join Date
    Mar 2008
    Location
    Vergon6
    Posts
    7,449
    Thanks
    549
    Thanked
    1,012 times in 747 posts
    • TAKTAK's system
      • Motherboard:
      • ASUS ROG STRIX B450-F GAMING
      • CPU:
      • Ryzen 7 3700X
      • Memory:
      • 16GB Corsair Vengeance LPX 3200MHz
      • Storage:
      • 500GB Samsung 970 EVO
      • Graphics card(s):
      • 5700 XT 50th Anniversary
      • PSU:
      • Be Quiet SFX-L 600W
      • Case:
      • Lian Li PC-O11 Mini
      • Operating System:
      • Windows 10
      • Monitor(s):
      • LG Ultrawide
      • Internet:
      • 200Mb FTTP

    Re: Ubuntu: Auto shut down on idle

    the advanced power management daemon?? HERE

    it's used to shut the system down when power is low, but you might be able to rework it to a given variable...

    also, whats wrong with the sytem just going to sleep? or do you just want to save more power?

    after googling
    Code:
    #!/bin/bash
    #
    # This is scheduled in CRON using ROOT, it runs every 5 minutes
    # and uses who -a to determine user activity. Once the idle time is
    # more than the threshold value it shuts the system down.
    #
    echo "Start of sidle.shl"
    
    threshold=25
    log=/home/ubuntu/sidle.log
    userid=kids
    inactive=`who -a | grep $userid | cut -c 45-46 | sed 's/ //g'`
    
    if [ "$inactive" != "" ]; then
    
    echo "Idle time is: " $inactive
    
    if [ "$inactive" -gt "$threshold" ]; then
    echo "Threshold met so issuing shutdown command"
    /sbin/shutdown -h now
    else
    echo "Bellow threshold"
    fi
    else
    echo "Idle time is: 0"
    fi
    
    echo "Ending"
    Here, is the original code source, plus some other snippets and mods...

    edit: aah, i see you were using the same thread...
    Post Counts and Other Rewards, Rules, Folding@Home, Fans: Push vs Pull vs Push-Pull, Corsair PSU OEMs.

    Quote Originally Posted by razer121 View Post
    Would you like me to enter you? it would be my pleasure
    TAKTAK.co.uk

  7. Received thanks from:

    SiM (17-07-2008)

  8. #6
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Re: Ubuntu: Auto shut down on idle

    Yup, that was what I was trying... Sleep is no good as I don't want to have to manually ssh in to switch it off everytime... I don't think it goes into sleep automatically anyway

    OK I am going to learn about crontab to see if I can fix this

  9. #7
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: Ubuntu: Auto shut down on idle

    There's lots of power management configuration stuff here, just swap out portage commands for apt ones.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  10. Received thanks from:

    SiM (17-07-2008)

  11. #8
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Re: Ubuntu: Auto shut down on idle

    hmm... /etc/crontab is different to the crontab that comes up when you use crontab -e... Well I have stuck it in both, let see what happens

    I have moved it to /usr/local/bin, chown'd it and chmod'd it like you suggested... but what does that actually do? Does it just make it ask for the root password before allowing to edit?

    Thanks for the guide Aidanjt, will give it a look while I test this. (Will put threshold to 10 minutes for the test this time lol)

  12. #9
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: Ubuntu: Auto shut down on idle

    Quote Originally Posted by SiM View Post
    I have moved it to /usr/local/bin, chown'd it and chmod'd it like you suggested... but what does that actually do? Does it just make it ask for the root password before allowing to edit?
    Exactly, so that stops any malicious processes or people writing new commands to the script, and thus not being able to gain root privileges via the cronjob without explicit authorisation.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  13. #10
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Re: Ubuntu: Auto shut down on idle

    OK so I can confirm that the script does work. If I run it manually, it will shut down if the idle timer hits the threshold. But it is not running it automatically every 5 minutes as it should... So something is up with the crontab settings... I will try it with just * for the minutes

  14. #11
    SiM
    SiM is offline
    Senior Member
    Join Date
    Apr 2006
    Location
    London
    Posts
    7,787
    Thanks
    300
    Thanked
    633 times in 422 posts
    • SiM's system
      • Motherboard:
      • P5K Premium
      • CPU:
      • Q6600
      • Memory:
      • 8GB PC2-6400 OCZ ReaperX + Platinum
      • Storage:
      • 3 x 320gb HD322HJ single platter in Raid 0
      • Graphics card(s):
      • PNY GTX285
      • PSU:
      • Corsair TX650W
      • Case:
      • Antec 1200
      • Monitor(s):
      • 2407-HC

    Re: Ubuntu: Auto shut down on idle

    Correction - it only works manually sometimes... but sometimes the counter doesn't go up! I think it needs some load before it can start count idle time

    Edit: aahhh I understand now... It works when I log in from 2 PCs on the same account (one on the actual PC and the other via putty) and then leave one of them idle... so the script does work.... cron is not running it for some reason...

    Edit2: damn, I just realised that this script won't work in the way I want to use it anyway, even if the cron worked... it needs a specific user logged in before it works, because it looks for the idle time of the specified user... I want a solution that won't need me to ssh or log in at all
    Back to the drawing board

    Any ideas anyone?
    Last edited by SiM; 18-07-2008 at 05:17 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 12
    Last Post: 29-06-2008, 11:23 AM
  2. P5K Premium / 8GB RAM
    By rwm63 in forum Help! Quick Relief From Tech Headaches
    Replies: 17
    Last Post: 18-06-2008, 12:14 PM
  3. Ubuntu
    By just_laze in forum Software
    Replies: 22
    Last Post: 07-04-2008, 09:14 AM
  4. Ubuntu - utter junk? (A rant)
    By stroberaver in forum Software
    Replies: 18
    Last Post: 26-02-2008, 02:14 PM
  5. Speedtouch 330 and Virgin.net under Ubuntu
    By PeteSmith in forum Networking and Broadband
    Replies: 14
    Last Post: 13-08-2007, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •