{"id":47329,"date":"2024-10-06T15:56:56","date_gmt":"2024-10-06T15:56:56","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47329"},"modified":"2024-10-08T14:07:49","modified_gmt":"2024-10-08T14:07:49","slug":"custom-actions-waking-sleep","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/10\/custom-actions-waking-sleep\/","title":{"rendered":"Auto-run Custom Actions When Waking up Your Ubuntu from Sleep"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-39085\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2021\/08\/hibernate-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to automatically run custom command, script, or other actions after resuming your Linux PC\/laptop from suspend or hibernate.<\/p>\n<p>As you may know, there are quite a few ways to run something automatically at Ubuntu startup. You can use &#8220;<i>Startup Applications<\/i>&#8221; utility to run apps or custom commands at login, use crontab to schedule startup jobs, udev rules to auto-configure devices on detection, and <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2024\/05\/auto-create-delete-write-file-startup\/\" target=\"_blank\" rel=\"noopener\">systemd-tmpfiles<\/a> service to auto create, delete, and write configuration files.<\/p>\n<p>But what if your computer does not function properly, or some personal settings do not persist after waking up from sleep?<\/p>\n<p>Without waiting for upstream fixes, you can workaround by creating custom systemd services, to auto-restart the function, or re-configure the settings, and trigger it automatically when waking up from sleep.<\/p>\n<p><!--more--><\/p>\n<p><b>NOTE: not only for Ubuntu, this tutorial should work for most Linux that uses systemd, including Debian, Linux Mint, Fedora, Arch, and their based systems.<\/b><\/p>\n<h3>Create wake up actions for root \/ system wide<\/h3>\n<p><b>1. Create a systemd service file<\/b> by running command below in a terminal (for Ubuntu, press Ctrl+Alt+T) window.<\/p>\n<pre>sudo gnome-text-editor \/etc\/systemd\/system\/fan-resume.service<\/pre>\n<p>Here I use <code>gnome-text-editor<\/code> to create (and edit) a <code>fan-resume.service<\/code> file under <code>\/etc\/systemd\/system<\/code> directory.<\/p>\n<p>You may replace the text editor according to your desktop environment, for example, use <code>mousepad<\/code> for XFCE, <code>pluma<\/code> for MATE, <code>nano<\/code> command line text editor that works in most desktops and server.<\/p>\n<p>Depends on what you want to do, <b>you may replace the file-name <code>fan-resume<\/code><\/b> to whatever that you want. When file opens in text editor, add following lines and edit accordingly:<\/p>\n<pre>[Unit]\r\nDescription=Resume fan speed after waking up\r\nAfter=suspend.target\r\n\r\n[Service]\r\nType=simple\r\nExecStart=\/usr\/bin\/bash -c \"echo level 1 | tee \/proc\/acpi\/ibm\/fan\"\r\n\r\n[Install]\r\nWantedBy=suspend.target<\/pre>\n<p>The lines above tell to trigger the action after waking up from suspend. <b>You may replace the 2 suspend.target<\/b> with:<\/p>\n<ul>\n<li><code>hibernate.target<\/code>,<\/li>\n<li><code>hybrid-sleep.target<\/code>,<\/li>\n<li><code>suspend-then-hibernate.target<\/code>,<\/li>\n<li>or use <code>sleep.target<\/code> that works for resuming from all the sleep states above.<\/li>\n<\/ul>\n<p>When it triggers, it runs <code>echo level 1 | tee \/proc\/acpi\/ibm\/fan<\/code> command in my case in bash shell <b>with root permission<\/b> to set my Thinkpad fan speed to level 1.<\/p>\n<p>You <b>need to replace the value of &#8220;ExecStart&#8221;<\/b> with custom command, script, or other actions, such as restart a service via:<\/p>\n<pre>ExecStart=\/usr\/bin\/systemctl restart xxx.service<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47330\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume-700x447.webp\" alt=\"\" width=\"610\" height=\"390\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume-700x447.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume-300x192.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume-768x491.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/fan-resume.webp 828w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2. Enable the service<\/b>. After saving the file above (for nano, press Ctrl+S to save then Ctrl+X to exit), run the command below to enable the service:<\/p>\n<pre>sudo system enable --now fan-resume.service<\/pre>\n<p>Here, replace the service name <code>fan-resume<\/code> according to what you created.<\/p>\n<p>The <code>--now<\/code> option in the command above will try to run the service action at once. It&#8217;s good for debugging purpose, though you may skip it.<\/p>\n<p>And, run the command below to check the service status.<\/p>\n<pre>sudo system status fan-resume.service<\/pre>\n<p>If the service is loaded and enabled (though inactive) without any error, you can then suspend your computer then resume to see if it functions.<br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/enable-fanspeed-service.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47331\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/enable-fanspeed-service-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/enable-fanspeed-service-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/enable-fanspeed-service-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/enable-fanspeed-service.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>For user only actions:<\/h3>\n<p><b>Some actions need to be run as user rather than root<\/b>. For example, restart a service with <code>--user<\/code> flag, or auto-connect to a VPN network.<\/p>\n<p><b>1.<\/b> In the case, run command in terminal (Ctrl+Alt+T) to create a service file:<\/p>\n<pre>sudo gnome-text-editor \/etc\/systemd\/system\/user-resume@.service<\/pre>\n<p>Here, also replace <code>gnome-text-editor<\/code> accordingly. <b>And, &#8220;@&#8221; is required in the end of file-name.<\/b><\/p>\n<p>When file opens, add similar lines below and edit accordingly.<\/p>\n<pre>[Unit]\r\nDescription=Run my script after 10s delay\r\nAfter=sleep.target\r\n\r\n[Service]\r\nUser=%I\r\nType=simple\r\nExecStartPre=\/usr\/bin\/sleep 10\r\nExecStart=\/usr\/local\/bin\/myscript.sh\r\n\r\n[Install]\r\nWantedBy=sleep.target<\/pre>\n<p>Different to system-wide action, here it includes <b>@<\/b> in file-name and <b>User=%I<\/b> in content under <i>Service<\/i> section. So, the service can be run via specific user by starting <code>user-resume@USER_NAME.service<\/code>.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47333\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume-700x428.webp\" alt=\"\" width=\"610\" height=\"373\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume-700x428.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume-300x183.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume-768x470.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/user-resume.webp 875w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2.<\/b> To run the action for user <b>ji<\/b> only (replace with your username), run command to enable the service:<\/p>\n<pre>systemctl enable --now user-resume@<b>ji<\/b>.service<\/pre>\n<p>Then run <code>systemctl status user-resume@ji.service<\/code> to check if it&#8217;s enabled without error.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47332\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service-700x444.webp\" alt=\"\" width=\"610\" height=\"387\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service-700x444.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service-300x190.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service-768x487.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/10\/start-user-resume-service.webp 802w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>In addition<\/b>, depends on what action you need to do, you may need more options for the custom service. See the <a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/latest\/systemd.service.html\" target=\"_blank\" rel=\"noopener\">official documentation<\/a> for details.<\/p>\n<h3>Undo:<\/h3>\n<p>To undo the things you did above, first open terminal (Ctrl+Alt+T) and run command to disable the service:<\/p>\n<pre>sudo systemctl disable --now xxx.service<\/pre>\n<p>Depends on what service you added and enabled, replace <code>xxx<\/code> in command with the service name or service-name@user-name.<\/p>\n<p>After disabled the service, then delete the corresponding file by running command:<\/p>\n<pre>sudo rm \/etc\/systemd\/system\/xxx.service<\/pre>\n<p>Also replace xxx with the service name you created.<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to automatically run custom command, script, or other actions after resuming your Linux PC\/laptop from suspend or hibernate. As you may know, there are quite a few ways to run something automatically at Ubuntu startup. You can use &#8220;Startup Applications&#8221; utility to run apps or custom commands at login, use crontab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":39085,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[653,2203],"class_list":["post-47329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-hibernate","tag-suspend"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47329"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47329\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/39085"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}