From 08326b8019d1be3447cab7d9f939620440373df8 Mon Sep 17 00:00:00 2001 From: sahinakkaya Date: Mon, 16 Jan 2023 15:57:32 +0000 Subject: [PATCH] jekyll build from Action f21e5839ed9b95e4837e69015a4cc9039cc7e6f5 --- 2023/01/15/hot-reloading-with-trap-and-kill.html | 14 ++++++++------ assets/js/lunr/lunr-store.js | 2 +- feed.xml | 16 +++++++++------- sitemap.xml | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/2023/01/15/hot-reloading-with-trap-and-kill.html b/2023/01/15/hot-reloading-with-trap-and-kill.html index b93e2fd..a8d9e48 100644 --- a/2023/01/15/hot-reloading-with-trap-and-kill.html +++ b/2023/01/15/hot-reloading-with-trap-and-kill.html @@ -467,12 +467,14 @@ restart_scripts(){ # for each script in the list for script in "${scripts_to_be_executed[@]}" do - # run the script and store its pid. - # '&' at the end of command is important otherwise - # the script will block until its execution is finished. - $script & - pid=$! - pids+=("$pid") + # Run the script and store its pid. + # note the '&' at the end of command. Without it the script will + # block until its execution is finished. Also we are putting it + # into braces because we want to create a process group so that + # killing this pid will end all the grandchilds. + # (useful if you have pipes (|) or other &'s in your script!) + ($script) & + pids+=("$!") done } diff --git a/assets/js/lunr/lunr-store.js b/assets/js/lunr/lunr-store.js index bd4ea9d..75d4d0f 100644 --- a/assets/js/lunr/lunr-store.js +++ b/assets/js/lunr/lunr-store.js @@ -60,7 +60,7 @@ var store = [{ "teaser": null },{ "title": "Hot-Reload Long Running Shell Scripts (feat. trap / kill)", - "excerpt":"trap them and kill them! There is a beautiful command in Linux called trap which traps signals and let you run specific commands when they invoked. There is also good ol’ kill command which not only kills processes but allows you to specify a signal to send. By combining these two, you can run specific functions from your scripts any time! Basic Example Let’s start by creating something very simple and build up from there. Create a script with the following contents: #!/bin/bash echo \"My pid is $$. Send me SIGUSR1!\" func() { echo \"Got SIGUSR1\" } # here we are telling that run 'func' when USR1 signal is # received. You can run anything. Combine commands with ; etc. trap \"func\" USR1 # The while loop is important here otherwise our script will exit # before we manage to get a chance to send a signal. while true ; do echo \"waiting SIGUSR1\" sleep 1 done Now make it executable and run it: ❯ chmod +x trap_example ❯ ./trap_example My pid is 2811137. Send me SIGUSR1! waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 Open another terminal and send your signal with kill to the specified pid. ❯ kill -s USR1 2811137 You should receive \"Got SIGUSR!\" from the other process. That’s it! Now, imagine you write whatever thing you want to execute in func and then you can simply kill -s ... anytime and as many times you want! Let’s move the while loop into the func and add some variables so you can see how powerful this is. #!/bin/bash echo \"My pid is $$. Send me SIGUSR1!\" func() { i=1 while true ; do echo \"i: $i\" i=$(( i + 1 )) sleep 1 done } trap \"echo 'Got SIGUSR1!'; func\" USR1 # we need to call the function once, otherwise script # will exit before we manage to send a signal func Now run the script and send SIGUSR1. Here is the result: ❯ ./trap_example My pid is 2880704. Send me SIGUSR1! i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 Got SIGUSR1! i: 1 i: 2 i: 3 i: 4 i: 5 Got SIGUSR1! i: 1 i: 2 ^C Isn’t this neat? More useful example Let’s imagine you have multiple long running (infinite loops basically) scripts and you want to restart them without manually searching for their pid’s and killing them. trap is for the rescue, again! * This command is awesome. Without further ado, let’s get started. Create a script called script1 with the following contents: #!/bin/bash # file: script1 i=1 while true ; do echo \"Hello from $0. i is $i\" i=$(( i + 1 )) sleep 1 done And symlink it to another name just for fun: ❯ chmod +x script1 ❯ ln -s script1 script2 Now we can pretend they are two different scripts as their outputs differ: ❯ ./script1 Hello from ./script1. i is 1 Hello from ./script1. i is 2 Hello from ./script1. i is 3 Hello from ./script1. i is 4 ^C ❯ ./script2 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script2. i is 3 ^C Finally, create the main script which will start child scripts and restart them on our signals: #!/bin/bash echo \"My pid is $$. You know what to do ( ͡° ͜ʖ ͡°)\" echo \"You can also send me signal with 'killall `basename $0` ...'\" pids=() # we will store the pid's of child scripts here scripts_to_be_executed=(\"./script1\" \"./script2\") kill_childs(){ # wow, this sounded wild for pid in \"${pids[@]}\" do echo killing \"$pid\" kill \"$pid\" done pids=() } # kill childs and restart all the scripts restart_scripts(){ kill_childs # for each script in the list for script in \"${scripts_to_be_executed[@]}\" do # run the script and store its pid. # '&' at the end of command is important otherwise # the script will block until its execution is finished. $script & pid=$! pids+=(\"$pid\") done } # we will restart_scripts with SIGUSR1 signal trap 'echo \"restarting scripts\"; restart_scripts' USR1 # we will kill all the childs and exit the main script with SIGINT # which is same signal as when you press <Control-C> on your terminal trap 'echo exiting; kill_childs; exit' INT # run the function once restart_scripts # infinite loop, otherwise main script will exit before we send signal. # remember, we started child processes with '&' so they won't block this script while true; do sleep 1 done Now, you can run your main script and reload your child scripts any time with killall main_script -USR1 Here is an example run: ❯ ./trap_multiple My pid is 3124123. You know what to do ( ͡° ͜ʖ ͡°) You can also send me signal with 'killall trap_multiple ...' Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script1. i is 2 Hello from ./script2. i is 3 Hello from ./script1. i is 3 restarting scripts killing 3124125 killing 3124126 Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script1. i is 2 Hello from ./script2. i is 3 Hello from ./script1. i is 3 Hello from ./script2. i is 4 Hello from ./script1. i is 4 restarting scripts killing 3124304 killing 3124305 Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script1. i is 2 Hello from ./script2. i is 2 ^Cexiting killing 3124875 killing 3124876 Final words I think I am started to getting obsessed with trap command because it has such a good name and purpose. FOSS people are really on another level when it comes to naming. Here is another good one: - How can you see the contents of a file? + You cat it. - What if you want to see them in reverse order? + You tac it. No, it is not just a joke. Try it… Man I love Gnoo slash Linux. Anyway, I hope now you know how to trap and kill. Next week I will explain how to unzip; strip; touch; finger; grep; mount; fsck; more; yes; fsck; fsck; umount; clean; sleep ( ͡° ͜ʖ ͡°). * ","categories": [], + "excerpt":"trap them and kill them! There is a beautiful command in Linux called trap which traps signals and let you run specific commands when they invoked. There is also good ol’ kill command which not only kills processes but allows you to specify a signal to send. By combining these two, you can run specific functions from your scripts any time! Basic Example Let’s start by creating something very simple and build up from there. Create a script with the following contents: #!/bin/bash echo \"My pid is $$. Send me SIGUSR1!\" func() { echo \"Got SIGUSR1\" } # here we are telling that run 'func' when USR1 signal is # received. You can run anything. Combine commands with ; etc. trap \"func\" USR1 # The while loop is important here otherwise our script will exit # before we manage to get a chance to send a signal. while true ; do echo \"waiting SIGUSR1\" sleep 1 done Now make it executable and run it: ❯ chmod +x trap_example ❯ ./trap_example My pid is 2811137. Send me SIGUSR1! waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 waiting SIGUSR1 Open another terminal and send your signal with kill to the specified pid. ❯ kill -s USR1 2811137 You should receive \"Got SIGUSR!\" from the other process. That’s it! Now, imagine you write whatever thing you want to execute in func and then you can simply kill -s ... anytime and as many times you want! Let’s move the while loop into the func and add some variables so you can see how powerful this is. #!/bin/bash echo \"My pid is $$. Send me SIGUSR1!\" func() { i=1 while true ; do echo \"i: $i\" i=$(( i + 1 )) sleep 1 done } trap \"echo 'Got SIGUSR1!'; func\" USR1 # we need to call the function once, otherwise script # will exit before we manage to send a signal func Now run the script and send SIGUSR1. Here is the result: ❯ ./trap_example My pid is 2880704. Send me SIGUSR1! i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 Got SIGUSR1! i: 1 i: 2 i: 3 i: 4 i: 5 Got SIGUSR1! i: 1 i: 2 ^C Isn’t this neat? More useful example Let’s imagine you have multiple long running (infinite loops basically) scripts and you want to restart them without manually searching for their pid’s and killing them. trap is for the rescue, again! * This command is awesome. Without further ado, let’s get started. Create a script called script1 with the following contents: #!/bin/bash # file: script1 i=1 while true ; do echo \"Hello from $0. i is $i\" i=$(( i + 1 )) sleep 1 done And symlink it to another name just for fun: ❯ chmod +x script1 ❯ ln -s script1 script2 Now we can pretend they are two different scripts as their outputs differ: ❯ ./script1 Hello from ./script1. i is 1 Hello from ./script1. i is 2 Hello from ./script1. i is 3 Hello from ./script1. i is 4 ^C ❯ ./script2 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script2. i is 3 ^C Finally, create the main script which will start child scripts and restart them on our signals: #!/bin/bash echo \"My pid is $$. You know what to do ( ͡° ͜ʖ ͡°)\" echo \"You can also send me signal with 'killall `basename $0` ...'\" pids=() # we will store the pid's of child scripts here scripts_to_be_executed=(\"./script1\" \"./script2\") kill_childs(){ # wow, this sounded wild for pid in \"${pids[@]}\" do echo killing \"$pid\" kill \"$pid\" done pids=() } # kill childs and restart all the scripts restart_scripts(){ kill_childs # for each script in the list for script in \"${scripts_to_be_executed[@]}\" do # Run the script and store its pid. # note the '&' at the end of command. Without it the script will # block until its execution is finished. Also we are putting it # into braces because we want to create a process group so that # killing this pid will end all the grandchilds. # (useful if you have pipes (|) or other &'s in your script!) ($script) & pids+=(\"$!\") done } # we will restart_scripts with SIGUSR1 signal trap 'echo \"restarting scripts\"; restart_scripts' USR1 # we will kill all the childs and exit the main script with SIGINT # which is same signal as when you press <Control-C> on your terminal trap 'echo exiting; kill_childs; exit' INT # run the function once restart_scripts # infinite loop, otherwise main script will exit before we send signal. # remember, we started child processes with '&' so they won't block this script while true; do sleep 1 done Now, you can run your main script and reload your child scripts any time with killall main_script -USR1 Here is an example run: ❯ ./trap_multiple My pid is 3124123. You know what to do ( ͡° ͜ʖ ͡°) You can also send me signal with 'killall trap_multiple ...' Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script1. i is 2 Hello from ./script2. i is 3 Hello from ./script1. i is 3 restarting scripts killing 3124125 killing 3124126 Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script2. i is 2 Hello from ./script1. i is 2 Hello from ./script2. i is 3 Hello from ./script1. i is 3 Hello from ./script2. i is 4 Hello from ./script1. i is 4 restarting scripts killing 3124304 killing 3124305 Hello from ./script1. i is 1 Hello from ./script2. i is 1 Hello from ./script1. i is 2 Hello from ./script2. i is 2 ^Cexiting killing 3124875 killing 3124876 Final words I think I am started to getting obsessed with trap command because it has such a good name and purpose. FOSS people are really on another level when it comes to naming. Here is another good one: - How can you see the contents of a file? + You cat it. - What if you want to see them in reverse order? + You tac it. No, it is not just a joke. Try it… Man I love Gnoo slash Linux. Anyway, I hope now you know how to trap and kill. Next week I will explain how to unzip; strip; touch; finger; grep; mount; fsck; more; yes; fsck; fsck; umount; clean; sleep ( ͡° ͜ʖ ͡°). * ","categories": [], "tags": ["trap","kill","linux"], "url": "/2023/01/15/hot-reloading-with-trap-and-kill.html", "teaser": null diff --git a/feed.xml b/feed.xml index a22d7ab..d6ee16c 100644 --- a/feed.xml +++ b/feed.xml @@ -1,4 +1,4 @@ -Jekyll2023-01-16T11:21:01+00:00https://sahinakkaya.dev/feed.xmlŞahin Akkaya’s Personal PageŞahin Akkaya's personal blog - a perfectionist who likes to tinker everything until it is just right. Get ready to find some sweet tips that will boost your productivity and make you fall in love with your computer.Şahin AkkayaHot-Reload Long Running Shell Scripts (feat. trap / kill)2023-01-15T21:48:08+00:002023-01-15T21:48:08+00:00https://sahinakkaya.dev/2023/01/15/hot-reloading-with-trap-and-kill<h2 id="trap-them-and-kill-them"><code class="language-plaintext highlighter-rouge">trap</code> them and <code class="language-plaintext highlighter-rouge">kill</code> them!</h2> +Jekyll2023-01-16T15:57:30+00:00https://sahinakkaya.dev/feed.xmlŞahin Akkaya’s Personal PageŞahin Akkaya's personal blog - a perfectionist who likes to tinker everything until it is just right. Get ready to find some sweet tips that will boost your productivity and make you fall in love with your computer.Şahin AkkayaHot-Reload Long Running Shell Scripts (feat. trap / kill)2023-01-15T21:48:08+00:002023-01-15T21:48:08+00:00https://sahinakkaya.dev/2023/01/15/hot-reloading-with-trap-and-kill<h2 id="trap-them-and-kill-them"><code class="language-plaintext highlighter-rouge">trap</code> them and <code class="language-plaintext highlighter-rouge">kill</code> them!</h2> <p>There is a beautiful command in Linux called <a href="https://man7.org/linux/man-pages/man1/trap.1p.html"><code class="language-plaintext highlighter-rouge">trap</code></a> which <em>trap</em>s signals and let you run specific commands when they invoked. There is also good ol’ <a href="https://man7.org/linux/man-pages/man1/kill.1.html"><code class="language-plaintext highlighter-rouge">kill</code></a> command which not only kills processes but allows you to specify a signal to send. By combining these two, you can run specific functions from your scripts any time!</p> <h3 id="basic-example">Basic Example</h3> @@ -146,12 +146,14 @@ restart_scripts<span class="o">(){</span> <span class="c"># for each script in the list</span> <span class="k">for </span>script <span class="k">in</span> <span class="s2">"</span><span class="k">${</span><span class="nv">scripts_to_be_executed</span><span class="p">[@]</span><span class="k">}</span><span class="s2">"</span> <span class="k">do</span> - <span class="c"># run the script and store its pid.</span> - <span class="c"># '&amp;' at the end of command is important otherwise</span> - <span class="c"># the script will block until its execution is finished.</span> - <span class="nv">$script</span> &amp; - <span class="nv">pid</span><span class="o">=</span><span class="nv">$!</span> - pids+<span class="o">=(</span><span class="s2">"</span><span class="nv">$pid</span><span class="s2">"</span><span class="o">)</span> + <span class="c"># Run the script and store its pid.</span> + <span class="c"># note the '&amp;' at the end of command. Without it the script will</span> + <span class="c"># block until its execution is finished. Also we are putting it</span> + <span class="c"># into braces because we want to create a process group so that</span> + <span class="c"># killing this pid will end all the grandchilds.</span> + <span class="c"># (useful if you have pipes (|) or other &amp;'s in your script!)</span> + <span class="o">(</span><span class="nv">$script</span><span class="o">)</span> &amp; + pids+<span class="o">=(</span><span class="s2">"</span><span class="nv">$!</span><span class="s2">"</span><span class="o">)</span> <span class="k">done</span> <span class="o">}</span> diff --git a/sitemap.xml b/sitemap.xml index 60275eb..68248ef 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -55,6 +55,6 @@ https://sahinakkaya.dev/assets/docs/resume.pdf -2023-01-16T11:20:54+00:00 +2023-01-16T15:57:23+00:00