{"id":27039,"date":"2023-01-11T18:41:11","date_gmt":"2023-01-11T17:41:11","guid":{"rendered":"https:\/\/ekiwi-blog.de\/?p=27039"},"modified":"2023-01-14T11:47:25","modified_gmt":"2023-01-14T10:47:25","slug":"linux-execute-script-on-login","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/27039\/linux-execute-script-on-login\/","title":{"rendered":"Linux: Execute script on login"},"content":{"rendered":"<p>Autostart when logging in to Linux.<\/p>\n<p><!--more--><\/p>\n<p>In my article &#8220;<a href=\"https:\/\/ekiwi-blog.de\/en\/25833\/include-onedrive-access-in-linux-with-rclone\/\">Setting up OneDrive on Linux with Rclone<\/a>&#8220;, we created a script that allowed us to include OneDrive in Linux. This script always had to be executed manually.<\/p>\n<p>In this article, we will look at how to run scripts and commands on Linux when the user logs in. Autostart for Linux.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/27039\/linux-execute-script-on-login\/#Video_description\" >Video description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/27039\/linux-execute-script-on-login\/#Execute_login_script\" >Execute login script<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Video_description\"><\/span>Video description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/Kh5vtwFtb64\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Execute_login_script\"><\/span>Execute login script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We start with our script, which we want to run. The example is about OneDrive, but of course any other scripts and commands can be executed in this way. In the example, our script is called &#8220;onedrive.sh&#8221; and is located in the home folder.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_1.png\" alt=\"\" width=\"787\" height=\"108\" class=\"aligncenter size-full wp-image-27032\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_1.png 787w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_1-300x41.png 300w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_1-768x105.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<p>We can execute commands and scripts at login via the &#8220;.profile&#8221; file in the home folder. We edit the file with an editor of our choice.<\/p>\n<pre>\r\nvim ~\/.profile\r\n<\/pre>\n<p>Here we can simply add our script or further commands by inserting them at the end.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_2.png\" alt=\"\" width=\"593\" height=\"213\" class=\"aligncenter size-full wp-image-27034\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_2.png 593w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_2-300x108.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/p>\n<p>In the example, we run our script &#8220;onedrive.sh&#8221;. It is important that the command is not blocking, i.e. that it terminates. This should be tested before inserting it. This is the case with the OneDrive script, i.e. the execution hangs here. Thus, in the worst case, logging in is no longer possible.<\/p>\n<p>In this case, it helps to add a &#8220;&#038;&#8221; to the end of the command. This executes the command as a separate process. The command no longer blocks and the execution continues directly.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_3.png\" alt=\"\" width=\"318\" height=\"123\" class=\"aligncenter size-full wp-image-27036\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_3.png 318w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/login_linux_3-300x116.png 300w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/p>\n<p>Done! When you log in, the command is now executed and OneDrive on Linux is automatically integrated.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/linux_login_4.png\" alt=\"\" width=\"917\" height=\"323\" class=\"aligncenter size-full wp-image-27030\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/linux_login_4.png 917w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/linux_login_4-300x106.png 300w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/01\/linux_login_4-768x271.png 768w\" sizes=\"auto, (max-width: 917px) 100vw, 917px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>Autostart when logging in to Linux.<\/p>\n","protected":false},"author":1,"featured_media":18426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[1682],"class_list":["post-27039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-linux-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/27039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=27039"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/27039\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/18426"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=27039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=27039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=27039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}