Ubuntu – How to throttle a command in a terminal window

cpu loadimage processingmemory usage

I needed to run convert with a lot of images at the same time. The command took quite a while but this doesn't bother me.

The issue is that this command rendered my computer unusable while the command was running (for about 15 minutes).

So is it possible to throttle the command by limiting resources (processor and memory) to the command, directly from the command line? This can only work if I add something to the same line before pressing Enter because once I start the process the computer slows so much that it is impossible for example to switch to "System monitor" and reduce priority.

Edit: top and iotop results

I managed to run top and sudo iotop >iotop.txt while doing one of these convert operations. (The iotop.txt file produced is difficult to read)

Results of top:

   PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND           
14275 username     20   0 4043m 3.0g 1448 D   7.0 80.4   0:16.45 convert 

Results of iotop:

[?1049h[1;24r(B[m[4l[?7h[?1h=[39;49m[?25l[39;49m(B[m[H[2JTotal DISK READ:    1269.04 K/s | Total DISK WRITE:[59G0.00 B/s
(B[0;7m  TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN(B[0;1;7m     IO>(B[0;7m    COMMAND          [3;2H(B[m2516 be/4 username     350.08 K/s    0.00 B/s  0.00 %  0.00 % zeitgeist-datahub
 7394 be/4 username     568.88 K/s    0.00 B/s 77.41 %  0.00 % --rendere~.530483991[5;1H14275 idle username     350.08 K/s    0.00 B/s 37.49 %  0.00 % convert S~f test.pdf[6;2H2048 be/4 root[6;24H0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/3:2]
[5G1 be/4 root[7;24H0.00 B/s    0.00 B/s  0.00 %  0.00 % init

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

Best Answer

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)