{"id":13597,"date":"2019-04-03T11:27:14","date_gmt":"2019-04-03T11:27:14","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13597"},"modified":"2019-04-03T11:27:14","modified_gmt":"2019-04-03T11:27:14","slug":"how-to-auto-execute-commands-scripts-during-reboot-or-startup","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/03\/how-to-auto-execute-commands-scripts-during-reboot-or-startup\/","title":{"rendered":"How to Auto Execute Commands\/Scripts During Reboot or Startup"},"content":{"rendered":"<p>I am always fascinated by the things going on behind the scenes when I\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-boot-process\/\" target=\"_blank\" rel=\"noopener\">boot a Linux system and log on<\/a>. By pressing the power button on a bare metal or starting a virtual machine, you put in motion a series of events that lead to a fully-functional system \u2013 sometimes in less than a minute. The same is true when you log off and \/ or shutdown the system.<\/p>\n<p>What makes this more interesting and fun is the fact that you can have the operating system execute certain actions when it boots and when you logon or logout.<\/p>\n<p>In this distro-agnostic article we will discuss the traditional methods for accomplishing these goals in Linux.<\/p>\n<p><strong>Note<\/strong>: We will assume the use of\u00a0<strong>Bash<\/strong>\u00a0as main shell for logon and logout events. If you happen to use a different one, some of these methods may or may not work. If in doubt, refer to the documentation of your shell.<\/p>\n<h3>Executing Linux Scripts During Reboot or Startup<\/h3>\n<p>There are two traditional methods to execute a command or run scripts during startup:<\/p>\n<h4>Method #1 \u2013 Use a cron Job<\/h4>\n<p>Besides the usual format (minute \/ hour \/ day of month \/ month \/ day of week) that is widely used to indicate a schedule,\u00a0<a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">cron scheduler<\/a>\u00a0also allows the use of\u00a0<code>@reboot<\/code>. This directive, followed by the absolute path to the script, will cause it to run when the machine boots.<\/p>\n<p>However, there are two caveats to this approach:<\/p>\n<ol>\n<li><strong>a)<\/strong>\u00a0the cron daemon must be running (which is the case under normal circumstances), and<\/li>\n<li><strong>b)<\/strong>\u00a0the script or the crontab file must include the environment variables (if any) that will be needed (refer to this StackOverflow thread for more details).<\/li>\n<\/ol>\n<h4>Method #2 \u2013 Use \/etc\/rc.d\/rc.local<\/h4>\n<p>This method is valid even for systemd-based distributions. In order for this method to work, you must grant execute permissions to\u00a0<code>\/etc\/rc.d\/rc.local<\/code>\u00a0as follows:<\/p>\n<pre># chmod +x \/etc\/rc.d\/rc.local\r\n<\/pre>\n<p>and add your script at the bottom of the file.<\/p>\n<p>The following image shows how to run two sample scripts (<code>\/home\/gacanepa\/script1.sh<\/code>\u00a0and\u00a0<code>\/home\/gacanepa\/script2.sh<\/code>) using a\u00a0<strong>cron<\/strong>\u00a0job and\u00a0<strong>rc.local<\/strong>, respectively, and their respective results.<\/p>\n<div class=\"code-label\">script1.sh:<\/div>\n<pre>#!\/bin\/bash\r\nDATE=$(date +'%F %H:%M:%S')\r\nDIR=\/home\/gacanepa\r\necho \"Current date and time: $DATE\" &gt; $DIR\/file1.txt\r\n<\/pre>\n<div class=\"code-label\">script2.sh:<\/div>\n<pre>#!\/bin\/bash\r\nSITE=\"Tecmint.com\"\r\nDIR=\/home\/gacanepa\r\necho \"$SITE rocks... add us to your bookmarks.\" &gt; $DIR\/file2.txt\r\n<\/pre>\n<div id=\"attachment_24557\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/02\/Run-Linux-Commands-at-Startup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24557\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/02\/Run-Linux-Commands-at-Startup.png\" alt=\"Run Linux Scripts at Startup\" width=\"657\" height=\"301\" aria-describedby=\"caption-attachment-24557\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24557\" class=\"wp-caption-text\">Run Linux Scripts at Startup<\/p>\n<\/div>\n<p>Keep in mind that both scripts must be granted execute permissions previously:<\/p>\n<pre>$ chmod +x \/home\/gacanepa\/script1.sh\r\n$ chmod +x \/home\/gacanepa\/script2.sh\r\n<\/pre>\n<h3>Executing Linux Scripts at Logon and Logout<\/h3>\n<p>To execute a script at logon or logout, use\u00a0<code>~.bash_profile<\/code>\u00a0and\u00a0<code>~.bash_logout<\/code>, respectively. Most likely, you will need to create the latter file manually. Just drop a line invoking your script at the bottom of each file in the same fashion as before and you are ready to go.<\/p>\n<h5>Summary<\/h5>\n<p>In this article we have explained how to run script at reboot, logon, and logout. If you can think of other methods we could have included here, feel free to use the comment form below to point them out. We look forward to hearing from you!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/auto-execute-linux-scripts-during-reboot-or-startup\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am always fascinated by the things going on behind the scenes when I\u00a0boot a Linux system and log on. By pressing the power button on a bare metal or starting a virtual machine, you put in motion a series of events that lead to a fully-functional system \u2013 sometimes in less than a minute. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/03\/how-to-auto-execute-commands-scripts-during-reboot-or-startup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Auto Execute Commands\/Scripts During Reboot or Startup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13597","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13597"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13597\/revisions"}],"predecessor-version":[{"id":13598,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13597\/revisions\/13598"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}