{"id":2435,"date":"2018-01-09T13:12:17","date_gmt":"2018-01-09T13:12:17","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2435"},"modified":"2024-01-17T10:56:07","modified_gmt":"2024-01-17T10:56:07","slug":"linux-shell-script-pause-a-process-with-kill-signals","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/linux-shell-script-pause-a-process-with-kill-signals","title":{"rendered":"How to Pause (Suspend) a process in Linux with kill signals"},"content":{"rendered":"\n<p>Linux KILL command gives you killing opportunities to use SIGNALs. In this post we&#8217;re going to Pause (or suspend) and Start an ongoing Linux process.<\/p>\n\n\n\n<p>Problem: The requirement here is to pause a process for certain time and run it only during off hours. The process itself doesn&#8217;t have this option and also we cannot stop it in-between, as it will start from the scratch again.<\/p>\n\n\n\n<p>Solution: To resolve this we can think of sending out signals to the process to hint it to Pause or Progress.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>A signal is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred. When a signal is sent, the operating system interrupts the target process&#8217; normal flow of execution to deliver the signal.<\/p>\n<\/blockquote>\n\n\n\n<!--more-->\n\n\n\n<p>For a command run on terminal we may use ctrl+z to suspend the process.&nbsp; This internally sends the SIGTSTP signal to the foreground process. Which will basically pause the execution of the command and return control to the terminal.<\/p>\n\n\n\n<p>We can later use bg &#8211; to send the execution in background or fg &#8211; to bring the process in foreground for proceeding the execution.<\/p>\n\n\n\n<p>Though here we don&#8217;t have terminal access and we need to work on a different process using script. We will use KILL command with two signals SIGTSTP &amp; SIGCONT to achieve the goal. All we need is the PID of the target process.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SIGTSTP, by default, causes the process to suspend execution. Our Pause.<\/li>\n\n\n\n<li>SIGCONT will continue executing the process, if stopped. That&#8217;s our Start.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">The Script to Pause\/Start Linux Process:<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@nitty-witty sigscripts]# cat .\/signal.sh\nstop() {\nkill -TSTP $1\n}\n\nstart() {\nkill -SIGCONT $1\n}\n\nusage() {\necho \"$1 [stop|start] process_id\"\n}\n\nif [ \"$#\" -eq 0 ]; then\n        usage;\n        exit 1;\nfi\n\necho \"Processing $1 to PID $2\"\nif [ \"$1\" = \"start\" ]; then\n        start $2;\nelif [ \"$1\" = \"stop\" ]; then\n        stop $2;\nelse\n        usage;\nfi;\n[root@nitty-witty sigscripts]#\n<\/pre>\n\n\n\n<p>Sample usage:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[root@nitty-witty sigscripts]# .\/signal.sh\n [stop|start] process_id\n<\/pre>\n\n\n\n<p>Watch video in action:<br><iframe src=\"https:\/\/www.youtube.com\/embed\/HIRBdse7heI\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n\n\n<p>We have used another script which runs a for-loop to print date and counter at certain interval. Here&#8217;s one for your reference.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#contd.sh\necho $ &gt; \/tmp\/mypid\nfor number in {0..10000}\ndo\n     sleep 1\ndone\nexit 0\n<\/pre>\n\n\n\n<p>Ofcourse the script is quiet basic, just to prove the point and share the idea. Hope you liked this small tutorial of how to pause a process using shell script.<\/p>\n","protected":false},"excerpt":{"rendered":"In this post we will see how to pause (suspend) and start a linux process using signals (SIGTSTP and SIGCONT) via kill command.\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[444,59,447,442,105,446,443,445,847],"class_list":{"0":"post-2435","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-kill","8":"tag-linux","9":"tag-pause-a-linux-process","10":"tag-pause-process","11":"tag-shell-script","12":"tag-sigcont","13":"tag-signal","14":"tag-sigtstp","15":"tag-suspend-linux-process"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2435"}],"version-history":[{"count":6,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2435\/revisions"}],"predecessor-version":[{"id":3179,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2435\/revisions\/3179"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}