Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux:tips [2008/11/17 20:30] a |
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:// | ||
+ | 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 ' | ||
+ | |||
+ | cpulimit --exe bigloop --limit 40 | ||
+ | cpulimit --exe / | ||
+ | |||
+ | 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. | ||
+ | </ | ||
+ | |||
+ | ===== Move a running process into a screen session ===== | ||
+ | [[http:// | ||
+ | <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 / | ||
+ | MYFD1=`readlink / | ||
+ | MYFD2=`readlink / | ||
+ | EXE=`readlink / | ||
+ | if [ x$EXE == x ]; then | ||
+ | echo $0: $PID: no such pid | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | BATCHFILE=`mktemp / | ||
+ | cat > | ||
+ | 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(" | ||
+ | call open(" | ||
+ | call open(" | ||
+ | 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 >/ | ||
+ | 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 / | ||
+ | </ | ||
+ | |||
+ | ===== Force a clean reboot when the system freezes ===== | ||
+ | |||
+ | If you manage to freeze your system in such a way that even '' | ||
+ | |||
+ | Hold down the **Right Alt** and **SysRq** keys and press this sequence: | ||
+ | |||
+ | **R E I S U B** | ||
+ | |||
+ | < | ||
+ | R: Switch the keyboard from raw mode to XLATE mode | ||
+ | E: Send the SIGTERM signal to all processes except init | ||
+ | I: Send the SIGKILL signal to all processes except init | ||
+ | S: Sync all mounted filesystems | ||
+ | U: Remount all mounted filesystems in read-only mode | ||
+ | B: Immediately reboot the system, without unmounting partitions or syncing | ||
+ | </ | ||
+ | |||
+ | This will cleanly unmount drives, terminate processes and nicely reboot your machine. | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | **also for more detailed info see:** [[http:// | ||
===== suid, sgid, sticky bit, permissions ===== | ===== suid, sgid, sticky bit, permissions ===== | ||
Line 44: | Line 166: | ||
echo deadline > / | echo deadline > / | ||
- | < | + | < |
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 120: | 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 ''" | ||
+ | |||
+ | //This tunable was added in 2.6.16.// </ | ||
echo 3 > / | echo 3 > / | ||
Afterwards, echo ' | Afterwards, echo ' | ||
+ | |||
+ | **Detailed explanation** | ||
+ | |||
+ | echo # > / | ||
+ | |||
+ | | **#** | **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: | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | From Red Hat manual: | ||
+ | |||
+ | * **'' | ||
+ | * **0** — The kernel performs heuristic memory over commit handling by estimating the amount of memory available and failing requests that are blatantly invalid. Unfortunately, | ||
+ | * **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 ''/ | ||
+ | |||
+ | //**Note** This setting is only recommended for systems with swap areas larger than physical memory.// | ||
+ | |||
+ | * **'' | ||
+ | |||
+ | |||
+ | ===== / | ||
+ | |||
+ | * 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 " | ||
+ | * //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 (/ | ||
+ | |||
+ | * **swappiness** is a parameter which sets the kernel' | ||
+ | * 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:// | ||
+ | |||
+ | Example: | ||
+ | * '' | ||
+ | * '' | ||
+ | * A value of '' | ||
Line 130: | Line 312: | ||
In Debian, add '' | In Debian, add '' | ||
+ | |||
===== Reducing disk-access (and thus saving power) ===== | ===== Reducing disk-access (and thus saving power) ===== | ||
Line 146: | Line 329: | ||
echo 864000 > / | echo 864000 > / | ||
+ | |||
+ | |||
+ | **See also: [[http:// | ||
===== Reduce disk I/O for small reads using memory ===== | ===== Reduce disk I/O for small reads using memory ===== | ||
Line 174: | 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 ==== | ||
+ | |||
+ | < | ||
+ | for x in `find . -name ' | ||
+ | </ | ||
+ | |||
===== 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 189: | Line 382: | ||
# usermod -U < | # usermod -U < | ||
+ | === space report === | ||
+ | for i in `ls -1 /home`;do du -sh / |