User Tools

Site Tools


linux_notes

High CPU Usage find process:

top -b -n 1 | awk '{if (NR <=7) print; else if ($8 == "D") {print; count++} } END {print "Total status D: "count}'

Find growing files:

touch /tmp/testtempfind; sleep 30; find /home -newer /tmp/testtempfind -size +300k -exec ls -lh {} \;

http://zlib.net/pigz/ A parallel implementation of gzip for modern multi-processor, multi-core machines

Changing file/dir permission recursively (great for fixing apache files and dirs)

find . -type d -print0 | xargs -I {} -0 chmod 0755 {}
find . -type f -print0 | xargs -I {} -0 chmod 0644 {}

Copy using tar

>tar cf - . | (cd /new/path && tar xBf - )

Tune2fs reserved blocks

-l shows status
tune2fs -m 3 /dev/mapper/StorageU01-Vol--u10

To quickly zero a file without having to restart the app:

#> /var/log/logfile


Show current boot flags

cat /proc/cmdline

Setting your prompt:

PS1user="$( test `/usr/bin/whoami` == root && echo '\[\e[101m\]' )\u\[\e[0m\]"; PS1color='\[\e[1;37;44m\]'; PS1="\d - \t\n$PS1user@\h:$PS1color\w\[\e[0m\]$PS1version> "

Rack Prompt

PS1user="$( test /usr/bin/whoami == root && echo '\[\e[101m\]' )\u\[\e[0m\]"; PC=44m; if [ -e /etc/cluster/cluster.conf ];then PC=41m; fi; PS1color='\[\e[1;37;$PC\]'; PS1="\d - \t\n$PS1user@\h:$PS1color\w\[\e[0m\]$PS1version> "; echo " "; w; echo " "; echo " "; df -h

Sun Apr 08–22:57:29

root@info-svr:/tmp>

    \a : an ASCII bell character (07)
    \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
    \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
    \e : an ASCII escape character (033)
    \h : the hostname up to the first '.'
    \H : the hostname
    \j : the number of jobs currently managed by the shell
    \l : the basename of the shell’s terminal device name
    \n : newline
    \r : carriage return
    \s : the name of the shell, the basename of $0 (the portion following the final slash)
    \t : the current time in 24-hour HH:MM:SS format
    \T : the current time in 12-hour HH:MM:SS format
    \@ : the current time in 12-hour am/pm format
    \A : the current time in 24-hour HH:MM format
    \u : the username of the current user
    \v : the version of bash (e.g., 2.00)
    \V : the release of bash, version + patch level (e.g., 2.00.0)
    \w : the current working directory, with $HOME abbreviated with a tilde
    \W : the basename of the current working directory, with $HOME abbreviated with a tilde
    \! : the history number of this command
    \# : the command number of this command
    \$ : if the effective UID is 0, a #, otherwise a $
    \nnn : the character corresponding to the octal number nnn
    \\ : a backslash
    \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
    \] : end a sequence of non-printing characters

Autokey:

Linux

http://code.google.com/p/autokey/

Windows

http://www.autohotkey.com/

Rsync:

wget http://pkgs.repoforge.org/rsync/rsync-3.0.9-1.el5.rfx.x86_64.rpm
rsync -avz --progress --bwlimit=8320 /path/ 192.168.241.225::path/

/etc/rsyncd.conf:

log file = /var/log/rsyncd.log

[CIPDOCBASE]
path=/path
comment= This is a temporary rsync daemon
read only= no
list = yes
uid = root
gid = root
auth users = root
secrets file = /etc/rsyncd.secrets

-----------
/etc/rsyncd.secrets
root:plaintextpassword

make ISO:

dd if=/dev/cdrom of=/vmfs/volumes/rhel6.iso bs=2k

Preserve and copy: </code> cp –preserve -R </code>

Check ntp:

#> ntpdc 
ntpdc> peers
     remote           local      st poll reach  delay   offset    disp
=======================================================================
*min-time-01.ine 10.0.0.15        1 1024  377 0.07047  0.014673 0.14360
=dns-01.esd189.o 10.0.0.15        2 1024  377 0.07587  0.022277 0.13660
ntpdc> exit
DISKSPACE alerts

