How do I throttle a command in a terminal window?

Solution 1:

man nice, and if you need I/O throttling man ionice.

Let's look at some simple examples.

Throttling CPU usage:

you@box:/$ nice -n 10 /usr/bin/convert blah.gif blah.jpg

From your description, however, CPU usage is almost certainly not your real problem here. More likely, you're having severe I/O contention.

Throttling Disk Usage:

you@box:/$ ionice -c2 -n7 /usr/bin/convert blah.gif blah.jpg

-c2 is "best effort", and -n7 is the lowest "best effort" priority. So, this will throttle the job to lower I/O priority than most other things on the system.

you@box:/$ ionice -c3 /usr/bin/convert blah.gif blah.jpg

-c3 (no priority level necessary) means "Idle only". Jobs set to -c3 ONLY take up otherwise idle disk scheduling time, for virtually no impact at all on the system - but potentially a considerably longer execution time for your job, depending on how busy the rest of the system gets.

Solution 2:

Oh, boy, I just caught this... quoting OP:

Furthermore, even after the process ends, the computer does not return to the previous performance. I found a way around this by running sudo swapoff -a followed by sudo swapon -a

OK, so that means you were exhausting the available RAM on your system, which means you're just plain trying to run too many convert processes at once. We'd need to look at your actual syntax in spawning convert to advise, but basically, you need to make sure you don't try to open more simultaneous processes than you have the RAM to comfortably handle.

Since you state what's causing this is convert *.tif blah.pdf, what's happening is that the content of every single TIF and its conversion to PDF are getting stuffed into RAM at once. What you need to do is split the job up so that this isn't necessary. One possibility that leaps to mind is instead doing something like find . -iname '*.tif' | xargs -I% convert % %.pdf, then using pdftk or something like it to glue all the individual pdfs together. If you really want to get fancy, and you have a multicore CPU, this also affords you the chance to write a small script to run conversions in batches of n, where n is your number of cores, and get the whole thing done significantly faster. :)

pdftk how-to: http://ubuntuhowtos.com/howtos/merge_pdf_files (basically boils down to sudo apt-get install pdftk; pdftk *.pdf cat output merged.pdf)

Solution 3:

Here's a pretty good article on using a program called cpulimit to limit the cpu usage of any process:

http://maketecheasier.com/limit-cpu-usage-of-any-process-in-linux/2010/09/22

The problem is like you said finding the PID of the convert process after it has started.

After starting convert if you can still type commands in the terminal you can run ps aux | grep convert to find it's PID, then sudo cpulimit -p PID -l CPU% to start throttling.

Or if starting convert also locks up your terminal the article shows you how you can run cpulimit as a background daemon that will automatically monitor and throttle any processes over a certain amount of CPU usage. Good luck!