Every once in a while my machine runs stuff in the background, like backups, db dumps and whatnot. This obviously causes performance issues at times, but among other things I don’t want to shutdown during these processes.
So I created a simple status icon to warn me if one of those processes were executing so I wouldn’t shutdown the machine, and it also will disable hibernate/suspend.
I figure I’d share it with the world.
#!/usr/bin/env python
import gobject
import gtk
import subprocess
import os
import re
gobject.threads_init()
class DontShutdown:
def __init__(self, conditions):
self.suspend_enabled = False
self.conditions = conditions
self.icon = gtk.StatusIcon()
self.icon.set_has_tooltip(True)
self.icon.set_from_stock(gtk.STOCK_OK)
self.icon.set_tooltip_text("This is a test")
self.is_xdg = os.environ.has_key('XDG_CURRENT_DESKTOP')
self.XDG_CURRENT_DESKTOP = ""
self.INACTIVITY_ON_AC = -1
self.ps_command = ["/bin/ps","-A","-o","cmd"]
if self.is_xdg:
self.XDG_CURRENT_DESKTOP = os.environ['XDG_CURRENT_DESKTOP'].lower()
self.get_inactive()
self.check_ps()
def enable_suspend(self):
if self.suspend_enabled or not self.is_xdg:
return
if self.XDG_CURRENT_DESKTOP == 'unity':
subprocess.call_checkoutput([
"dconf", "write",
"org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-timeout",
self.INACTIVITY_ON_AC])
elif self.XDG_CURRENT_DESKTOP == 'xfce':
subprocess.call_checkoutput([
"xfconf-query", "-c", "xfce4-power-manager", "-n", "-p",
"/xfce4-power-manager/inactivity-on-ac", "-t", "uint", "-s",
self.INACTIVITY_ON_AC])
self.suspend_enabled = True
def disable_suspend(self):
if not self.suspend_enabled or not self.is_xdg:
return
if self.XDG_CURRENT_DESKTOP == 'unity':
subprocess.call_checkoutput([
"dconf", "write",
"org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-timeout",
"0"])
elif self.XDG_CURRENT_DESKTOP == 'xfce':
subprocess.call_checkoutput([
"xfconf-query", "-c", "xfce4-power-manager", "-n", "-p",
"/xfce4-power-manager/inactivity-on-ac", "-t", "uint", "-s",
"14"]) # with xfce any number under 15 is considered disabled
self.suspend_enabled = False
def get_inactive(self):
if not self.is_xdg:
return
if self.XDG_CURRENT_DESKTOP == 'unity':
self.INACTIVITY_ON_AC = subprocess.check_output([
"dconf", "read",
"/org/gnome/settings-daemon/plugins/power/sleep-inactive-ac-timeout"
]).strip()
if int(self.INACTIVITY_ON_AC) < = 0:
self.suspend_enabled = False
else:
self.suspend_enabled = True
elif self.XDG_CURRENT_DESKTOP == 'xfce':
self.INACTIVITY_ON_AC = subprocess.check_output([
"xfconf-query", "-c", "xfce4-power-manager", "-p",
"/xfce4-power-manager/inactivity-on-ac"]).strip()
if int(self.INACTIVITY_ON_AC) <= 14:
self.suspend_enabled = False
else:
self.suspend_enabled = True
def check_ps(self):
ps_output = subprocess.check_output(self.ps_command)
# print ps_output
msgs = []
for c in self.conditions:
if c.has_key("exists"):
if os.path.exists(c["exists"]):
msgs.append(c["msg"])
if c.has_key("is_running"):
if not c.has_key("re"):
c["re"] = re.compile("("+re.escape(c["is_running"])+")")
res = c["re"].search(ps_output)
if res:
print "res:",res
print "res.groups():",res.groups()
msgs.append(c["msg"])
if not msgs:
self.icon.set_from_stock(gtk.STOCK_YES)
msgs.append("Nothing is happening")
else:
self.icon.set_from_stock(gtk.STOCK_NO)
self.icon.set_tooltip_text("\n".join(msgs))
return True
if __name__ == "__main__":
conditions = [
{
"exists":"/home/erm/.backing_up",
"msg":"Backup is running."
},
{
"is_running":"/usr/lib/openssh/sftp-server",
"msg":"SFTP is being used."
},
{
"is_running":"gst-launch-0.10 pulsesrc device=",
"msg":"Gstreamer is running."
},
{
"is_running":"backup-all-databases.sh",
"msg":"Backing up databases."
},
]
dont_shutdown = DontShutdown(conditions)
gobject.timeout_add(5000, dont_shutdown.check_ps)
try:
gtk.main()
except KeyboardInterrupt:
pass
