{"id":285,"date":"2023-02-08T17:58:01","date_gmt":"2023-02-08T22:58:01","guid":{"rendered":"https:\/\/daveking.com\/blog\/?p=285"},"modified":"2023-02-08T17:59:57","modified_gmt":"2023-02-08T22:59:57","slug":"keeping-my-oracle-always-free-instance-alive","status":"publish","type":"post","link":"https:\/\/daveking.com\/blog\/index.php\/2023\/02\/08\/keeping-my-oracle-always-free-instance-alive\/","title":{"rendered":"Keeping My Oracle Always Free Instance Alive"},"content":{"rendered":"\n<p>When you sign up for <a href=\"https:\/\/daveking.com\/blog\/index.php\/2022\/11\/15\/free-internet-servers\/\">an Always Free Instance server on the Oracle Cloud<\/a>, they tell you that your server will be shut down if it sits idle for too long.  This doesn&#8217;t mean they delete it, it just means they turn it off as if you&#8217;d issued a shutdown command.  You have to log into the control panel and start it back up.  They do this to save cycles in their cloud.  From their perspective, it&#8217;s expensive to have a bunch of servers just sitting around doing nothing.  From my perspective, I don&#8217;t want my server to shut down.  Having it sitting there waiting to perform an internet search for me whenever I ask it to do so is exactly what I set it up for.  So, is there a way I can make it so that Oracle doesn&#8217;t see my server as being idle?<\/p>\n\n\n\n<p>Oracle&#8217;s <a href=\"https:\/\/docs.oracle.com\/en-us\/iaas\/Content\/FreeTier\/freetier_topic-Always_Free_Resources.htm\">definition of an Always Free Instance being idle<\/a> is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CPU utilization for the 95th percentile is less than 10%<\/li>\n\n\n\n<li>Network utilization is less than 10%<\/li>\n\n\n\n<li>Memory utilization is less than 10%&nbsp;<em>(applies to&nbsp;<a href=\"https:\/\/docs.oracle.com\/en-us\/iaas\/Content\/FreeTier\/freetier_topic-Always_Free_Resources.htm#Details_of_the_Always_Free_Compute_instance__a1_flex\">A1 shapes<\/a>&nbsp;only)<\/em><\/li>\n<\/ul>\n\n\n\n<p>The way I read the linked page, all three of those things must be true over a seven-day period for the instance to be identified as being idle.  If I can make just one of them not true, then my instance won&#8217;t get shut down.<\/p>\n\n\n\n<p>So I&#8217;ve written a Python script that will make sure that the CPU utilization on my server is always above 10%.  It dynamically adjusts depending on what is happening on the system.  When other processes are using lots of CPU,  the script reduces the load it puts on the system, and vice versa.  I&#8217;ve set things up so that this script is run as a systemd service so that it automatically starts at boot and is restarted if it ever fails.<\/p>\n\n\n\n<p>After this script is installed and running the instance&#8217;s actual system load for the 95th percentile can be seen in the metrics graphs on the instance details page of the Oracle Cloud console.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">#!\/usr\/bin\/env python3\n\n#  Place enough load on system to keep load average over 10%\n#\n#  Oracle stops any Always Free instances that are running at less\n#  than 10% CPU utilization in the 95th percentile over seven days.\n\nimport time\nimport psutil\nimport logging\nlogging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)\n\n#  How often should we write a log message?\nlog_interval = 300   # seconds\n\n#  Arbitrary starting value based on trial and error, in nanoseconds\nloop_interval = 230000000 \ntry:\n    log_timer = time.time()\n    while True:\n        #  Go into a hard loop for an adjustable period of time\n        timeout = time.perf_counter_ns() + loop_interval\n        while time.perf_counter_ns() &lt; timeout:\n            pass\n        #  Adjust the period of time up or down to acheive the desired CPU utilization\n        cpu_percent = psutil.cpu_percent()\n        if time.time() &gt; log_timer + log_interval:\n            log_timer = time.time()\n            logging.info('CPU at {}% with an interval of {} nanoseconds'.format(cpu_percent, loop_interval))\n        if cpu_percent &gt; 13:\n            loop_interval = loop_interval - 10000000\n        if cpu_percent &lt; 12:\n            loop_interval = loop_interval + 10000000\n        #  Sleep long enough to allow for interupts\n        time.sleep(0.25)\nexcept KeyboardInterrupt:\n    logging.info('keyboard interrupt, terminating')\n    exit()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Preventing my Oracle Always Free Instance from getting shut down because it is seen as being idle.<\/p>\n","protected":false},"author":1,"featured_media":95,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[],"class_list":["post-285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-administration","eq-blocks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/daveking.com\/blog\/wp-content\/uploads\/2022\/11\/free-vps-hosting-trial.png?fit=347%2C249&ssl=1","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":2,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/285\/revisions\/287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/95"}],"wp:attachment":[{"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daveking.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}