{"id":6174,"date":"2020-10-21T07:00:44","date_gmt":"2020-10-21T06:00:44","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=6174"},"modified":"2020-10-27T16:32:02","modified_gmt":"2020-10-27T16:32:02","slug":"3-useful-unix-commands-you-might-not-know","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2020\/10\/3-useful-unix-commands-you-might-not-know\/","title":{"rendered":"3 Useful UNIX commands you might not know"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">nohup<\/h2>\n\n\n\n<p>The command <code>nohup<\/code> (stands for &#8220;No hang up&#8221;) allows your script to run even if you quit the terminal. It can be very useful, especially if your terminal has been opened through <code>ssh<\/code> and you have a dodgy connection. It can be used as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nohup python my_script.py > log.out &amp;<\/pre>\n\n\n\n<p>nohup will automatically append the output from your script to a file named <code>nohup.out<\/code>. By adding the <code>&gt; log.out <\/code> part of the command you can save the output to a different file of your choice. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>As <code>nohup<\/code> keeps processes running even after you have closed the terminal, it makes it hard to kill the process once it has started. This can easily be solved using <code>ps<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ps<\/h2>\n\n\n\n<p>The command&nbsp;<code>ps<\/code>&nbsp;(stands for &#8220;process status&#8221;), reports information on current running processes, outputting to standard output. It can be used with the <code>grep<\/code>  command to find the ID of processes started with <code>nohup<\/code> by searching for the name of your script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ps -Af | grep my_script.py<\/pre>\n\n\n\n<p>Once you have the ID of the process you want to stop you can use the <code>kill <\/code>command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">kill 123456<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">tail<\/h2>\n\n\n\n<p>The <code>tail<\/code> command shows the final few lines of a text file. It has a special command line option <code>-f<\/code> (follow) that allows a file to be monitored. This can be handy when used in combination with <code>nohup<\/code>. If you have a process that you want to stay running when you quit the terminal, but you are also interested in its output, you can use the following command.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nohup python -u my_script.py > log.out &amp;\ntail -f log.out<\/pre>\n\n\n\n<p>If you have got this far, I hope this was not a complete waste of your time. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>nohup The command nohup (stands for &#8220;No hang up&#8221;) allows your script to run even if you quit the terminal. It can be very useful, especially if your terminal has been opened through ssh and you have a dodgy connection. It can be used as follows: nohup will automatically append the output from your script [&hellip;]<\/p>\n","protected":false},"author":71,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[296],"tags":[246],"ppma_author":[552],"class_list":["post-6174","post","type-post","status-publish","format-standard","hentry","category-hints-and-tips","tag-unix"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":552,"user_id":71,"is_guest":0,"slug":"brennan","display_name":"Brennan Abanades Kenyon","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/5c85dcbb5b1499e82ecfc264ec387c8302ac238c786e68cc5c92e9c21904d260?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/71"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=6174"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6174\/revisions"}],"predecessor-version":[{"id":6191,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/6174\/revisions\/6191"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=6174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=6174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=6174"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=6174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}