Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

First the situation:

I have a server that I want to shutdown automatically at night (00:00), but I need to make sure no one is logged via ssh in still. If they are, then I will allow up to another hour for a second shutdown attempt (01:00) at which time the server goes down no matter what.

I figure I can just go with a cron job for shutdown (unless there's a better way to schedule shutdowns), but wouldn't know how to implement the check in the first case.

Extra:

Is there a way to know if a user is actively using his samba share, and in this case, not complete the shutdown procedure. Or better yet, check if there is a healthy amount of Disk I/O that will also not complete the shutdown.

I have seen some answers already that can help me out, so I wish to know specifically, what would be the best way to accomplish this as a whole, without hacking up different bits and pieces.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

You can check for active connections with:

netstat -an | grep ESTABLISHED | grep ":[portname] " | wc -l
  • Port 22 is for ssh connections.
  • Port 445 is for samba connections.

So add this to a bash script and add the bash script to a cronjob.

  1. Creating a cronjob:

    sudo su
    crontab -e
    

    Insert something like this for a check at 24:00 and a forced shutdown at 01:00:

    # Check for shutdown system at 24:00 every day
    00 0 * * * /usr/bin/shutdown_script
    # Shutdown at 01:00 every day
    00 1 * * * /usr/bin/shutdown -h now
    
  2. Create the script (still as root ;) ):

    gedit /usr/bin/shutdown_script
    

    and add in the commands you want to check for:

    #!/bin/bash
    users_ssh=`netstat -an | grep ESTABLISHED | grep ':[445] ' | wc -l`
    users_ssh_alertvalue="0"
    if [ "$users_ssh" -eq "$users_ssh_alertvalue" ]; then
         /sbin/shutdown -h now
    fi 
    

    and ofcourse make it executable.

You can make this more refined by adding more options to the if all you need to do is find the correct command to find the value you want to check. I have not (yet) found a nice I/O check though (iostat and iotop look promising).

edit: while looking for a I/O check command I found mpstat. It reports processors related statistics (see man mpstat). CPU idle time from mpstat:

mpstat | grep all | sed -s 's/.* all// ; s/ * / /g' | cut -d ' ' -f10

So you could add this in and shutdown when CPU idle time is higher than 98%.

share|improve this answer
    
I don't know why I assumed this would be anything but this, but I had to ask. I guess that's the price we pay for the power of linux! Thanks for pointing me in a narrower direction. Really like the completeness of this answer. –  fadmcrank Aug 20 '11 at 16:53
    
well the sed and grep parts are difficult yes but it isn't all bad (most of these things are doing a good google search :D ) –  Rinzwind Aug 20 '11 at 16:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.