Kevin Deldycke - echohttps://kevin.deldycke.com/2010-02-28T12:39:32+01:00System & Shell Commands2010-02-28T12:39:32+01:002010-02-28T12:39:32+01:00Kevin Deldycketag:kevin.deldycke.com,2010-02-28:/2010/02/system-and-shell-commands/<ul> <li> <p>Run a process detached to the current&nbsp;terminal:</p> <div class="highlight"><pre><span></span>$ nohup my_command <span class="p">&amp;</span> </pre></div> </li> <li> <p>Run a process with a shell for a system user which has none (i.e. its default shell is set to <code>/bin/false</code> in <code>/etc/passwd</code>):</p> <div class="highlight"><pre><span></span>$ su sys_user -s /bin/bash -c <span class="s2">&quot;my_command&quot;</span> </pre></div> </li> <li> <p>Get the exit code of the latest …</p></li></ul><ul> <li> <p>Run a process detached to the current&nbsp;terminal:</p> <div class="highlight"><pre><span></span>$ nohup my_command <span class="p">&amp;</span> </pre></div> </li> <li> <p>Run a process with a shell for a system user which has none (i.e. its default shell is set to <code>/bin/false</code> in <code>/etc/passwd</code>):</p> <div class="highlight"><pre><span></span>$ su sys_user -s /bin/bash -c <span class="s2">&quot;my_command&quot;</span> </pre></div> </li> <li> <p>Get the exit code of the latest runned&nbsp;command:</p> <div class="highlight"><pre><span></span>$ <span class="nb">echo</span> <span class="nv">$?</span> </pre></div> </li> <li> <p>Run the last command as <code>root</code> (<a href="https://blog.hardikr.com/post/2337320222/sudo-previous-command">source</a>):</p> <div class="highlight"><pre><span></span>$ sudo !! </pre></div> </li> <li> <p>Show the user under which I&rsquo;m currently logged&nbsp;in:</p> <div class="highlight"><pre><span></span>$ whoami </pre></div> </li> <li> <p>List cron jobs of the current&nbsp;user:</p> <div class="highlight"><pre><span></span>$ crontab -l </pre></div> </li> <li> <p>If you have the following&nbsp;error:</p> <div class="highlight"><pre><span></span>$ -bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory </pre></div> <p>Then the fix consist of removing the bad&nbsp;characters:</p> <div class="highlight"><pre><span></span>$ sed -i <span class="s1">&#39;s/\r//&#39;</span> ./myscript.sh </pre></div> </li> <li> <p>Extract strings from a binary&nbsp;file:</p> <div class="highlight"><pre><span></span>$ strings ./firmware.bin <span class="p">|</span> less </pre></div> </li> <li> <p>Free up some memory by clearing <span class="caps">RAM</span> caches (<a href="https://www.scottklarr.com/topic/134/linux-how-to-clear-the-cache-from-memory/">source</a>):</p> <div class="highlight"><pre><span></span>$ sync <span class="p">;</span> <span class="nb">echo</span> <span class="m">3</span> &gt; /proc/sys/vm/drop_caches </pre></div> </li> <li> <p>Display which distro is running the system (<a href="https://news.ycombinator.com/item?id=1973441">source</a>):</p> <div class="highlight"><pre><span></span>$ lsb_release -a </pre></div> <p>or</p> <div class="highlight"><pre><span></span>$ cat /etc/lsb-release </pre></div> </li> <li> <p>List of most used&nbsp;commands:</p> <div class="highlight"><pre><span></span>$ <span class="nb">history</span> <span class="p">|</span> awk <span class="s1">&#39;{a[$2]++}END{for(i in a){print a[i] &quot; &quot; i}}&#39;</span> <span class="p">|</span> sort -rn <span class="p">|</span> head </pre></div> </li> <li> <p>Disable a service on Debian/Ubuntu, then re-enable&nbsp;it:</p> <div class="highlight"><pre><span></span>$ update-rc.d my-service-name remove $ update-rc.d my-service-name defaults </pre></div> </li> <li> <p>Same thing as above but on a RedHat-like&nbsp;system:</p> <div class="highlight"><pre><span></span>$ chkconfig sshd --del $ chkconfig sshd --add </pre></div> </li> </ul>