生成随机字符串
1
2
3
4
| # 生成 16 位包含字母 + 数字的随机字符串
tr -dc A-Za-z0-9 < /dev/urandom | head -c 16
# 某些情况下用 $(tr -dc A-Za-z0-9 < /dev/urandom | head -c 16) 取值的时候会异常退出,可以加上 head -16 预先读取几行 urandom 再处理,如:
random_string="$(head -16 /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)"
|
生成临时文件
1
2
3
4
| trap 'rm -f "$TMPFILE"' EXIT
TMPFILE=$(mktemp) || exit 1
echo "Our temp file is $TMPFILE"
|
获取 CPU 架构
1
2
3
4
5
6
7
8
9
10
11
12
13
| arch=$(uname -i)
if [ "$arch" == 'x86_64' ]; then
echo "X64 Architecture"
fi
if [ "$arch" == 'x86_32' ]; then
echo "X32 Architecture"
fi
if [ "$arch" == 'armv*' ]; then
echo "Arm architecture"
fi
|
清理系统日志
1
2
3
4
5
| # 查看日志占用磁盘空间
journalctl --disk-usage
# 清理两天前的日志文件
sudo journalctl --vacuum-time=2d
|
Linux too many open files
1
| sudo sysctl -a | grep fs.inotify.max_user_
|
如果这个数字小于一万,可以使用下面的命令临时修改
1
2
| sudo sysctl -w fs.inotify.max_user_watches=100000
sudo sysctl -w fs.inotify.max_user_instances=100000
|
如果想要让参数在系统重启后依然生效,可以修改 /etc/sysctl.conf 文件
1
| sudo vim /etc/sysctl.conf
|
在文件最后加上
1
2
| fs.inotify.max_user_watches=100000
fs.inotify.max_user_instances=100000
|
注销或重启后生效
查看进程打开的文件数
1
2
3
4
5
| # 查看每个进程占用的文件数,按照占用文件的数量倒序排列,第一列是占用文件数,第二列是进程ID
lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr | more
# 查看某个进程占用的文件
ls /proc/进程号/fd/
|