Ubuntu – Prevent sleep while sharing files over samba

Windows has a little feature that Ubuntu/samba server sadly does not seem to have: On Linux, If another user is accessing files on your samba server, the system will still enter an S3 Sleep / suspend, where as Windows will stay awake until the share is no longer active.

After some research I have developed a script that will prevent Ubuntu from sleeping while sharing files. If your HTPC is accessing videos on your desktop, this can prevent your desktop from sleeping.

These instructions were designed for Ubuntu 10.10 with the GNOME desktop as it relies on a command “gnome-screensaver-command”. If you find another command that can reset the system idle timer, the script could work for you.

To Prevent sleeping while samba is active, create the file ~/.checksmb with the following script: (scroll down for instructions creating file if you don’t know how)

#!/usr/bin/env bash

# sleep prevent command - "gnome-screensaver-command --poke" resets idle timer
PREVENTSLEEP='gnome-screensaver-command --poke'

# gnome-screensaver-command uses X11 which expects this ENV to be set
export DISPLAY=:0.0

#### DO NOT EDIT ####
smbstatus | grep -q "DENY_WRITE"
if [ $? == 0 ]; then $PREVENTSLEEP ; fi

open a terminal (control + alt + T or use menu > Accessories) and type gedit ~/.checksmb and then paste that code into the file, save and close.

Then in the terminal, run chmod +x ~/.checksmb
Nothing should of printed from that.

Then type crontab -e to open the editor for crontab.

If prompted to select an editor, choose what ever number nano is. its the default on Ubuntu 10.10.

Scroll to bottom of file (page down key) and add ON A NEW LINE

* * * * * ~/.checksmb

That line should be all by itself. Now press Control + X, and then press Y to confirm save.

Now, if you haven’t already, you can go configure your PC to go to Sleep after inactivity under System > Preferences > Power Management.

Every 1 minute this script will run and check if anyone is accessing files over Samba (SMB) and it will prevent sleep / suspend.

Tags:

I am very passionate about development, and communities. I am now living my dream of running my own gaming company, as I have now founded Starlis LLC and operate the Empire Minecraft service, the place where I met my lovely wife Mia!

