PC suspends while sharing files with Samba

Questions about Wi-Fi and other network devices, file sharing, firewalls, connection sharing etc
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
sinisab89

PC suspends while sharing files with Samba

Post by sinisab89 »

Hi!
I need some help. My PC suspends while Samba sharing is active. I have set up my power manager to suspend after 30min of inactivity. I have TV smart box with CoreELEC connected to home network and I stream movies from my PC to it with Samba. Basically, I share my movies folder on home network through Samba. It's working great except one thing - after 30min PC suspends even though I am streaming a movie from it's hard drive.
Is there some way I can inhibit system suspend while Samba share is active? I don't want to turn off system suspend...
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
sinisab89

Re: PC suspends while sharing files with Samba

Post by sinisab89 »

I'm trying something but it doesn't quite work. I'm really inexperienced in this stuff...
I have found a script :

Code: Select all

#!/bin/bash

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

echo “SUSPEND!!”
#pm-suspend
My idea is to replace echo command with xdotool and run every few minutes. Problem is whatever is happening with Samba share i get "Samba SMB lock exists" response! It's like `/usr/bin/smbstatus | grep DENY | wc -l`command always returns "1" as a result. If I use this command in terminal it works as expected - if there are locked files in Samba it returns "1", if not than "0". If i run the script it seems like it's "1" every time so it makes it useless. If it could work I would replace "echo “Samba SMB lock exists”" with something like "xdotool key escape" and make it run every 20 minutes. What's the problem here? Is the script bad?
sinisab89

Re: PC suspends while sharing files with Samba

Post by sinisab89 »

Script now looks like this and it's functional:

Code: Select all

#!/bin/bash
sleep_period=10s

if [ `smbstatus | grep DENY | wc -l` != 0 ]
then
xdotool key a
exit 0
fi
Now I have another problem - smbstatus command only works with sudo. If I launch script manually from terminal with sudo privileges i types a letter "a" as a response. I've put it in my startup apps and it's triggered every minute but nothing happens. I guess thats because it doesnt run as sudo, so smbstatus command always returns "0" and nothing happens. How can i run this script automatically as sudo so that smbstatus within it works as it should?
sinisab89

Re: PC suspends while sharing files with Samba

Post by sinisab89 »

I did it! I'll explain how, maybe it will help others.

Install xdotool

Code: Select all

sudo apt install xdotool
Then, make a script in /usr/local/bin/

Code: Select all

sudo xed /usr/local/bin/checksmb.sh
Copy this to xed:

Code: Select all

#!/bin/bash
export DISPLAY=:0
export XAUTHORITY=/home/sinisab89/.Xauthority 

if [ `sudo smbstatus | grep DENY | wc -l` != 0 ]
then
xdotool key F9
exit 0
fi
Save file.

DISPLAY and XAUTHORITY is needed for CRON to function properly. smbstatus only runs with sudo. I chose F9 key, maybe there are better options.
Script reads smbstatus and if there are more than 0 "DENY" entries in the report (active connections in Samba) simulates F9 key press which resets system idle timer and prevents suspend. If there are 0 "DENY" entries script does nothing.

Make checksmb.sh executable

Code: Select all

sudo chmod +x /usr/local/bin/checksmb.sh
Now set up CRON to execute script every x minutes. I chose 20 minutes.

Code: Select all

sudo crontab -e
Scroll to the bottom and add

Code: Select all

*/20 * * * * /usr/local/bin/checksmb.sh >> /home/YOUR USERNAME/cron.log 2>&1 
>> /home/YOUR USERNAME/cron.log 2>&1 makes a cron.log file in your home folder. You can use it for troubleshooting if the script is not executed properly.

With this command you can see if CRON is executing script at given time

Code: Select all

less /var/log/syslog | grep checksmb
This is not the most elegant solution but it works. If someone knows how, for example, video players prevent system from sleeping it would be nice to modify this script so it doesn't use xdotool. There has to be some system suspend inhibitor but I just couldn't find anything online.
dr0
Level 1
Level 1
Posts: 20
Joined: Tue Nov 07, 2017 9:11 am

Re: PC suspends while sharing files with Samba

Post by dr0 »

I know that this topic is rather old, but I just wanted to thank you sinisab89 for this detailed guide. With some minor modifications, it has worked perfectly for me.
PS: my keyboard has a physical WakeUp button, so, with the help of sudo xev I learned that my system registers this button as "XF86WakeUp", after which I've put it in your script instead of F9.
PPS: an open-source program called qBittorrent has a built-in sleep inhibitor for active uploads and/or downloads. It even manages to inhibit system suspension without intervening with the display power saving. Perhaps, someone knowledgeable would be able to create an even better scrip leveraging qBittorrent's sleep inhibition techniques.
Locked

Return to “Networking”