计算圆周率评估CPU计算性能

snippet.bash
TIME_START=$(date +'%s.%N'); echo "scale=5000; 4*a(1)" | bc -l -q ; TIME_STOP=$(date +'%s.%N'); echo $(echo "$TIME_STOP - $TIME_START" | bc)

调试python脚本

snippet.bash
python -m pdb my_python.py

使用脚本ftp上传

snippet.bash
ftp_upload()
{
    echo "upload $2 to remote server..."
    up_file_name=$(basename $2)
    remote_dir="/opt/repos/gxdb/ci_node/result_TPCH"
    > ./ftp_up_tar.sh
    echo "ftp -i -n << EOF" >> ./ftp_up_tar.sh
    echo "open 192.168.8.77" >> ./ftp_up_tar.sh
    echo "user yz yz" >> ./ftp_up_tar.sh
    echo "binary" >> ./ftp_up_tar.sh
    echo "cd \$1" >> ./ftp_up_tar.sh
    echo "lcd \$2" >> ./ftp_up_tar.sh
    echo "put \$3" >> ./ftp_up_tar.sh
    echo "exit 0" >> ./ftp_up_tar.sh
    echo "EOF" >> ./ftp_up_tar.sh
    sh -x ./ftp_up_tar.sh "${remote_dir}" "$1" "${up_file_name}"
    echo "Upload end."
}
 
ftp_upload "/opt/remote_dir/" "./test/myfile.txt"

修改使用SWAP交换器使用优先级

查询swappiness:

snippet.bash
[root@yz_dev ~]# cat /proc/sys/vm/swappiness
30

修改swappiness:

snippet.bash
[root@yz_dev ~]# echo 20 > /proc/sys/vm/swappiness

swappiness值越小越优先使用物理内存。

强制磁盘 I/O无缓存

snippet.bash
[root@yz_dev ~]# vi /etc/fstab

磁盘分区加上sync选项

清理系统缓存

snippet.bash
[yz@yz_dev gbase]$ sudo sync
[yz@yz_dev gbase]$ sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

限制Linux系统CPU核数

snippet.bash
[root@gc6541 grub]# vi /boot/grub/grub.conf
snippet.txt
\# grub.conf generated by anaconda
\#
\# Note that you do not have to rerun grub after making changes to this file
\# NOTICE: You have a /boot partition. This means that
\#     all kernel and initrd paths are relative to /boot/, eg.
\#     root (hd0,0)
\#     kernel /vmlinuz-version ro root=/dev/sda3
\#     initrd /initrd-[generic-]version.img
\#boot=/dev/sda
 
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
 
     root (hd0,0)
     kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=f77a0bfb-e971-4afa-9f40-3da2db792df0 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_NO_LVM crashkernel=auto rhgb quiet rd_NO_DM rhgb quiet maxcpus=8
     initrd /initramfs-2.6.32-431.el6.x86_64.img

重启系统生效。

限制Linux系统内存大小

snippet.bash
[root@gc6541 grub]# vi /boot/grub/grub.conf
snippet.txt
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=f77a0bfb-e971-4afa-9f40-3da2db792df0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_NO_LVM crashkernel=auto rhgb quiet rd_NO_DM rhgb quiet mem=16G

重启系统生效。

修订脚本范例:

snippet.bash
echo ${mem_string}
grub_os_path="/boot/grub/grub.conf"
find_os_mem=`sudo sed -n "/mem=/p" ${grub_os_path}`
 
if [ "${mem_string}" != "0" ] ; then
    if [ -n "${find_os_mem}" ] ; then
        sudo sed -i "s/mem=[^ ]*/mem=${mem_string}/g" ${grub_os_path}
    else
        sudo sed -i "/kernel / s/$/ mem=${mem_string}/" ${grub_os_path}
    fi
else
    if [ -n "${find_os_mem}" ] ; then
        sudo sed -i "s/[ ]*mem=[^ ]*//g" ${grub_os_path}
    fi
fi
 
sudo cat ${grub_os_path}
sudo reboot

增加Linux SWAP分区大小

查看全部分区信息:

snippet.bash
[root@yz_dev ~]# fdisk -l

停止swap分区:

snippet.bash
[root@yz_dev ~]# swapoff –a

增加swap分区:

snippet.bash
[root@yz_dev ~]# dd if=/dev/zero of=/home/swapfile bs=1M count=1024
[root@yz_dev ~]# mkswap /home/swapfile
[root@yz_dev ~]# swapon /home/swapfile
/etc/fstab增加一行
/home/swapfile swap swap defaults 00

重启系统

打印bash脚本调用堆栈

snippet.bash
print_stack()
{
    echo "stack on:"
    stack_deep=${#FUNCNAME[@]}
    for ((i=1; i<$stack_deep; i++)); do
        echo "#$i $0," ${FUNCNAME[$i]}:${BASH_LINENO[`expr $i - 1`]}
    done
}
 
main()
{
    print_stack
}
 
main