10 Comments Leave yours

  1. panthe #

    Hi,
    I’ve just used your script with Ubuntu 10.04 and works well.
    But now in Ubuntu 11.04 don’t work.
    I think that is because Unity have replaced Gnome.
    Have you some idea about?

    Thanks 😉
    Panthe

  2. Unsure why it wouldnt.

    I just checked on my 11.04 Desktop, and gnome-screensaver-command is still there.

    I ran the script posted here, and it still ran without error.

    try manually invoking it from your command line (outside of cronjob) and see if any errors are reported.

  3. This post was a huge help to me; a million internets to you, sir! 🙂
    You may want to add that KDE users can use this command for the PREVENTSLEEP variable: qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity

  4. Didi1357 #

    I am using 11.04 and experiencing the same problems. I like your idea and will try to fix that. It seems that there are problems whith the dissemination of the return value, but the occur only in cron oO

    Greetings Didi

  5. Didi1357 #

    Fixing that stuff was just too messy for me… The problem with the return values was the wrong shell in /etc/crontab. There you can change sh to /bin/bash… With this one I got at least the following script working… It checks for some things like someone streaming from smb or if someone is connected to ssh and stuff. If nobody is connected it suspends the pc. I just disabled the Ubuntu-suspend with GUI. So check out the script:

    #!/bin/bash
    #SSH, FTP, VNC(vino) Check
    if [ `netstat -ute | grep ssh | wc -l` == “0” ]
    then
    if [ `/usr/bin/smbstatus | grep DENY | wc -l` == “0” ]
    then
    if [ `netstat -ute | grep ftp | wc -l` == “0” ]
    then
    if [ `netstat -ute | grep :5900 | wc -l` == “0” ]
    then
    if [ `pgrep rsync | wc -l` == “0” ]
    then
    pm-suspend
    else echo “Got work to do (Backup)”
    fi
    else echo “Got user on VNC”
    fi
    else echo “Got user on FTP”
    fi
    else echo “Got user on SMB”
    fi
    else echo “Got user on SSH”
    fi
    exit 0

    I put it in cron which runs it every 30 minutes. Works like a charm 🙂

    Greetings Didi

    • Didi1357 #

      Damn… Code isn’t formatted when I put it here.. Anyways, Good luck.

      • Hi Did, thanks for the code. I’ve tidied it up a little bit and added a few extra clauses, I find it very useful, hope you like what I’ve done with it:

        #!/bin/bash
        #SSH, FTP, VNC(vino) Check

        if [ `/usr/bin/smbstatus | grep DENY | wc -l` != “0” ]
        then
        echo “Samba (SMB) lock exists”
        exit 0
        fi

        if [ `pgrep rsync | wc -l` != “0” ]
        then
        echo “Process relating to rsync exists”
        exit 0
        fi

        if [ `pgrep get_iplayer | wc -l` != “0” ]
        then
        echo “Process relating to get_iplayer exists”
        exit 0
        fi

        if [ `pgrep rtorrent | wc -l` != “0” ]
        then
        echo “Process relating to rtorrent exists”
        exit 0
        fi

        ### following takes a while

        netstat -ute > .netstat

        ### now use output for various tests:

        if [ `cat .netstat | grep ftp | wc -l` != “0” ]
        then
        echo “Got user on FTP”
        exit 0
        fi

        if [ `cat .netstat | grep telnet | wc -l` != “0” ]
        then
        echo “Got user on telnet”
        exit 0
        fi

        if [ `cat .netstat | grep :5900 | wc -l` != “0” ]
        then
        echo “Got user on VNC”
        exit 0
        fi

        if [ `cat .netstat | grep ssh | wc -l` != “0” ]
        then
        echo “Got user on SSH”
        exit 0
        fi

        echo “SUSPEND!!”
        #pm-suspend

  6. Nice additions, though id really prefer to let Ubuntu manage auto sleep so that as Ubuntu gets better about it, its own criteria can also go into play.

    Hopefully Ubuntu will mimic windows one day and handle most of these conditions itself.

    It would be possible to take your above additions, and wrap those as additional criteria to trigger the simulate user activity.

  7. ecadman #

    A lot of searches about how to prevent a samba server from suspending while in use remotely lead back here. I’ve wasted an afternoon trying to crack this, so here’s to trying to save others from the same.

    I prefer to have my Ubuntu 13.04 box automatically suspend itself after some period of “inactivity.” But once I’ve woken it by magic packet (WoL) or by good ol’ power button pushing so it can share media for my kids to watch streamed on the TV, I don’t want it to go asleep or into suspend or hibernation or any other power-saving mode while the samba share is in use. This avoids unnecessary tantrums by me or the kids. This will also keep the computer from suspending if in use locally (by mouse/keyboard) and can be tweaked to prevent going down upon a myriad other events.

    These instructions assume your machine already has a samba share and the machine will suspend automatically or on command.

    1. Install powernap (sudo apt-get install powernap).
    2. Change the config file at /etc/default/powernap so that it automatically runs upon start-up. (Strangely it can’t run at all without changing this.)
    3. Alter the config file at /etc/powernap/config. There are many options to experiment, but these are the only lines I changed in the file:

    ACTION_METHOD = 1

    ABSENT_SECONDS = 1800

    [LoadMonitor]
    threshold = 5

    And most importantly, under this section…

    [IOMonitor]
    #kvm-io = “kvm”
    #mysqld-io = “mysql”

    Add this line

    smbd-io = “smbd”

    Aside from Samba, if one is concerned about preventing the computer suspension or hibernating during other events, one may wish to prevent it going down during RDP or SSH connections with the last two lines below.

    [TCPMonitor]
    ssh = 22
    rdp = 3389

    Your ports may be different if you’ve changed them from the default ports, of course. If one wishes to prevent suspension during other processes, such as package management or installation, simply add the process under “[ProcessMonitor],” similarly to this:

    [ProcessMonitor]
    #init = “^/sbin/init”
    dpkg = “dpkg ”
    aptget = “apt-get ”

    Hope that is helpful to someone.

1 Trackbacks

Leave a Reply





I am Senior Software Engineer and Entrepeneur. I am an enthusiast and love creating things. I operate my own side company in my free time called Starlis LLC, working in Minecraft.

I enjoy doing things right and learning modern technologies.