Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:tips [2008/11/27 11:58]
a + R E I S U B
linux:tips [2015/05/08 12:09] (current)
zagi
Line 1: Line 1:
 ====== General linux tips and tricks ====== ====== General linux tips and tricks ======
 +
 +
 +===== Limit the CPU usage of an application (process) - cpulimit =====
 +
 +=== Installation: ===
 +
 +Download last stable version of [[http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz|cpulimit]]
 +Then extract the source and compile with make:
 +
 +    tar zxf cpulimit-xxx.tar.gz
 +    cd cpulimit-xxx
 +    make
 +
 +Executable file name is cpulimit. You may want to copy it in /usr/bin.
 +
 +Usage:
 +Limit the process 'bigloop' by executable name to 40% CPU:
 +
 +    cpulimit --exe bigloop --limit 40
 +    cpulimit --exe /usr/local/bin/bigloop --limit 40
 +
 +Limit a process by PID to 55% CPU:
 +
 +    cpulimit --pid 2960 --limit 55 
 +
 +<note tip>
 +If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top. 
 +</note>
 +
 +===== Move a running process into a screen session =====
 +[[http://isteve.bofh.cz/~isteve/knowledgebase/articles/screenify.html]]:
 +<code bash>
 +#!/bin/sh
 +# Copyright Timo Lindfors 2004
 +usage () {
 +    echo usage: $0 pid
 +    exit 1
 +}
 +TCGETS=0x5401
 +TCSETS=0x5402
 +SIZEOF_STRUCT_TERMIOS=60
 +O_RDWR=2
 +((FLAGS=O_RDWR))
 +
 +PID=$1
 +if [ x`which gdb` == x ]; then
 +    echo gdb not found in PATH. Please apt-get install gdb
 +    exit
 +fi
 +if [ x$PID == x ]; then
 +    usage;
 +fi
 +if [ x$2 != x ]; then
 +    usage;
 +fi
 +MYPID=$$
 +MYFD0=`readlink /proc/$MYPID/fd/0`
 +MYFD1=`readlink /proc/$MYPID/fd/1`
 +MYFD2=`readlink /proc/$MYPID/fd/2`
 +EXE=`readlink /proc/$PID/exe`
 +if [ x$EXE == x ]; then
 +    echo $0: $PID: no such pid
 +    exit 1
 +fi
 +
 +BATCHFILE=`mktemp /tmp/gdb.XXXXXXXXXXXX`
 +cat >$BATCHFILE <<EOF
 +file $EXE
 +attach $PID
 +call malloc($SIZEOF_STRUCT_TERMIOS)
 +call malloc($SIZEOF_STRUCT_TERMIOS)
 +call malloc($SIZEOF_STRUCT_TERMIOS)
 +call ioctl(0, $TCGETS, \$1)
 +call ioctl(1, $TCGETS, \$2)
 +call ioctl(2, $TCGETS, \$3)
 +call close(0)
 +call close(1)
 +call close(2)
 +call open("$MYFD0", $FLAGS)
 +call open("$MYFD1", $FLAGS)
 +call open("$MYFD2", $FLAGS)
 +call ioctl(0, $TCSETS, \$1)
 +call ioctl(1, $TCSETS, \$2)
 +call ioctl(2, $TCSETS, \$3)
 +call free(\$1)
 +call free(\$2)
 +call free(\$3)
 +detach
 +EOF
 +gdb -batch -x $BATCHFILE >/dev/null 2>&1 </dev/null
 +rm $BATCHFILE
 +
 +cat <<EOF
 +Process $PID should now be talking to this pty. Refresh the screen
 +(e.g. CTRL+L) and have fun!
 +EOF
 +
 +exec tail -f --pid=$PID /proc/$PID/stat
 +</code>
  
 ===== Force a clean reboot when the system freezes ===== ===== Force a clean reboot when the system freezes =====
Line 19: Line 118:
  
 This will cleanly unmount drives, terminate processes and nicely reboot your machine. This will cleanly unmount drives, terminate processes and nicely reboot your machine.
 +
 +<note important>**If you suspect memory corruption killed your kernel, be careful with Sync option. It may flush corrupted disk buffers over your filesystem and cause more headache after reboot.**</note>
  
 **also for more detailed info see:** [[http://en.wikipedia.org/wiki/Magic_SysRq_key]] **also for more detailed info see:** [[http://en.wikipedia.org/wiki/Magic_SysRq_key]]
Line 65: Line 166:
    echo deadline > /sys/block/sda/queue/scheduler    echo deadline > /sys/block/sda/queue/scheduler
  
-<note>**the above command needs to be run as root, but sudo does not work with it on my systemRun sudo -i if you have problem to get a root prompt**.</note>+<note>**The above command needs to be run as root. If you use sudo, make sure you escape the > with backslash (\), otherwise output of sudo command instead of echo will go to /sys/block/sda/queue/scheduler, and of course it won't be as the root user**.</note>
  
 You can replace sda with the disk you want to change, and deadline with any of the available schedulers. This change is temporary and will be reset when you reboot. You can replace sda with the disk you want to change, and deadline with any of the available schedulers. This change is temporary and will be reset when you reboot.
Line 141: Line 242:
  
 Now when you want to run some disk-intensive process while not suffering too much, but 'LP ' in front of the command. E.g. 'LP updatedb'. Now when you want to run some disk-intensive process while not suffering too much, but 'LP ' in front of the command. E.g. 'LP updatedb'.
 +
 +
 +
 +
 +
 ===== How to clear (and keep empty) the pagecache, dentries and inodes ===== ===== How to clear (and keep empty) the pagecache, dentries and inodes =====
 +
 +<note tip>
 +As this is a non-destructive operation, and dirty objects are not freeable, the user **should run ''"sync"'' first in order to make sure all cached objects are freed**. If you are writing data at the time you run these commands, you’ll actually be dumping the data out of the filesystem cache before it reaches the disk, which could lead to very bad things. \\
 +
 +//This tunable was added in 2.6.16.// </note>
  
    echo 3 > /proc/sys/vm/drop_caches    echo 3 > /proc/sys/vm/drop_caches
  
 Afterwards, echo '0' to this file. Afterwards, echo '0' to this file.
 +
 +**Detailed explanation**
 +
 +  echo # > /proc/sys/vm/drop_caches
 +
 +| **#** | **description** |
 +| 1 | To free pagecache |
 +| 2 | To free dentries and inodes |
 +| 3 | To free pagecache, dentries and inodes |
 +
 +===== Linux memory management =====
 +
 +Memory overcommit can be configured via two parameters:
 +
 +   loreto:/tmp # sysctl -a|grep overcommit
 +   vm.overcommit_ratio = 50
 +   vm.overcommit_memory = 0
 +
 +From Red Hat manual:
 +
 +    * **''overcommit_memory''** — Configures the conditions under which a large memory request is accepted or denied. The following three modes are available:
 +       * **0** — The kernel performs heuristic memory over commit handling by estimating the amount of memory available and failing requests that are blatantly invalid. Unfortunately, since memory is allocated using a heuristic rather than a precise algorithm, this setting can sometimes allow available memory on the system to be overloaded. This is the default setting.
 +       * **1** — The kernel performs no memory over commit handling. Under this setting, the potential for memory overload is increased, but so is performance for memory intensive tasks (such as those executed by some scientific software).
 +       * **2** — The kernel fails requests for memory that add up to all of swap plus the percent of physical RAM specified in ''/proc/sys/vm/overcommit_ratio''. This setting is best for those who desire less risk of memory overcommitment.
 +
 +//**Note** This setting is only recommended for systems with swap areas larger than physical memory.//
 +
 +    * **''overcommit_ratio''** — Specifies the percentage of physical RAM considered when ''/proc/sys/vm/overcommit_memory'' is set to 2. The default value is 50.
 +
 +
 +===== /proc/sys/vm/vfs_cache_pressure =====
 +
 +   * Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects.
 +   * At the default value of vfs_cache_pressure = 100 the kernel will attempt to reclaim dentries and inodes at a "fair" rate with respect to  agecache and swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. Increasing  fs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.
 +   * //In short, values less than 100 won’t reduce the caches very much as all. Values over 100 will signal to the kernel that you want to clear out the caches at a higher priority. I found that no matter what value you use, the kernel clears the caches at a slow rate. I’ve been using a value of 10000 on the server I talked about earlier in the article, and it has kept the caches down to a reasonable level.//
 +
 +
 +
 +
 +
 +===== swappiness (/proc/sys/vm/swappiness) =====
 +
 +   * **swappiness** is a parameter which sets the kernel's balance between reclaiming pages from the page cache and swapping process memory. //The default value is 60//.
 +   * If you want kernel to swap out more process memory and thus cache more file contents increase the value. Otherwise, if you would like kernel to swap less decrease it.
 +   * More info [[http://lwn.net/Articles/83588/|2.6 swapping behavior]]
 +
 +Example:
 +   * ''swappiness=0'' tells the kernel to avoid swapping processes out of physical memory for as long as possible
 +   * ''swappiness=100'' tells the kernel to aggressively swap processes out of physical memory and move them to swap cache
 +   * A value of ''swappiness=10'' is recommended, but feel free to experiment.
  
  
Line 151: Line 312:
  
 In Debian, add ''**nodelay**'' to the line with **pam_unix.so** in **/etc/pam.d/common-auth**. In Debian, add ''**nodelay**'' to the line with **pam_unix.so** in **/etc/pam.d/common-auth**.
 +
  
 ===== Reducing disk-access (and thus saving power) ===== ===== Reducing disk-access (and thus saving power) =====
Line 167: Line 329:
  
    echo 864000 > /proc/sys/vm/dirty_expire_centisecs    echo 864000 > /proc/sys/vm/dirty_expire_centisecs
 +
 +
 +**See also: [[http://www.lesswatts.org/tips/|Tips & Tricks - Saving power with linux]]**
  
 ===== Reduce disk I/O for small reads using memory ===== ===== Reduce disk I/O for small reads using memory =====
Line 195: Line 360:
  
 If you have a reliable server with a good RAID card and power supply, you could set the dirty_ratio to 100 and the dirty_background_ratio to 1. This was recommended by a buddy of mine who runs quite a few servers running virtual machines. If you have a reliable server with a good RAID card and power supply, you could set the dirty_ratio to 100 and the dirty_background_ratio to 1. This was recommended by a buddy of mine who runs quite a few servers running virtual machines.
 +
 +===== convert files from ISO to UTF-8 ====
 +
 +<code> 
 +for x in `find . -name '*.htm'` ; do iconv -f ISO-8859-2 -t UTF-8 $x > "$x.utf8"; rm $x; mv "$x.utf8" $x; done
 +</code>
 +
  
 ===== How do I force users to change their passwords upon first login? ===== ===== How do I force users to change their passwords upon first login? =====
Line 210: Line 382:
    # usermod -U <username>    # usermod -U <username>
  
 +=== space report === 
 +for i in `ls -1 /home`;do du -sh /home/$i|mail $i -a "From: \"Admininistrator\" <root@aufbix.org>" -s "Poraba prostora `date +%d.%m.%y`";done\\
linux/tips.1227783510.txt.gz · Last modified: 2009/05/25 00:34 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready