Nov 102013
 

linux-cpu

Linux is much better at multitasking processor-intensive tasks than Windows. I remember how virus scanning used to make by old Windows PC almost unusable. Linux is much better, but sometimes bad things happens ! Perhaps a plugin of your browser is using all the CPU, or some bad software is freezing your system, or Apache it’s eating up all your resources on your server.

If you have seen some situation like these, don’t worry anymore, you don’t need to kill all the offending processes, restart your graphical session, or even worse restart the computer, you can simply put the specific process in “PAUSE” and analyse the situation, in some cases you could find the cause of a poor performing process, or just restart it in a second moment, maybe after you have saved all your works, when you can give to that process all the CPU.



To explain how to use the STOP signal to pause a specific process and the CONT signal to restart it I’ll show you a simple example that you can easily replicate on your Linux if you want.
Open a terminal as root and run the command updatedb into the background forking a process

# updatedb &

Now, determine the current process PIDs with the command ps

# 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 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.

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.

An easier way

Sometimes you want to stop all processes running a specified command, in this case the killall command can help you in make things much easier, so to stop all the updatedb processes you can use the command

killall -STOP updatedb

To restart it, you can use once again killall:

killall -CONT updatedb

Or if you want to make it even easier, you can insert these lines of code in the file .bashrc of your user, or root:

pause() { 
if $(killall -STOP "$1"); then 
echo "[+] $1 successfully paused!"
else 
echo "[-] Failed to pause $1"
fi 
}
 
unpause() { 
if $(killall -CONT "$1"); then 
echo "[+] $1 successfully unpaused!" 
else 
echo "[-] Failed to unpause $1" 
fi 
}

And now on the terminal you can use the commands:

pause updatedb

and

unpause updatedb

Reference:

Comment mettre en pause une application et économiser un peu de CPU ?
Stop, Pause, and Continue A Linux Process

Popular Posts:

Flattr this!

  One Response to “How to put in pause any process in Linux”

  1. thank you for this nice 2 functions

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*