跳转至

Linux 小笔记

此笔记记录 Linux 下的一些命令使用技巧,作为工作中的备忘。

导入数据库时跳过某个表

有时我们需要将某天的数据库备份恢复到内网的机器,用来分析数据,但备份中的某些很大的表对我们的数据分析作用不大,却要占用大量的导入时间(譬如session表,message表),我们可以用简单的sed命令跳过对这些表的插入操作。


sed '/INSERT INTO `sessions`/d' db.sql > db_new.sql

找出最近修改、创建的文件

以下命令可以用来简单的检查最近目录下哪些文件被修改过。


find . -newermt "2013-01-01 00:00:00" ! -newermt "2013-01-02 00:00:00"

find . -mtime -10 -mtime +4


自动压缩旧日志文件的shell脚本

logrotate 是 Linux 自带的日志分割压缩工具,但它有个比较让人不爽的缺点就是无法精确的按时间点分割(譬如日志严格的按天)。因此考虑App日志直接按 ×××-Y-m-d.log的方式来按天记录,然后用crontab定时压缩。

下面是自动gzip压缩多个目录下log文件,并且自动跳过当天文件的shell脚本:


#!/bin/sh

FOLDERS=(/data/logs/view/ /data/logs/search/)
NOW="$(date +"%Y-%m-%d")"
for FOLDER in ${FOLDERS[@]}
do
    echo "Zipping $FOLDER"
    for FILE in ${FOLDER}*.log
    do
        if [[ ! $FILE =~ $NOW ]]; then
            echo "$FILE"
            gzip -9 $FILE
        fi
    done
done


Linux 初始化 SSH 会话慢的解决方法

使用ssh登录Linux服务器,就算是在内网环境中,也需要等待比较长的时间才弹出密码输入界面。

检查:

ssh -vvv 加上 v参数,通过分析ssh打印的debug信息,即可找到时间消耗的环节。


$ ssh -vvv root@*****
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to @***** port 22.
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug2: fd 3 setting O_NONBLOCK

可能原因:

服务端 /etc/ssh/sshd_config,修改 "UseDNS no"

客户端 /etc/ssh/ssh_config,修改 "GSSAPIAuthentication no"

Crontab 控制脚本运行时间段

Cronjob 通常用来在指定时间点运行某个命令(脚本),而有时我们不但需要定时启动脚本,还需要定时关闭,譬如需要在空闲时段备份服务器文件到新机器。

我们可以使用两个cronjob,一个定时启动,一个定时关闭,或者使用类似下面的脚本,启动后后台运行一个进程,通过获取时间来定时关闭:


#!/bin/bash

rsync ... &
pid=$!

while /bin/true; do
  if [ $(date +%H) -ge 8 ]; then
    kill -TERM $pid
    exit 0
  else
    sleep 60
  fi
done

rsync 只同步匹配的目录文件

通常我们的图片、文件会根据某中规则(譬如2016-09-09)的格式存储在文件系统中,有时我们需要将适合某个

rsync -aPv --include='**/2016-09-09/*' --include='*/' --exclude='*' rsync://...

Ubuntu 下切换 PHP 版本

偶尔需要维护一些旧项目,而这些项目无法做到php7兼容,此时就需要安装多个PHP版本,并且用类似下面的命令进行切换

sudo a2dismod php7.1 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; sudo update-alternatives --set php /usr/bin/php5.6 ; sudo update-alternatives --set phar /usr/bin/phar5.6 ; sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6 ;  sudo update-alternatives --set phpize /usr/bin/phpize5.6 ;  sudo update-alternatives --set php-config /usr/bin/php-config5.6

ttyACM 插入后被占用问题

最近在调试 STM32 的 USB 功能的过程中发现,将 STM32 插入电脑的 USB 口后 STM32 模拟出来的串口 ttyACM0 会被占用,使用 lsof 也看不出问题,十几秒中后又会自动释放。

后来将 USB 口收到的数据包 printf 出来后发现是一堆 AT 指令,看了一下系统进程发现一个可以的 ModemManager。

Google 了一下,大概就是 ModemManager 检测到 ttyACM* 接入后会尝试进行通讯,看对方是否为合法的 Modem。

解决方法:将 ModemManager 禁用,或者将设备 id 加入忽略列表。

P52 Manjaro 外接显示器输出问题

按官方文档安装相关软件后,修改 /etc/bumblebee/xorg.conf.nvidia

##
## Generated by mhwd - Manjaro Hardware Detection
##


Section "ServerLayout"
    Identifier "Layout0"
    Option "AutoAddDevices" "true"
EndSection

Section "Device"
    Identifier  "Device1"
    Driver      "nvidia"
    BusID       "PCI:1:0:0"
    VendorName "NVIDIA Corporation"
    Option "NoLogo" "true"
    Option "UseEDID" "true"
    #Option "AllowEmptyInitialConfiguration"
    #Option "ConnectedMonitor" "DFP"
EndSection

Manjaro zsh 新标签页保持路径

Manjaro 下默认配置的 zsh,当使用 ctrl-T 打开新标签时,会自动将路径变成用户主目录,会稍微影响效率,修改 ~/.zshrc , 增加下面配置行,可以使新标签依然保持原路径:

[[ -f /etc/profile.d/vte.sh ]] && . /etc/profile.d/vte.sh