date ; nice -n 19 find / -mount -type f -size +51200k -ls|sort -rnk7 |head -30|awk '{printf "%d MB\t%s\n",($7/1024)/1024,$NF}'
find / -mount -type f -ls | sort -k 7 -r -n | head -10
lsof |awk '{print $7," ",$9}' |sort -n | uniq
du -sh /var/*
du -sh /home/*
lsof / | sort -rn -k7
du -hx --max-depth=1 /
du -hx --max-depth=1 | grep [0-9]G

These commands allow you to generate CSRs, Certificates, Private Keys, check certs and do other miscellaneous tasks

* Check Certs
openssl x509 -noout -modulus -in server.crt | openssl md5
openssl rsa -noout -modulus -in server.key | openssl md5

* Generate a new private key and Certificate Signing Request
openssl req -out CSR.csr -pubkey -new -keyout privateKey.key

* Generate a certificate signing request (CSR) for an existing private key
 openssl req -out CSR.csr -key privateKey.key -new

* Generate a certificate signing request based on an existing certificate
openssl x509 -x509toreq -in MYCRT.crt -out CSR.csr -signkey privateKey.key

* Generate a self-signed certificate
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout privateKey.key -out certificate.crt

* Remove a passphrase from a private key
openssl rsa -in privateKey.pem -out newPrivateKey.pem

Checking Using OpenSSL
-----------------------------------
If you need to check the information within a Certificate, CSR or Private Key, use these commands.

* Check a Certificate Signing Request (CSR)
openssl req -text -noout -verify -in CSR.csr

* Check a private key
openssl rsa -in privateKey.key -check

* Check a certificate
openssl x509 -in certificate.crt -text -noout

* Check a PKCS#12 file (.pfx or .p12)
openssl pkcs12 -info -in keyStore.p12
openssl rsa -text < key.txt
openssl x509 -text < cert.txt

killing a process by name, replace <process> (without <>)

kill -9 `ps -ef|grep <process>|grep -v grep|awk '{print $2}'`

mysql processlist in real time (every 1s)

while :; do mysql <<< 'SHOW FULL PROCESSLIST;'; sleep 1; clear; done

list files within the current directory by number notation of permissions

ls -lat | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'

bonus:

alias lx='ls -lat | awk '\''{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'\'''

vmstat and sar performance in a neat format

vmstat -a -S m|tail -n1|awk 'BEGIN {FS=" "}{printf \
 "\nAvail\tActive\tTotal\tPercent Avail\n%sMB\t%sMB\t%sMB\t%s\n\n",$4+$5,$6,\
 $4+$5+$6,($4+$5)/($4+$5+$6)*100}';ps -eo user,%cpu,%mem,rsz,args|sort -rnk4| awk 'BEGIN {printf "%8s %6s %6s %8s %-10s\n","USER","%CPU","%MEM","RSZ",\
 "COMMAND"}{printf "%8s %6s %6s %8s MB %-10s\n",$1,$2,$3,$4/1024,$5}'|head  -n10; echo ""; sar -r 2 5; echo ""; sar -u 2 5; echo ""; sar -B 2 5

view the "real time memory" usage

(unset LANG ;sar -r) | awk '{print ($1" - "$4)}'| head -1;echo "Time - Real memory used %"; (unset LANG ;sar -r) | awk '$3~/[0-9]/{total=$3+$2; usedbc=$3-($5+$6); pc_used=(100*usedbc)/total;print $0,pc_used} $3!~/[0-9]/{print $0}' | awk '{print ($1" - "$11)}'| tail -10

find within the current folder and it's subdirectories all the largest files within it, and output to the screen.

find `pwd` -mount -type f -ls|sort -rnk7 |head -30|awk '{printf "%d MB\t%s\n",($7/1024)/1024,$NF}'

find largest directories and files on a server

resize; clear; date; echo "Largest Directories:"; du -hcx / | grep '^[0-9]*G'| sort -nr | head; echo ""; echo "Largest Files:"; nice -n 19 find / -mount -type f -ls|sort -rnk7 |head -10|awk '{printf "%d MB\t%s\n",($7/1024)/1024,$NF}'

show all active connections on port 80 (replace port for additional usage)

netstat -plan | grep :80 | awk  '{print $5}' | sed 's/:.*$//' | sort | uniq -c | sort -rn

full strace for STDIN and STDOUT by PID #

strace -ff -e trace=write -e write=1,2 -p <PID>

watch vmstat in real time

watch vmstat -sSM

watch mysql slave go down every 1 second interval (BORING!)

watch --interval=1 --differences 'mysql -e "show slave status\G"'

pull the slave status and seconds behind master in one

mysql -e "show slave status\G" | egrep 'Slave|Seconds'

% of RAM free MB

free -m | grep + | awk '{ a=$3+$4 ; b=($3/a)*100 ; print b "% of " a " MB" }'

Release INfo for OS

lsb_release -a
linux_notes.txt · Last modified: 2013/11/13 16:29 (external edit)