Thursday, November 22, 2007

Stop, Pause, and Continue A Linux Process

On most medium to large scale companies, most demanding production servers I know undergo a few minutes of executing batches of scheduled shell scripts or linux commands. These shell scripts and linux commands consume a lot of time and server work load before finishing certain particular job specially for CPU-cycle-eating scripts and linux commands. Thus lessening server performance and response time to normal daily operations of a non-load-balanced production servers.

How to stop, pause, and continue a currently running process job?
How to create a pending linux shell process?
How to temporarily stop a command?

Here's how to achieve the effect of pausing a linux process.

This blog entry would assume, as an example, running a CPU-cycle-eating linux command, then temporarily pause and stop it, and later on continue executing th process without killing the actual PIDs.

Let's start.

Run updatedb into the background forking a process
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# updatedb &
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Determine current process PIDs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ps axuw | grep updatedb | grep -v grep
root 3264 5.2 0.0 3852 800 pts/0 D 12:35 0:00 updatedb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Noticed from the above lines that the executed updatedb represent a PID of 3264 . Take note of this as we are going to need this when pausing the process.

Next, confirm that the updatedb linux command is still being processing and running
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# jobs
[1]+ Running updatedb &
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, let's pause the linux process
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# kill -STOP 3264
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reconfirm if the process is still running
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# jobs
[1]+ Stopped updatedb

# ps axuw | grep update
root 3264 1.4 0.0 3852 800 pts/0 T 12:35 0:00 updatedb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From the issued commands above, we have successfully paused a currently running process. The updatedb process with PID 3264 is now a currently frozen process. Eventually, depending on CPU cycles and disk activity needed by your scheduled batch of shell scripts and linux commands, server work load would gradually decreased then and much more response time would be available for others to use.


As a sample of stopping ,pausing and continuing a linux process, we can now continue executing the currently stopped linux process:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# kill -CONT 3264
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Verify that the process is now running
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# jobs
[1]+ Running updatedb &

# ps axuw | grep updatedb
root 3272 0.0 0.0 4044 672 pts/0 S+ 12:37 0:00 grep update
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The line below shows that the background process of updatedb was already finished with its job.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[1]+ Done updatedb
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As you can see, the updatedb process was stopped, pause and continued using the 'kill' command.

You might be interested with the shown advertisement from those black boxes around.
HTH

Here is kill description from man page:

The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy