{"id":529,"date":"2012-12-07T00:33:18","date_gmt":"2012-12-07T06:33:18","guid":{"rendered":"http:\/\/blog.the-erm.com\/?p=529"},"modified":"2012-12-07T00:36:04","modified_gmt":"2012-12-07T06:36:04","slug":"529","status":"publish","type":"post","link":"https:\/\/blog.the-erm.com\/?p=529","title":{"rendered":"enable\/disable hibernate\/suspend pygtk status icon"},"content":{"rendered":"<p>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&#8217;t want to shutdown during these processes.<\/p>\n<p>So I created a simple status icon to warn me if one of those processes were executing so I wouldn&#8217;t shutdown the machine, and it also will disable hibernate\/suspend.<\/p>\n<p>I figure I&#8217;d share it with the world.<\/p>\n<p><!--more--><\/p>\n<pre style=\"overflow:auto; width:100%; border:1px solid; height:500px;\">#!\/usr\/bin\/env python\r\nimport gobject\r\nimport gtk\r\nimport subprocess\r\nimport os\r\nimport re\r\ngobject.threads_init()\r\n\r\nclass DontShutdown:\r\n    def __init__(self, conditions):\r\n        self.suspend_enabled = False\r\n        self.conditions = conditions\r\n        self.icon = gtk.StatusIcon()\r\n        self.icon.set_has_tooltip(True)\r\n        self.icon.set_from_stock(gtk.STOCK_OK)\r\n        self.icon.set_tooltip_text(\"This is a test\")\r\n        \r\n        self.is_xdg = os.environ.has_key('XDG_CURRENT_DESKTOP')\r\n        self.XDG_CURRENT_DESKTOP = \"\"\r\n        self.INACTIVITY_ON_AC = -1\r\n        self.ps_command = [\"\/bin\/ps\",\"-A\",\"-o\",\"cmd\"]\r\n\r\n        if self.is_xdg:\r\n            self.XDG_CURRENT_DESKTOP = os.environ['XDG_CURRENT_DESKTOP'].lower()\r\n        self.get_inactive()\r\n\r\n        self.check_ps()\r\n\r\n\r\n    def enable_suspend(self):\r\n        if self.suspend_enabled or not self.is_xdg:\r\n            return\r\n\r\n        if self.XDG_CURRENT_DESKTOP == 'unity':\r\n            subprocess.call_checkoutput([\r\n                \"dconf\", \"write\",\r\n                \"org\/gnome\/settings-daemon\/plugins\/power\/sleep-inactive-ac-timeout\", \r\n                self.INACTIVITY_ON_AC])\r\n        elif self.XDG_CURRENT_DESKTOP == 'xfce':\r\n            subprocess.call_checkoutput([\r\n                \"xfconf-query\", \"-c\", \"xfce4-power-manager\", \"-n\", \"-p\", \r\n                \"\/xfce4-power-manager\/inactivity-on-ac\", \"-t\", \"uint\", \"-s\", \r\n                self.INACTIVITY_ON_AC])\r\n\r\n        self.suspend_enabled = True\r\n\r\n    def disable_suspend(self):\r\n        if not self.suspend_enabled or not self.is_xdg:\r\n            return\r\n\r\n        if self.XDG_CURRENT_DESKTOP == 'unity':\r\n            subprocess.call_checkoutput([\r\n                \"dconf\", \"write\",\r\n                \"org\/gnome\/settings-daemon\/plugins\/power\/sleep-inactive-ac-timeout\", \r\n                \"0\"])\r\n        elif self.XDG_CURRENT_DESKTOP == 'xfce':\r\n            subprocess.call_checkoutput([\r\n                \"xfconf-query\", \"-c\", \"xfce4-power-manager\", \"-n\", \"-p\", \r\n                \"\/xfce4-power-manager\/inactivity-on-ac\", \"-t\", \"uint\", \"-s\", \r\n                \"14\"]) # with xfce any number under 15 is considered disabled\r\n\r\n        self.suspend_enabled = False\r\n\r\n    def get_inactive(self):\r\n        if not self.is_xdg:\r\n            return\r\n\r\n        if self.XDG_CURRENT_DESKTOP == 'unity':\r\n            self.INACTIVITY_ON_AC = subprocess.check_output([\r\n                \"dconf\", \"read\",\r\n                \"\/org\/gnome\/settings-daemon\/plugins\/power\/sleep-inactive-ac-timeout\"\r\n                ]).strip()\r\n            if int(self.INACTIVITY_ON_AC) < = 0:\r\n                self.suspend_enabled = False\r\n            else:\r\n                self.suspend_enabled = True\r\n\r\n        elif self.XDG_CURRENT_DESKTOP == 'xfce':\r\n            self.INACTIVITY_ON_AC = subprocess.check_output([\r\n                \"xfconf-query\", \"-c\", \"xfce4-power-manager\", \"-p\",\r\n                \"\/xfce4-power-manager\/inactivity-on-ac\"]).strip()\r\n            if int(self.INACTIVITY_ON_AC) <= 14:\r\n                self.suspend_enabled = False\r\n            else:\r\n                self.suspend_enabled = True\r\n\r\n    def check_ps(self):\r\n        ps_output = subprocess.check_output(self.ps_command)\r\n        # print ps_output\r\n        msgs = []\r\n        for c in self.conditions:\r\n            if c.has_key(\"exists\"):\r\n                if os.path.exists(c[\"exists\"]):\r\n                    msgs.append(c[\"msg\"])\r\n\r\n            if c.has_key(\"is_running\"):\r\n                if not c.has_key(\"re\"):\r\n                    c[\"re\"] = re.compile(\"(\"+re.escape(c[\"is_running\"])+\")\")\r\n                res = c[\"re\"].search(ps_output)\r\n                if res:\r\n                    print \"res:\",res\r\n                    print \"res.groups():\",res.groups()\r\n                    msgs.append(c[\"msg\"])\r\n\r\n        if not msgs:\r\n            self.icon.set_from_stock(gtk.STOCK_YES)\r\n            msgs.append(\"Nothing is happening\")\r\n        else:\r\n            self.icon.set_from_stock(gtk.STOCK_NO)\r\n\r\n        self.icon.set_tooltip_text(\"\\n\".join(msgs))\r\n        return True\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    conditions = [\r\n        {\r\n            \"exists\":\"\/home\/erm\/.backing_up\",\r\n            \"msg\":\"Backup is running.\"\r\n        },\r\n        {\r\n            \"is_running\":\"\/usr\/lib\/openssh\/sftp-server\",\r\n            \"msg\":\"SFTP is being used.\"\r\n        },\r\n        {\r\n            \"is_running\":\"gst-launch-0.10 pulsesrc device=\",\r\n            \"msg\":\"Gstreamer is running.\"\r\n        },\r\n        {\r\n            \"is_running\":\"backup-all-databases.sh\",\r\n            \"msg\":\"Backing up databases.\"\r\n        },\r\n    ]\r\n    dont_shutdown = DontShutdown(conditions)\r\n    gobject.timeout_add(5000, dont_shutdown.check_ps)\r\n    try:\r\n        gtk.main()\r\n    except KeyboardInterrupt:\r\n        pass\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-pygtk"],"_links":{"self":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":6,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}