sahinakkaya.dev/feed.xml
2023-02-14 09:03:00 +00:00

690 lines
94 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.2.1">Jekyll</generator><link href="https://sahinakkaya.dev/feed.xml" rel="self" type="application/atom+xml" /><link href="https://sahinakkaya.dev/" rel="alternate" type="text/html" /><updated>2023-02-14T09:02:59+00:00</updated><id>https://sahinakkaya.dev/feed.xml</id><title type="html">Şahin Akkayas Personal Page</title><subtitle>Ş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.</subtitle><author><name>Şahin Akkaya</name></author><entry><title type="html">Hot-Reload Long Running Shell Scripts (feat. trap / kill)</title><link href="https://sahinakkaya.dev/2023/01/15/hot-reloading-with-trap-and-kill.html" rel="alternate" type="text/html" title="Hot-Reload Long Running Shell Scripts (feat. trap / kill)" /><published>2023-01-15T21:48:08+00:00</published><updated>2023-01-15T21:48:08+00:00</updated><id>https://sahinakkaya.dev/2023/01/15/hot-reloading-with-trap-and-kill</id><content type="html" xml:base="https://sahinakkaya.dev/2023/01/15/hot-reloading-with-trap-and-kill.html">&lt;h2 id=&quot;trap-them-and-kill-them&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt; them and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt; them!&lt;/h2&gt;
&lt;p&gt;There is a beautiful command in Linux called &lt;a href=&quot;https://man7.org/linux/man-pages/man1/trap.1p.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt;&lt;/a&gt; which &lt;em&gt;trap&lt;/em&gt;s signals and let you run specific commands when they invoked. There is also good ol &lt;a href=&quot;https://man7.org/linux/man-pages/man1/kill.1.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt;&lt;/a&gt; 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!&lt;/p&gt;
&lt;h3 id=&quot;basic-example&quot;&gt;Basic Example&lt;/h3&gt;
&lt;p&gt;Lets start by creating something very simple and build up from there. Create a script with the following contents:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;My pid is &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;. Send me SIGUSR1!&quot;&lt;/span&gt;
func&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Got SIGUSR1&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# here we are telling that run 'func' when USR1 signal is&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# received. You can run anything. Combine commands with ; etc.&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;trap&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;func&quot;&lt;/span&gt; USR1
&lt;span class=&quot;c&quot;&gt;# The while loop is important here otherwise our script will exit&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# before we manage to get a chance to send a signal.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;waiting SIGUSR1&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sleep &lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now make it executable and run it:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x trap_example
./trap_example
My pid is 2811137. Send me SIGUSR1!
waiting SIGUSR1
waiting SIGUSR1
waiting SIGUSR1
waiting SIGUSR1
waiting SIGUSR1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Open another terminal and send your signal with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt; to the specified pid.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;kill&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; USR1 2811137
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You should receive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Got SIGUSR!&quot;&lt;/code&gt; from the other process. Thats it! Now, imagine you write whatever thing you want to execute in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func&lt;/code&gt; and then you can simply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill -s ...&lt;/code&gt; anytime and as many times you want!&lt;/p&gt;
&lt;p&gt;Lets move the while loop into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func&lt;/code&gt; and add some variables so you can see how powerful this is.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;My pid is &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;. Send me SIGUSR1!&quot;&lt;/span&gt;
func&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;i: &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$((&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sleep &lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;trap&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;echo 'Got SIGUSR1!'; func&quot;&lt;/span&gt; USR1
&lt;span class=&quot;c&quot;&gt;# we need to call the function once, otherwise script&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# will exit before we manage to send a signal&lt;/span&gt;
func
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now run the script and send &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGUSR1&lt;/code&gt;. Here is the result:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ./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
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Isnt this neat?&lt;/p&gt;
&lt;h3 id=&quot;more-useful-example&quot;&gt;More useful example&lt;/h3&gt;
&lt;p&gt;Lets imagine you have multiple long running (infinite loops basically) scripts and you want to restart them without manually searching for their pids and killing them. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt; is for the rescue, again! &lt;sup&gt;&lt;a href=&quot;##&quot; title=&quot;Yeah, I know you can run a systemd service if you want but I think it is an overkill for this situation. Plus, I don't like dealing with them.&quot;&gt;*&lt;/a&gt;&lt;/sup&gt; This command is awesome.&lt;/p&gt;
&lt;p&gt;Without further ado, lets get started. Create a script called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script1&lt;/code&gt; with the following contents:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# file: script1&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello from &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;. i is &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;$((&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sleep &lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And symlink it to another name just for fun:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x script1
&lt;span class=&quot;nb&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; script1 script2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now we can pretend they are two different scripts as their outputs differ:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ./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
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Finally, create the main script which will start child scripts and restart them on our signals:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;My pid is &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;. You know what to do ( ͡° ͜ʖ ͡°)&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;You can also kill me with 'kill -s INT -&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\`&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;pgrep -f &lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;basename&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$0&lt;/span&gt;&lt;span class=&quot;sb&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\`&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;'&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;pids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# we will store the pid's of child scripts here&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;scripts_to_be_executed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./script1&quot;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;./script2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
kill_childs&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# wow, this sounded wild&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;pid &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[@]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;killing &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$pid&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -P: kill all the processes whose parent process is 'pid'&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# see how we are creating processes below&lt;/span&gt;
pkill &lt;span class=&quot;nt&quot;&gt;-P&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$pid&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;done
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;pids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# kill childs and restart all the scripts&lt;/span&gt;
restart_scripts&lt;span class=&quot;o&quot;&gt;(){&lt;/span&gt;
kill_childs
&lt;span class=&quot;c&quot;&gt;# for each script in the list&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;script &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;scripts_to_be_executed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[@]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Run the script and store its pid.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# note the '&amp;amp;' at the end of command. Without it the script will&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# block until its execution is finished. Also we are putting it&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# into braces because we want to create a &quot;process group&quot; so that&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# we can kill all its children later by specifying parent pid&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# (useful if you have pipes (|) or other &amp;amp;'s in your script!)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$script&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &amp;amp;
pids+&lt;span class=&quot;o&quot;&gt;=(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$!&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# we will restart_scripts with SIGUSR1 signal&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;trap&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'echo &quot;restarting scripts&quot;; restart_scripts'&lt;/span&gt; USR1
&lt;span class=&quot;c&quot;&gt;# we will kill all the childs and exit the main script with SIGINT&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# which is same signal as when you press &amp;lt;Control-C&amp;gt; on your terminal&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;trap&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'echo exiting; kill_childs; exit'&lt;/span&gt; INT
&lt;span class=&quot;c&quot;&gt;# run the function once&lt;/span&gt;
restart_scripts
&lt;span class=&quot;c&quot;&gt;# infinite loop, otherwise main script will exit before we send signal.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# remember, we started child processes with '&amp;amp;' so they won't block this script&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sleep &lt;/span&gt;1
&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, you can run your main script and reload your child scripts any time with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;killall main_script -USR1&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Here is an example run:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ./trap_multiple
My pid is 3124123. You know what to do ( ͡° ͜ʖ ͡°)
You can also kill me with 'kill -s INT -`pgrep -f 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
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;final-words&quot;&gt;Final words&lt;/h3&gt;
&lt;p&gt;I think I am started to getting obsessed with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt; 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:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;- How can you see the contents of a file? &lt;br /&gt;
+ You &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt;&lt;/em&gt; it. &lt;br /&gt;
- What if you want to see them in reverse order? &lt;br /&gt;
+ You &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tac&lt;/code&gt;&lt;/em&gt; it. &lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No, it is not just a joke. Try it… Man I love Gnoo slash Linux.&lt;/p&gt;
&lt;p&gt;Anyway, I hope now you know how to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt;. Next week I will explain how to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unzip; strip; touch; finger; grep; mount; fsck; more; yes; fsck; fsck; umount; clean; sleep&lt;/code&gt; &lt;nobr&gt;( ͡° ͜ʖ ͡°)&lt;/nobr&gt;. &lt;sup&gt;&lt;a href=&quot;##&quot; title=&quot;jk :D&quot;&gt;*&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="trap" /><category term="kill" /><category term="linux" /><summary type="html">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!</summary></entry><entry><title type="html">Recap of 2022</title><link href="https://sahinakkaya.dev/2022/12/29/recap-of-2022.html" rel="alternate" type="text/html" title="Recap of 2022" /><published>2022-12-29T20:22:08+00:00</published><updated>2022-12-29T20:22:08+00:00</updated><id>https://sahinakkaya.dev/2022/12/29/recap-of-2022</id><content type="html" xml:base="https://sahinakkaya.dev/2022/12/29/recap-of-2022.html">&lt;p&gt;Its been a while… It has been so long that I forgot how I was writing my blogs back then. My life didnt change that much. Actually, it is getting worse.&lt;/p&gt;
&lt;p&gt;The biggest problem of my life is the graduation project. Oh, God it is making me sick! I simply dont have any interest for the subject I am supposed to work on. One part of me saying that “come on, you came this far. You are nearly finished. One last push!” and other part of me saying “oh no, dont do it. You have never done something you dont like in your entire life. F*ck it!”. So I am wasting my time each term with the dilemma I just described. I really dont know what to do. This thing is fed up.&lt;/p&gt;
&lt;p&gt;Second biggest problem is I live in Turkey. I feel like all my friends somehow get rid of this sh*thole and I am locked here. I use Twitter and Reddit to consume daily news and almost everyday I encounter something that make me say “F*ck me, why I am still here? There is no hope”. Actually, the situation was much worse while I was following pages that shares “street interviews”. At first I started watching them &lt;em&gt;for fun&lt;/em&gt; but the stupidity of people was real and harming my mental health. Since that day, I started consuming only news. My experience got better but I feel like it is still affecting me in a bad manner because everyday something bad happens and there is not much I can do to fix. Today, I decided to delete Twitter and Reddit. Ill see how it goes.&lt;/p&gt;
&lt;p&gt;I am living with my parents for the past 6 months, I break up with my girlfriend, I left the place I was working. Man, this could be the worst year of my life!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/recap-2022/jackie-chan-wtf.jpg&quot; alt=&quot;Jackie Chan WTF meme&quot; /&gt;&lt;/p&gt;
&lt;p&gt;You know what? I am not gonna give up. &lt;em&gt;“… It aint about how hard you get hit. Its about how hard you get hit and keep moving forward. How much you can take, and keep moving forward…”&lt;/em&gt; No, seriously things really will be different for me in 2023 I can feel it. I learn from my mistakes, they are making me even more perfect :D I love myself, I got this.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/recap-2022/i-got-this.jpg&quot; alt=&quot;Disaster girl saying &amp;quot;I got this&amp;quot;&quot; /&gt;&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><summary type="html">Its been a while… It has been so long that I forgot how I was writing my blogs back then. My life didnt change that much. Actually, it is getting worse.</summary></entry><entry><title type="html">Rant: Stop whatever you are doing and learn how licenses work</title><link href="https://sahinakkaya.dev/2022/06/22/rant-on-peoples-reaction-to-copilot.html" rel="alternate" type="text/html" title="Rant: Stop whatever you are doing and learn how licenses work" /><published>2022-06-22T07:46:00+00:00</published><updated>2022-06-22T07:46:00+00:00</updated><id>https://sahinakkaya.dev/2022/06/22/rant-on-peoples-reaction-to-copilot</id><content type="html" xml:base="https://sahinakkaya.dev/2022/06/22/rant-on-peoples-reaction-to-copilot.html">&lt;p&gt;Recently, Github &lt;a href=&quot;https://github.blog/changelog/2022-06-21-github-copilot-is-now-available-to-individual-developers/&quot;&gt;announced&lt;/a&gt;
that they are making Github Copilot available for everyone. Previously, it was in Beta and you could get it through the waiting list.
When I saw the news, I thought I can give it a try. But not so surprisingly it was not free. You have 3 ways to get it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pay the subscription fee and get it.&lt;/li&gt;
&lt;li&gt;Prove you are a student and get it for free.&lt;/li&gt;
&lt;li&gt;Be a maintainer of &lt;em&gt;a popular repository&lt;/em&gt; and get it for free.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I think I should be able to use it for free because I am a student but apparently they are not convinced yet. Anyways, that is a different
story. I dont care if they will give me access to Github Copilot or not. It is not a big deal for me.&lt;/p&gt;
&lt;p&gt;But some people were really angry about how Github Team being vague while defining the criteria as “being a maintainer of a popular open source project”.
I think they are right to some extent. If all you need is having a few thousands stars for a project, you could easily get that. I know a lot of troll
or low effort repositories that get a lot of stars because they are funny.&lt;/p&gt;
&lt;p&gt;Later, I found &lt;a href=&quot;https://twitter.com/fatih/status/1539574219629105156&quot;&gt;another tweet&lt;/a&gt; that explains how Github decides what is &lt;em&gt;popular&lt;/em&gt;. According to
this tweet, if you have a repository that is in top 1000 in one of the most popular 34 languages, you are eligible to get Github Copilot for free.
This is better than the previous definition but you can still argue that it is not fair because one can create a package for checking if a number is
even or not and get thousands of stars.&lt;/p&gt;
&lt;p&gt;You can criticize this, I get that. But do not come up with silly arguments to justify yourself. Like how on earth would you think that Github is doing
something bad because $10/month is too much for this service? It is business man, you pay if you think it is worth it. Thats it. &lt;em&gt;“I joined beta program
and it was free, now they want to charge me if I want to continue using it. They did not tell me that.”&lt;/em&gt; Uhhm… What? Are you aware that what you are using
is another companys service and they have all rights to do whatever they want with their service? How you guys even can build up arguments like that?! This is crazy!&lt;/p&gt;
&lt;p&gt;Some people argue that &lt;em&gt;“what Github is doing is wrong because they used open source projects &lt;strong&gt;without consent&lt;/strong&gt;.”&lt;/em&gt; Another similar argument is that &lt;em&gt;“what
Github is doing is evil because they used projects developed by community and now they are selling it without giving any money to the contributors of
these projects.”&lt;/em&gt; Do you guys even have an idea what licenses stands for? If you dont want to some random person use your code, just license it that
way. And if you licensed it with a GPL compatible or similar license you already gave rights anyone to use or sell your code. That is not Githubs
problem. That is your problem not understanding how licenses work. Stop complaining.&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="copilot" /><category term="license" /><category term="github" /><summary type="html">Recently, Github announced that they are making Github Copilot available for everyone. Previously, it was in Beta and you could get it through the waiting list. When I saw the news, I thought I can give it a try. But not so surprisingly it was not free. You have 3 ways to get it: Pay the subscription fee and get it. Prove you are a student and get it for free. Be a maintainer of a popular repository and get it for free.</summary></entry><entry><title type="html">Confession Time</title><link href="https://sahinakkaya.dev/2022/04/08/confession-time.html" rel="alternate" type="text/html" title="Confession Time" /><published>2022-04-08T15:46:00+00:00</published><updated>2022-04-13T00:00:00+00:00</updated><id>https://sahinakkaya.dev/2022/04/08/confession-time</id><content type="html" xml:base="https://sahinakkaya.dev/2022/04/08/confession-time.html">&lt;h2 id=&quot;a-failure-story&quot;&gt;A failure story&lt;/h2&gt;
&lt;p&gt;Last week, I received an email from &lt;a href=&quot;https://letsencrypt.org/&quot;&gt;Lets Encrypt&lt;/a&gt; reminding me to renew my certificates. I forgot to renew it and the certificate expired. Now I cant send or receive any emails. If you send me email in the last week and wonder why I didnt respond, this is the reason.&lt;/p&gt;
&lt;p&gt;Anyway, I thought it will be easy to fix. Just run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;certbot&lt;/code&gt; again and let him do the job, right? NOPE. It is not that easy. It is just giving me errors with some success messages. If I was not so clueless about what the heck I am doing, I could fix the error. But I dont know anything about how SSL works and it is a shame.&lt;/p&gt;
&lt;p&gt;I dont even know the subject enough to Google it. I feel like I am the only guy in the planet whose certificate is expired. Seriously, how tf I cant find a solution to a such common problem? There was a saying like, &lt;em&gt;“If you cant find something on the internet, there is a high chance that you are being stupid”&lt;/em&gt;. It was not exactly like this but I cant find the original quote either. Argghh…&lt;/p&gt;
&lt;p&gt;If you know the original quote, email me… No, do not email because it does not work. F%ck this thing. F*%k everything. I deserved this. Do not help. If I cant fix this by myself, I should not call myself computer engineer. I am out.&lt;/p&gt;
&lt;h3 id=&quot;update&quot;&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;The problem is fixed. One of my colleagues told me to reboot the server so that it will (&lt;em&gt;possibly&lt;/em&gt;) trigger a script to get a new certificate. I did not think it would work because I already try to get a new certificate manually running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;certbot renew&lt;/code&gt;. And yeah, it didnt change anything but gave me courage to try other &lt;em&gt;dead simple&lt;/em&gt; solutions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;One of them was adding missing MX records for my domain. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;certbot&lt;/code&gt; was telling me that it cant find any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AAAA&lt;/code&gt; records for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www.mail&lt;/code&gt;. I didnt think this is related with my problem because how would I receive emails before then? Anyway, I added the records and the errors are gone. It was only giving me success messages now. Everything seemed to be fine. But I still could not connect to my mail account.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;And here is the solution: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo systemctl restart dovecot&lt;/code&gt;. Kill me. I am &lt;em&gt;guessing&lt;/em&gt; I had to restart the mail service because certificate has changed and it had to pick up the new one. I bet if I had run this command right after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;certbot renew&lt;/code&gt; I would not face any issues. The error messages caused by missing mx records were not related with this problem but I was confused by them and I thought something wrong with my certificates.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Any way, I am happy that it is finally fixed. Did I learn something from this? Not much. But yeah, sometimes all you need is a simple restart :D&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="ssl" /><summary type="html">A failure story Last week, I received an email from Lets Encrypt reminding me to renew my certificates. I forgot to renew it and the certificate expired. Now I cant send or receive any emails. If you send me email in the last week and wonder why I didnt respond, this is the reason.</summary></entry><entry><title type="html">Never Get Trapped in Grub Rescue Again!</title><link href="https://sahinakkaya.dev/2022/03/03/never-get-trapped-in-grub-rescue-again.html" rel="alternate" type="text/html" title="Never Get Trapped in Grub Rescue Again!" /><published>2022-03-03T00:46:00+00:00</published><updated>2022-03-03T00:46:00+00:00</updated><id>https://sahinakkaya.dev/2022/03/03/never-get-trapped-in-grub-rescue-again</id><content type="html" xml:base="https://sahinakkaya.dev/2022/03/03/never-get-trapped-in-grub-rescue-again.html">&lt;p&gt;Anytime I install a new system on my machine, I pray God for nothing bad happens. But it usually happens. When I reboot, I find myself in the “Grub rescue” menu and I dont know how to fix things from that point.&lt;/p&gt;
&lt;p&gt;I go into the live environment and run some random commands that I found on the internet and hope for the best.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/grub-rescue/random-bullshit.jpg&quot; alt=&quot;me trying to fix grub&quot; /&gt;&lt;/p&gt;
&lt;p&gt;What a nice way to shoot myself in the foot! But this time is different. This time, I f*cked up so much that even the random commands on the internet could not help me. I was on my own and I needed to figure out what is wrong with my system. Let me tell you what I did:&lt;/p&gt;
&lt;p&gt;I decided to install another OS just to try it in a real machine. I wanted to shrink one of my partitions to create a space for the new system. I run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fdisk /dev/sdb&lt;/code&gt;, the very first message that it tells me was&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;This disk is currently in use - repartitioning is probably a bad idea. It's recommended to umount all file systems, and swapoff all swap partitions on this disk.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, it just screams &lt;strong&gt;&lt;em&gt;“Do not do it!”&lt;/em&gt;&lt;/strong&gt; but come on. I will not try to shrink the partition I am using (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt;). So it should not be a problem. I ignored the message and shrink it anyway. No problem. Installed and tested the new OS a little bit. Time to reboot and hope for the best. And of course it did not boot. What would I even expecting?&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/grub-rescue/cj.png&quot; alt=&quot;ah sh*t here we go again&quot; /&gt;&lt;/p&gt;
&lt;p&gt;As always, I booted into a live environment and run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;boot-repair&lt;/code&gt; command. It was always working but this time… Even after finishing the operation successfully I could not boot into neither Arch nor Ubuntu (the two systems I had previously).&lt;/p&gt;
&lt;p&gt;Arch was originally mounted in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt; and Ubuntu was in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sda2&lt;/code&gt;. Considering the fact that I only messed with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb&lt;/code&gt;, I should be able to boot Ubuntu, right? Well, yeah. Technically I did boot into Ubuntu but I didnt see the login screen. It was dropping me into something called &lt;em&gt;“Emergency mode”&lt;/em&gt; which just makes me panic! &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo update-grub&lt;/code&gt;… Nope. Nothing changes. Arch does not boot and Ubuntu partially boots.&lt;/p&gt;
&lt;p&gt;Let me tell you what the problem was and how my ignorance made it worse:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;While installing the new system, I saw a partition &lt;strong&gt;labelled&lt;/strong&gt; &lt;em&gt;“Microsoft Basic Data”&lt;/em&gt;. I deleted it thinking it is not required because I dont use W*ndows. It turns out, it was my &lt;em&gt;boot&lt;/em&gt; partition for Arch, just labelled incorrectly… Big lolz :D But we will see this is not even important because I had to rewrite my boot partition anyway.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;My Arch was installed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt;. When I created a new partition and installed the new system, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt; was shifted to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb5&lt;/code&gt; even though I did not ask for it. But the grub configuration to boot my system was still pointing to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt;. That was the reason why Arch does not boot. It was trying to boot from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb3&lt;/code&gt;. So I had to recreate grub configuration and reinstall grub to fix it. I run the following commands that I found &lt;a href=&quot;https://www.jeremymorgan.com/tutorials/linux/how-to-reinstall-boot-loader-arch-linux/&quot;&gt;here&lt;/a&gt; in a live Arch environment:
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; /mnt/arch
mount &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; auto /dev/sdb5 /mnt/arch
arch-chroot /mnt/arch
mount &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; auto /dev/sdb4 /boot/efi
os-prober
grub-mkconfig &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; /boot/grub/grub.cfg
grub-install &lt;span class=&quot;nt&quot;&gt;--efi-directory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/boot/efi &lt;span class=&quot;nt&quot;&gt;--target&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;x86_64-efi /dev/sdb
&lt;span class=&quot;nb&quot;&gt;exit
&lt;/span&gt;reboot
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt;
&lt;p&gt;And it fixed my grub. I can now boot into Arch, hooray!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Ubuntu was not still booting properly. I checked the logs with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;journalctl -xb&lt;/code&gt; and saw something related with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb&lt;/code&gt;. Ubuntu was installed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sda2&lt;/code&gt;, why &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb&lt;/code&gt; should be a problem? Then I remembered something. Back in times when I was using Ubuntu, I was using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb1&lt;/code&gt; as a secondary storage. So I had a configuration where it automatically mounts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb1&lt;/code&gt; on startup. Since I messed with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sdb1&lt;/code&gt; , it was failing to mount it. I opened &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/fstab&lt;/code&gt;, and deleted the related line. Bingo! It started booting properly.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/grub-rescue/hackerman.jpg&quot; alt=&quot;i am something of a hackerman myself&quot; /&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;I started feeling like Hackerman, and I said to myself &lt;em&gt;“You know what, Imma fix everything.”&lt;/em&gt; I had a very sh*tty grub menu with useless grub entries from old systems that I dont use anymore. The UEFI also had the same problem. It had ridiculous amount of boot entries that most of them are just trash.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/grub-rescue/shitty-uefi.jpg&quot; alt=&quot;the pictures i took while trying to figure out which boot options are useless&quot; /&gt;&lt;/p&gt;
&lt;p&gt;These are the pictures I took for reference while trying to figure out which boot options are useless. Sorry for the bad quality. I didnt think I would use them in a blog post.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;While trying to fix the previous problems, Ive spent enough time in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/boot/efi&lt;/code&gt; directory that make me understand where these grub entries are coming from. There were a lot of files belong to old systems. I simply deleted them and updated grub. All of the bad entries were gone. I want to draw your attention here: &lt;em&gt;I did not search for how to delete the unused grub entries. I just knew deleting their directories from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/boot/efi&lt;/code&gt; will do the job. I am doing this sh*t! (Another hackerman moment :D )&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;In order to delete useless boot options from UEFI menu, I used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;efibootmgr&lt;/code&gt;. I searched for it on the internet, of course!
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;efibootmgr &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Check which entries you want to delete, say it is 0003.&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;efibootmgr &lt;span class=&quot;nt&quot;&gt;-b&lt;/span&gt; 0003 &lt;span class=&quot;nt&quot;&gt;-B&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# This will delete third boot option. &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And finally! I know everything about how all these work. Another shady part of Linux is clear for me. Now:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/grub-rescue/quote.jpg&quot; alt=&quot;Give me a ruined computer and an Arch ISO, and I shall fix it for you.&quot; /&gt;&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="linux" /><category term="grub" /><category term="partition" /><category term="uefi" /><summary type="html">Anytime I install a new system on my machine, I pray God for nothing bad happens. But it usually happens. When I reboot, I find myself in the “Grub rescue” menu and I dont know how to fix things from that point.</summary></entry><entry><title type="html">Creating a *Useless* User</title><link href="https://sahinakkaya.dev/2022/02/27/creating-a-useless-user.html" rel="alternate" type="text/html" title="Creating a *Useless* User" /><published>2022-02-27T13:40:00+00:00</published><updated>2022-02-27T13:40:00+00:00</updated><id>https://sahinakkaya.dev/2022/02/27/creating-a-useless-user</id><content type="html" xml:base="https://sahinakkaya.dev/2022/02/27/creating-a-useless-user.html">&lt;h2 id=&quot;story&quot;&gt;Story&lt;/h2&gt;
&lt;p&gt;In my &lt;a href=&quot;/2022/02/26/ssh-into-machine-that-is-behind-private-network.html&quot;&gt;previous post&lt;/a&gt;, I explained how to do port forwarding to access some machine behind private network. I will use this method to fix some issues in our desktop at home or my girlfriends computer. Now, of course I dont want to give them access to my server. But they also need to have a user in my server to be able to perform port forwarding via ssh. So I wanted to create a user with least privileges to make sure nothing goes wrong.&lt;/p&gt;
&lt;h2 id=&quot;the-solution&quot;&gt;The solution&lt;/h2&gt;
&lt;p&gt;I searched the problem in it turned out to be very simple. You just need to add two additional flags to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adduser&lt;/code&gt; command while creating the user.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;adduser uselessuser &lt;span class=&quot;nt&quot;&gt;--shell&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/bin/false &lt;span class=&quot;nt&quot;&gt;--no-create-home&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uselessuser&lt;/code&gt; cant do anything useful in your server. If they try to login, the connection will be closed immediately.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ssh uselessuser@remote.host
uselessuser@remote.host&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;s password:
Could not chdir to home directory /home/uselessuser: No such file or directory
Connection to remote.host closed.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;But they can still do forward the remote port to their local machine.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ssh &lt;span class=&quot;nt&quot;&gt;-Nf&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; 7777:localhost:22 uselessuser@remote.host
uselessuser@remote.host&lt;span class=&quot;se&quot;&gt;\'&lt;/span&gt;s password:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-N&lt;/code&gt; option is the most important one here. From the documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; -N Do not execute a remote command. This is useful
for just forwarding ports. Refer to the description
of SessionType in ssh_config(5) for details.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;last-words&quot;&gt;Last words&lt;/h2&gt;
&lt;p&gt;I love learning new things everyday. I knew setting the shell of a user to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/bin/false&lt;/code&gt; will prevent them from logging in. The reason I wrote this blog post is because 2 things I wanted to share:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;While looking for a solution to the problem I mentioned, I searched &lt;em&gt;“create a user with no privileges in linux”&lt;/em&gt; and &lt;a href=&quot;https://askubuntu.com/questions/1174376/how-to-create-a-user-with-the-least-privileges-permissions-but-enough-to-do-ssh&quot;&gt;this&lt;/a&gt; came out. It is really interesting for me that another person wanted to do the same thing for the &lt;em&gt;exact same reasons&lt;/em&gt;. They were also trying port forwarding via ssh and they wanted to create a limited user in their server to give friends. So the question was a &lt;strong&gt;perfect fit&lt;/strong&gt; to the problem.&lt;/li&gt;
&lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-N&lt;/code&gt; flag of the ssh command was also surprising for me. It was like as if someone had encountered these problems before and just took the exact steps required to solve this problem for me. I mean look at the documentation. Crazy!&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Şahin Akkaya</name></author><category term="linux" /><category term="permissions" /><category term="privileges" /><summary type="html">Story In my previous post, I explained how to do port forwarding to access some machine behind private network. I will use this method to fix some issues in our desktop at home or my girlfriends computer. Now, of course I dont want to give them access to my server. But they also need to have a user in my server to be able to perform port forwarding via ssh. So I wanted to create a user with least privileges to make sure nothing goes wrong.</summary></entry><entry><title type="html">SSH into Machine That Is Behind a Private Network</title><link href="https://sahinakkaya.dev/2022/02/26/ssh-into-machine-that-is-behind-private-network.html" rel="alternate" type="text/html" title="SSH into Machine That Is Behind a Private Network" /><published>2022-02-26T21:40:00+00:00</published><updated>2022-02-26T21:40:00+00:00</updated><id>https://sahinakkaya.dev/2022/02/26/ssh-into-machine-that-is-behind-private-network</id><content type="html" xml:base="https://sahinakkaya.dev/2022/02/26/ssh-into-machine-that-is-behind-private-network.html">&lt;h2 id=&quot;story&quot;&gt;Story&lt;/h2&gt;
&lt;p&gt;I believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.&lt;/p&gt;
&lt;h2 id=&quot;possible-solutions&quot;&gt;Possible solutions&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Just tell the non-tech people at home to configure the router to forward ssh traffic to desktop, right? Well, this is not an option for me, not because people are non-tech, but there is no router! The desktop is connected to internet via hotspot from mobile phone. There is no root access in the phone and even if there was, it is a really big pain to forward the packets manually. Trust me. Been there, done that!&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There are tools like &lt;a href=&quot;https://www.ngrok.com&quot;&gt;ngrok&lt;/a&gt;, &lt;a href=&quot;localtunnel.me&quot;&gt;localtunnel&lt;/a&gt; which exposes your localhost to the internet and gives you a URL to access it but I did not want to use them.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I did not want to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ngrok&lt;/code&gt; because it is not open source and it might have security issues. They are also charging you.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localtunnel&lt;/code&gt; seemed perfect. The code of both client and server is open. That is great news! But it did not last long because it is just forwarding http/https traffic :(&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;
&lt;p&gt;I was thinking of extending the functionality of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localtunnel&lt;/code&gt;, but I learned a very simple way. You dont need any external program to overcome this issue. The good old &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh&lt;/code&gt; can do that! All you need is another machine (a remote server) that both computers can access via ssh.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# local machine (my home computer)&lt;/span&gt;
ssh &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt; 7777:localhost:22 remote-user@remote.host
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This command forwards all the incoming connections to port 7777 of remote machine to port 22 of our current machine. In order for this to work, you need to make sure &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GatewayPorts&lt;/code&gt; is set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt; in the remote server ssh configuration. It also assumes our current machine accepts ssh connections via port 22.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Now, go to any machine and connect to the remote server first. When we are connected, we will create another ssh connection to port 7777 to connect our home computer.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# another local machine (my laptop)&lt;/span&gt;
ssh remote-user@remote.host
&lt;span class=&quot;c&quot;&gt;# connected remote&lt;/span&gt;
ssh &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 7777 homeuser@localhost
&lt;span class=&quot;c&quot;&gt;# we are now connected to home computer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The last two command can also combined so that we directly hop into the home computer.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; remote-user@remote.host ssh &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; 7777 homeuser@localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;
&lt;p&gt;As a result, it only took us 2 simple ssh commands to do this. This is just unbelievable! Now, I need to find a way to make non-tech people at home run this command when there is a problem. Too bad Linux cant help me there :D&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="ssh" /><category term="private-network" /><category term="remote-port-forwarding" /><summary type="html">Story I believe there is always a “tech support person” in every home. Everyone knows that when there is a problem with any electronic device, they should ask this person. I am the tech support in our house. Today, I had to fix a problem in our desktop. Since I was not at home, I had to fix the problem remotely.</summary></entry><entry><title type="html">Using ffmpeg for Simple Video Editing</title><link href="https://sahinakkaya.dev/2022/01/21/ffmpeg-to-rescue.html" rel="alternate" type="text/html" title="Using ffmpeg for Simple Video Editing" /><published>2022-01-21T20:40:00+00:00</published><updated>2022-01-21T20:40:00+00:00</updated><id>https://sahinakkaya.dev/2022/01/21/ffmpeg-to-rescue</id><content type="html" xml:base="https://sahinakkaya.dev/2022/01/21/ffmpeg-to-rescue.html">&lt;h2 id=&quot;story&quot;&gt;Story&lt;/h2&gt;
&lt;p&gt;Today, I have recorded a video for one of my classes and I was required to upload it till midnight. The video was perfect except for a few seconds where I misspelled some words and started again. I had to remove that part from the video before uploading it. Since I was low on time, I thought that I better use a GUI program to do this job. I opened up &lt;a href=&quot;https://kdenlive.org/en/&quot;&gt;Kdenlive&lt;/a&gt; and jumped into editing my video. It was my first time using it so I spent some time to cut and delete the parts that I want to get rid of. When I was ready, I clicked Render button to render my video. It was waaay too slow than I expected. Since I have nothing to do while waiting for render to finish, I thought I could give &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ffmpeg&lt;/code&gt; a shot.&lt;/p&gt;
&lt;h2 id=&quot;let-the-show-begin&quot;&gt;Let the show begin&lt;/h2&gt;
&lt;p&gt;Like Kdenlive, I have never used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ffmpeg&lt;/code&gt; before. Like every normal Linux user do, I opened up a terminal and typed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man ffmpeg&lt;/code&gt; to learn how to use it… Just kidding :D I opened a browser and typed &lt;em&gt;“ffmpeg cut video by time”&lt;/em&gt;. Not the best search query, but it was good enough to find what I am looking for as the &lt;a href=&quot;https://stackoverflow.com/questions/18444194/cutting-the-videos-based-on-start-and-end-time-using-ffmpeg&quot;&gt;first result&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;cutting-the-videos-based-on-start-and-end-time&quot;&gt;Cutting the videos based on start and end time&lt;/h3&gt;
&lt;p&gt;According to answers on the page I mentioned, I run the following commands to cut my video into two parts:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ffmpeg &lt;span class=&quot;nt&quot;&gt;-ss&lt;/span&gt; 00:00:00 &lt;span class=&quot;nt&quot;&gt;-to&lt;/span&gt; 00:01:55 &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; input.mov &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; copy part1.mp4 &lt;span class=&quot;c&quot;&gt;# take from 00:00 to 01:55&lt;/span&gt;
ffmpeg &lt;span class=&quot;nt&quot;&gt;-ss&lt;/span&gt; 00:02:03 &lt;span class=&quot;nt&quot;&gt;-to&lt;/span&gt; 00:05:17 &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; input.mov &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; copy part2.mp4 &lt;span class=&quot;c&quot;&gt;# take from 02:03 to 05:17&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;These two commands run &lt;strong&gt;instantly&lt;/strong&gt;! Kdenlive was still rendering… The progress was 46%. Meh… I said “Duck it, I am gonna use ffmpeg only” and cancelled the rendering.&lt;/p&gt;
&lt;h3 id=&quot;concatenating-the-video-files&quot;&gt;Concatenating the video files&lt;/h3&gt;
&lt;p&gt;Now we have two videos that we want to join. Guess what will be our next search query? &lt;em&gt;“ffmpeg join videos”&lt;/em&gt;. And &lt;a href=&quot;https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg&quot;&gt;here&lt;/a&gt; is the first result:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;file part1.mp4 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; mylist.txt
&lt;span class=&quot;nb&quot;&gt;echo &lt;/span&gt;file part2.mp4 &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; mylist.txt
ffmpeg &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; concat &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; mylist.txt &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; copy result.mp4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And we are DONE! How easy was that? Whole process took about 10 minutes including my search on the internet. If I continued waiting for Kdenlive to finish rendering, I would probably be still waiting at that time. I love the power of command line!&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="cli" /><category term="ffmpeg" /><summary type="html">Story Today, I have recorded a video for one of my classes and I was required to upload it till midnight. The video was perfect except for a few seconds where I misspelled some words and started again. I had to remove that part from the video before uploading it. Since I was low on time, I thought that I better use a GUI program to do this job. I opened up Kdenlive and jumped into editing my video. It was my first time using it so I spent some time to cut and delete the parts that I want to get rid of. When I was ready, I clicked Render button to render my video. It was waaay too slow than I expected. Since I have nothing to do while waiting for render to finish, I thought I could give ffmpeg a shot.</summary></entry><entry><title type="html">Automatically Build and Deploy Your Site using GitHub Actions and Webhooks</title><link href="https://sahinakkaya.dev/2022/01/04/build-and-deploy-automatically.html" rel="alternate" type="text/html" title="Automatically Build and Deploy Your Site using GitHub Actions and Webhooks" /><published>2022-01-04T17:40:00+00:00</published><updated>2022-01-04T17:40:00+00:00</updated><id>https://sahinakkaya.dev/2022/01/04/build-and-deploy-automatically</id><content type="html" xml:base="https://sahinakkaya.dev/2022/01/04/build-and-deploy-automatically.html">&lt;p&gt;In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.&lt;/p&gt;
&lt;h2 id=&quot;introduction-to-github-actions-and-webhooks&quot;&gt;Introduction to GitHub Actions and Webhooks&lt;/h2&gt;
&lt;p&gt;Let me start by explaining what are GitHub Actions and GitHub Webhooks.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Github Actions&lt;/strong&gt; is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Webhooks&lt;/strong&gt; provide a way for notifications to be delivered to an external web server whenever certain actions occur on a repository or organization. … For example, you can configure a webhook to execute whenever:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A repository is pushed to&lt;/li&gt;
&lt;li&gt;A pull request is opened&lt;/li&gt;
&lt;li&gt;A GitHub Pages site is built&lt;/li&gt;
&lt;li&gt;A new member is added to a team&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;defining-the-problem-and-solution&quot;&gt;Defining the problem and solution&lt;/h2&gt;
&lt;p&gt;As I said, my example will be automating the deployment of this site. Here is the normal workflow of me doing it manually:
&lt;img src=&quot;/assets/images/gh-actions-and-webhooks/workflow.png&quot; alt=&quot;My workflow&quot; /&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the only place where my work is really required is writing the post. Other two steps can be automated. We will use GitHub Actions to generate the site content and Webhooks to let our server know about the new content so it can pull the changes. Lets get started.&lt;/p&gt;
&lt;h3 id=&quot;setting-up-github-actions&quot;&gt;Setting up GitHub Actions&lt;/h3&gt;
&lt;p&gt;Setting up a GitHub Action is as easy as creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yml&lt;/code&gt; file in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.github/workflows/&lt;/code&gt; directory in your repository. Let us create a new action to build our site. Fortunately, there is already a &lt;a href=&quot;https://github.com/marketplace/actions/jekyll-actions&quot;&gt;GitHub action&lt;/a&gt; to do it for us. Create a file called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.github/workflows/jekyll.yml&lt;/code&gt; in your root directory of your repository and put the following contents:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Jekyll site CI&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;branches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;pull_request&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;branches&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;jobs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;runs-on&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ubuntu-latest&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;steps&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;actions/checkout@v2&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Jekyll Actions&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;helaili/jekyll-action@2.2.0&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;token&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;${{ secrets.GITHUB_TOKEN }}&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;keep_history&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;target_branch&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;gh-pages'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Thats it! We have created our first Action. When we push this change, GitHub will start building our site and push the result to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gh-pages&lt;/code&gt; branch. Currently, it will take a while to build because we dont use caching. So lets include it to build faster. Add the following piece as a second step:&lt;/p&gt;
&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Use GitHub Actions' cache to shorten build times and decrease load on servers&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;uses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;actions/cache@v2&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;with&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;vendor/bundle&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;${{ runner.os }}-gems-${{ hashFiles('**/Gemfile') }}&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;restore-keys&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;${{ runner.os }}-gems-&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We are done with the Actions part. You can see the final code &lt;a href=&quot;https://github.com/sahinakkaya/sahinakkayadotdev/blob/main/.github/workflows/jekyll.yml&quot;&gt;here&lt;/a&gt;. When you are also done with the code, just push it to trigger the action.&lt;/p&gt;
&lt;h3 id=&quot;setting-up-the-webhook-and-related-endpoint&quot;&gt;Setting up the Webhook and related endpoint&lt;/h3&gt;
&lt;p&gt;Now that we set up our Action to build the site, we need to let our server know about the changes so that it can pull the changes.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-webhook-from-github&quot;&gt;Creating a Webhook from GitHub&lt;/h4&gt;
&lt;p&gt;To add a Webhook, open your repository in browser and navigate to &lt;em&gt;Settings &amp;gt; Webhooks&lt;/em&gt; and click &lt;em&gt;Add Webhook&lt;/em&gt;. Fill in the form with appropriate values. Here is an example:
&lt;img src=&quot;/assets/images/gh-actions-and-webhooks/add-webhook.png&quot; alt=&quot;Webhook form example&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This is all you have to do from GitHub. Now, whenever there is a &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;push&lt;/code&gt;&lt;/em&gt; event to your repository, GitHub will send a POST request to your &lt;em&gt;payload url&lt;/em&gt; with the details.&lt;/p&gt;
&lt;p class=&quot;notice--info&quot;&gt;&lt;strong&gt;Note:&lt;/strong&gt; Our Action is configured to push to a branch in our repository, so it will also trigger this hook and we will catch it.&lt;/p&gt;
&lt;h4 id=&quot;creating-an-endpoint-to-handle-the-requests&quot;&gt;Creating an endpoint to handle the requests&lt;/h4&gt;
&lt;p&gt;I will use &lt;a href=&quot;https://flask.palletsprojects.com/en/2.0.x/&quot;&gt;Flask&lt;/a&gt; framework to handle the post requests coming to our endpoint. You can use whatever programming language or framework you want. It will be very simple code with just one job: Validate the secret keys and run a specific code.&lt;/p&gt;
&lt;p&gt;Lets start by creating a new project and a virtual environment:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mdkir post_receiver
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;post_receiver
python3 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; venv venv
&lt;span class=&quot;nb&quot;&gt;source &lt;/span&gt;venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Install the required packages:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;Flask gunicorn
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Create a new file for storing our environment variables:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# config.py
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;APP_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;your-secret-key&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# same key that is used in github while creating the webhook
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROJECT_PATH&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/path/to/your/project/&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# you will want to cd into this path and perform commands such as git pull etc.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And create the Flask application:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# post_receiver.py
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hmac&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;subprocess&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;flask&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;config&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'POST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'OK'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'POST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;APP_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'utf-8'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;digester&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hmac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;calculated_signature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'sha256='&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;digester&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;actual_signature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'X-Hub-Signature-256'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calculated_signature&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;actual_signature&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;subprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Popen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'./perform-git-pull.sh'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PROJECT_PATH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'OK'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Error'&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'0.0.0.0'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;I will not go into details explaining what each line does. Basically, we are checking if the request is a POST request and if so we are comparing the secret keys to make sure that the request is coming from GitHub. In our case, this is not too important because when the keys match we are running simple git commands in our repository but you might need it if you are doing something more complicated. And here is the contents of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;perform-git-pull.sh&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$1&lt;/span&gt;
git checkout gh-pages
git pull
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We are almost done! All we need to do is create a service to automatically run our code and let nginx handle our endpoint correctly.&lt;/p&gt;
&lt;p&gt;Create a new file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;post_receiver.service&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/systemd/system/&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#/etc/systemd/system/post_receiver.service
# change &amp;lt;user&amp;gt; to your actual username
[Unit]
Description=post_receiver
After=network.target multi-user.target
[Service]
User=&amp;lt;user&amp;gt;
Environment=&quot;PYTHONPATH=/home/&amp;lt;user&amp;gt;/post_receiver/venv/bin/python&quot;
WorkingDirectory=/home/&amp;lt;user&amp;gt;/post_receiver
ExecStart=/home/&amp;lt;user&amp;gt;/post_receiver/venv/bin/gunicorn -b 127.0.0.1:5000 -w 2 --log-file /home/&amp;lt;user&amp;gt;/post_receiver/post_receiver.log post_receiver
[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Make sure port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5000&lt;/code&gt; is reachable from outside.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;ufw allow 5000
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;ufw &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Finally, edit your nginx configuration, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/nginx/sites-available/yoursite&lt;/code&gt;&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location = /postreceive/ {
proxy_pass http://localhost:5000/;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Start, restart the services&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl daemon-reload
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl start post_receiver
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable &lt;/span&gt;post_receiver
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Thats it! &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl https://yourdomain.com/postreceive/&lt;/code&gt; should return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;OK&quot;&lt;/code&gt; and we are ready to accept POST requests from GitHub.&lt;/p&gt;
&lt;h3 id=&quot;notes-for-debugging&quot;&gt;Notes for debugging&lt;/h3&gt;
&lt;p&gt;In case anything goes wrong, here are a few tips to debug:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every GitHub Action produces a log that you can examine. Check them to see if anything is odd.&lt;/li&gt;
&lt;li&gt;In the &lt;em&gt;Webhooks&lt;/em&gt; tab, there is a sub-tab called &lt;em&gt;Recent Deliveries&lt;/em&gt;. You can take a look at there to see the results of the requests from your hooks.&lt;/li&gt;
&lt;li&gt;You can always test your code locally with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt;:
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; curl -i -X POST -H 'Content-Type: application/json' -d '{&quot;foo&quot;: &quot;bar&quot;, &quot;bar&quot;: &quot;baz&quot;}' https://yourdomain.com/postreceive/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Happy hacking!&lt;/p&gt;</content><author><name>Şahin Akkaya</name></author><category term="github-actions" /><category term="github-webhooks" /><category term="ci-cd" /><summary type="html">In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.</summary></entry><entry><title type="html">Stop cat-pipeing, You Are Doing It Wrong!</title><link href="https://sahinakkaya.dev/2022/01/01/stop-cat-pipeing.html" rel="alternate" type="text/html" title="Stop cat-pipeing, You Are Doing It Wrong!" /><published>2022-01-01T15:00:00+00:00</published><updated>2022-01-01T15:00:00+00:00</updated><id>https://sahinakkaya.dev/2022/01/01/stop-cat-pipeing</id><content type="html" xml:base="https://sahinakkaya.dev/2022/01/01/stop-cat-pipeing.html">&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;some_file | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;some_pattern
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Im sure that you run a command something like above at least once if you are using terminal. You know how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; works and you also know what pipe (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;) does. So you naturally combine all of these to make the job done. I was also doing it this way. What I didnt know is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; already accepts file as an argument. So the above command could be rewritten as:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;some_pattern some_file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;… which can make you save a few keystrokes and a few nanoseconds of CPU cycles. Phew! Not a big deal if you are not working files that contains GBs of data, right? I agree but you should still use the latter command because it will help you solve some other problems better. Here is a real life scenario: You want to search for some specific pattern in all the files in a directory.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you use the first approach, you may end up running commands like this:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;
 config.lua  Git.lua  init.lua  markdown.lua  palette.lua  util.lua
 diff.lua  highlights.lua  LSP.lua  Notify.lua  Treesitter.lua  Whichkey.lua
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;config.lua | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;diff.lua | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;Git.lua | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;highlights.lua | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light
Pmenu &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.popup_back &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
CursorLineNr &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, style &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bold&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
Search &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.search_blue &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
IncSearch &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.search_blue &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;init.lua | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light
&lt;span class=&quot;nb&quot;&gt;local &lt;/span&gt;highlights &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; require &lt;span class=&quot;s2&quot;&gt;&quot;onedarker.highlights&quot;&lt;/span&gt;
highlights,
&lt;span class=&quot;c&quot;&gt;# You still have a lot to do :/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;If you use the second approach, you will immediately realize that you can send all the files with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; operator and you will finish the job with just one command (2 if you include mandatory &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; :D):&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;
 config.lua  Git.lua  init.lua  markdown.lua  palette.lua  util.lua
 diff.lua  highlights.lua  LSP.lua  Notify.lua  Treesitter.lua  Whichkey.lua
&lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;light &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
highlights.lua: Pmenu &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.popup_back &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
highlights.lua: CursorLineNr &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, style &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bold&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
highlights.lua: Search &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.search_blue &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
highlights.lua: IncSearch &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.search_blue &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
init.lua:local highlights &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; require &lt;span class=&quot;s2&quot;&gt;&quot;onedarker.highlights&quot;&lt;/span&gt;
init.lua: highlights,
LSP.lua: NvimTreeNormal &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.alt_bg &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
LSP.lua: LirFloatNormal &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray, &lt;span class=&quot;nb&quot;&gt;bg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.alt_bg &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
markdown.lua: markdownIdDelimiter &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
markdown.lua: markdownLinkDelimiter &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; C.light_gray &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;,
palette.lua: light_gray &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;#abb2bf&quot;&lt;/span&gt;,
palette.lua: light_red &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;#be5046&quot;&lt;/span&gt;,
util.lua:local &lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;highlight&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;group, properties&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
util.lua: &lt;span class=&quot;s2&quot;&gt;&quot;highlight&quot;&lt;/span&gt;,
util.lua: highlight&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;group, properties&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Isnt this neat? You might say that &lt;em&gt;“This is cheating! You are using a wild card, of course it will be easier.”&lt;/em&gt; Well, yes. Technically I could use the same wild card in the first command like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat * | grep light&lt;/code&gt; but:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I figured that out only after using wild card in the second command. So I think it is does not feel natural.&lt;/li&gt;
&lt;li&gt;It is still not giving the same output. Try and see the difference! &lt;a href=&quot;##&quot; title=&quot;You will not be able to see which file contains which line. 'cat' will just concatenate all the input.&quot;&gt;*&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Şahin Akkaya</name></author><category term="cat" /><category term="grep" /><category term="linux" /><category term="command-line" /><summary type="html">cat some_file | grep some_pattern Im sure that you run a command something like above at least once if you are using terminal. You know how cat and grep works and you also know what pipe (|) does. So you naturally combine all of these to make the job done. I was also doing it this way. What I didnt know is that grep already accepts file as an argument. So the above command could be rewritten as: grep some_pattern some_file</summary></entry></feed>