Bash¶
Commands¶
- 查看磁盘使用前20
du -h --max-depth=1 2> /dev/null | sort -n -r | head -n20
- 查看内存前5
ps aux | sort -nrk 3,3 | head -n 5
# update
watch "ps aux | sort -nrk 3,3 | head -n 5"
- 删除前几天的文件
find /data/k8s/k8s/kube-apiserver/ -mindepth 1 -mtime +5 -depth -print # 删除前先检验
find /data/k8s/k8s/kube-apiserver/ -mindepth 1 -mtime +5 -delete
- 临时目录
pushd "$(mktemp -d)"
popd
Snippts¶
- 查看权限
echo -n "Enter file name : "
read file
# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
echo "$file permissions"
echo "$W"
echo "$R"
echo "$X"
- check ports
sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo ss -tulpn | grep LISTEN
sudo lsof -i:22 ## see a specific port such as 22 ##
sudo nmap -sTU -O IP-address-Here
- 清理journal log
# 查看消耗
journalctl --disk-usage
# 设置大小
journalctl --vacuum-size=500M
- delete
# delete one by one
sudo find images_in/ -type f -delete
- list
# list head 2
ls -1 | head -n 2
# du
du -h -d 1
- check the script exist
# 函数:检查是否有其他相同的进程在运行 注意未排除自身
check_running_process() {
# 获取当前脚本的名称(不包括路径)
local SCRIPT_NAME=$(basename "$0")
echo "bash file name: $SCRIPT_NAME"
local COUNT=$(ps -ef | grep "$SCRIPT_NAME" | grep -v grep | grep -v $$ | wc -l)
# 如果发现有其他相同的进程在运行,则退出
if [ $COUNT -gt 0 ]; then
echo "Another instance of $SCRIPT_NAME is already running. Exiting."
exit 1
fi
}
- 文件锁判断是否存在同一进程
# 获取脚本的绝对路径
SCRIPT_PATH=$(readlink -f "$0")
# 生成唯一的锁文件名
LOCK_FILE="/tmp/${SCRIPT_PATH//\//_}.lock"
echo "Lock file: $LOCK_FILE"
# 尝试获取文件锁(等待10秒)
exec 200>"$LOCK_FILE"
if ! flock -n -e 200; then
echo "Another instance is already running. Exiting."
exit 1
fi
- difference between crontab and terminal
/bin/bash /full/path/to/prod-optimize-tables.sh
# crontab
/bin/sh -c /full/path/to/prod-optimize-tables.sh
Config¶
http://bashrcgenerator.com/
vim ~/.bashrc
# add
export PS1="[\t]\[$(tput sgr0)\]\[\033[38;5;14m\]\u\[$(tput sgr0)\]\[\033[38;5;214m\]@\[$(tput sgr0)\]\[\033[38;5;14m\]\h\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;83m\]\w\[$(tput sgr0)\]\[\033[38;5;156m\]\\$\[$(tput sgr0)\]"