{"id":9434,"date":"2022-08-27T16:12:01","date_gmt":"2022-08-27T16:12:01","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=9434"},"modified":"2022-08-27T16:12:01","modified_gmt":"2022-08-27T16:12:01","slug":"saving-power-on-ubuntu-server","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/saving-power-on-ubuntu-server\/","title":{"rendered":"Saving power on Ubuntu Server"},"content":{"rendered":"<p>I&#8217;ve been meaning to look into this for a while. My Ubuntu server acts as a NAS drive. As such it&#8217;s on all the time, but really it needn&#8217;t be. So let&#8217;s look at what we can do to save some energy by &#8220;hibernating&#8221; late in the evening and restarting early in the morning&#8230;<\/p>\n<p>Let&#8217;s start with the <em>\/usr\/sbin\/rtcwake<\/em> command. This application will tell your Linux OS to go into a sleep\/hibernate state and you supply it with a time to wake back up, for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/usr\/sbin\/rtcwake -m off \r\n<\/pre>\n<p>The -m switch specifies the type of suspend. Options include<\/p>\n<ul>\n<li><strong>standby<\/strong> this option is the least energy efficient option and is the default option if you do not set the -m switch, it&#8217;s also very fast at resuming a system from sleep.<\/li>\n<li><strong>mem<\/strong> this option suspends to RAM, this puts everything into a low powered state (except RAM) and offers significant savings in energy.<\/li>\n<li><strong>disk<\/strong> this option suspends disk operations, the contents of memory are written to disk then restored when the computer wakes up.<\/li>\n<li><strong>off<\/strong> this option turns the computer off completely<\/li>\n<li><strong>no<\/strong> this option allows you to specify the awake time but doesn&#8217;t suspect the computer. The idea is that the user would put the machine into hibernation<br \/>\nand rtcwake would then wake it up again at the specified point in time.<\/li>\n<\/ul>\n<p>We might wish to set the resume time using either seconds (i.e. how many seconds in the future does the system resume) using the -s options, for example to wake up 10 hours after going to sleep we would specify 36000 seconds, like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/usr\/sbin\/rtcwake -m off -s 36000\r\n<\/pre>\n<p>or to specify a time we use the -t switch which requires the number of seconds since Unix epoch (i.e. Jan 1st 1970, 00:00:00) so we still need to calculate to seconds<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/usr\/sbin\/rtcwake -m off -t $(date +%s -d \u2018tomorrow 07:00\u2019)\r\n<\/pre>\n<p>If your hardware clock is set to local time, you&#8217;ll want to use the -l switch, for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/usr\/sbin\/rtcwake -m off -l -t $(date +%s -d \u2018tomorrow 07:00\u2019)\r\n<\/pre>\n<p>Below is a script taken from <a href=\"https:\/\/stackoverflow.com\/questions\/34320964\/how-to-schedule-a-server-start-up\" rel=\"noopener\" target=\"_blank\">StackOverflow<\/a>. Just save this script as wakeup.sh or whatever file name and chmod to executable.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/sh\r\n\r\n# unix time in s that we should wake at\r\nwaketime=$(date +%s -d '07:00')\r\n\r\n# make sure the time we got is in the future\r\nif &#x5B; $(date +%s) -gt $waketime ]; then\r\n\r\n        # if not add a day 60s * 60s * 24h\r\n        waketime=$(($waketime + 86400))\r\nfi\r\n\r\n# time in S from now till then\r\ndif=$(($waketime - $(date +%s)))\r\n\r\n# tell the user\r\necho\r\necho &quot;Current Time      :  $(date +%s)&quot;\r\necho &quot;Wake Time         :  $waketime&quot;\r\necho &quot;Seconds From Now  :  $dif&quot;\r\necho\r\necho &quot;Ctrl-C to Cancel in 5s&quot;\r\necho\r\nsleep 5\r\n\r\n# sleep with rtcwake\r\n\/usr\/sbin\/rtcwake -m off -l -s $dif\r\n\r\necho &quot;Shutdown.&quot;#!\/bin\/sh\r\n<\/pre>\n<p>But wait, we can run this <em>rtcwake<\/em> command (or the above script) manually but for my scenario I&#8217;d really want want it run automatically. So we add a cronjob as sudo (as rtcwake will need to be run as sudo) i.e.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsudo crontab -e\r\n<\/pre>\n<p>Now add the following<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n0 0 * * * \/home\/putridparrot\/wakeup.sh &gt; \/home\/putridparrot\/wakeup.log\r\n<\/pre>\n<p>As per crontab expectations the arguments are as follows<\/p>\n<ul>\n<li><strong>MIN:<\/strong> Minute 0-60<\/li>\n<li><strong>HOUR:<\/strong> Hour, 24 hour clock hence 0-23<\/li>\n<li><strong>MDAY:<\/strong> Day of the Month, 1-31<\/li>\n<li><strong>MON:<\/strong> Month, 1-12 or jan, feb&#8230;dec<\/li>\n<li><strong>DOW:<\/strong> Day of the week, 0-6 or sun, mon, tue&#8230;sat<\/li>\n<li><strong>COMMAND:<\/strong> The command to run<\/li>\n<\/ul>\n<p>So with this knowledge, our script will run at 12:00am every day. I found it useful to create a log of the output of the script, just to ensure I could see it had run. Remember > will create a new file overwriting any existing file or you could use >> to append to an existing file if you prefer (but then you might want a cronjob to delete the file periodically).<\/p>\n<p>You can also check the syslog for CRON jobs via<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ngrep CRON \/var\/log\/syslog\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been meaning to look into this for a while. My Ubuntu server acts as a NAS drive. As such it&#8217;s on all the time, but really it needn&#8217;t be. So let&#8217;s look at what we can do to save some energy by &#8220;hibernating&#8221; late in the evening and restarting early in the morning&#8230; Let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[312],"tags":[],"class_list":["post-9434","post","type-post","status-publish","format-standard","hentry","category-ubuntu-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9434"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9434\/revisions"}],"predecessor-version":[{"id":9448,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9434\/revisions\/9448"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}