【Linux】技巧

https://www.youtube.com/watch?v=V8EUdia_kOE

  • sudo !!
  • ctrl-k, ctrl-u, ctrl-w, ctrl-y - cutting and pasting text in the command line
  • practical kill/yank example
  • use ‘less +F’ to view logfiles, instead of ‘tail’ (ctrl-c, shift-f, q to quit)
  • ctrl-x-e - continue editing your current shell line in a text editor (uses $EDITOR)
  • alt-. - paste previous command’s argument (useful for running multiple commands on the same resource)
  • reset - resets/unborks your terminal

linux kill 某个端口对应的进程

1
kill -9 $(lsof -i:端口号 -t)
1
kill [']netstat -nlp | grep :3306 | awk '{print $7}' | awk -F"/" '{ print $1 }'[']

根据启动的应用名称来杀死进程

获取所有该应用名称的进程
ps -aux | grep user-0.0.1.jar

1
2
lilou     2676  0.0  0.0  21536   980 pts/18   S+   09:49   0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn user-0.0.1.jar
lilou 4353 0.0 2.0 8230288 339332 pts/21 Sl+ 528 0:43 java -jar user-0.0.1.jar

排除含有 grep 的行
ps -aux | grep user-0.0.1.jar | grep -v grep

1
lilou     4353  0.0  2.0 8230288 339332 pts/21 Sl+  528   0:43 java -jar user-0.0.1.jar

获取进程号
ps -aux | grep user-0.0.1.jar | grep -v grep | cut -c 9-15

1
4353

使用 kill 命令杀死进程
ps -aux | grep user-0.0.1.jar | grep -v grep | cut -c 9-15 | xargs kill -15

1
该进程已经被杀死

java 项目启动和停止脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
cat env.sh
NAME="user-0.0.1.jar"
cat start.sh
. ./env.sh
nohup java -Duser.timezone=GMT+08 -Xmx1G -Xms1G -jar $NAME > /dev/null &
cat stop.sh
. ./env.sh
ps -aux | grep $NAME | grep -v grep | cut -c 9-15 | xargs kill -15
cat restart.sh
./stop.sh
sleep 8
./start.sh

ps -aux | grep python | grep 9090 | cut -c 9-15 | xargs kill -15
cd /www/smbshare && python -m SimpleHTTPServer 9090 > /dev/null 2>&1 &

根据端口来 kill 进程

1
2
3
#!/bin/sh
cd $(dirname $0)
lsof -i :21001 |grep "\(LISTEN\)"|awk -F ' ' '{print "kill -15 " $2}'|sh

具体参考: man lsof

递归删除文件和目录

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

# 递归清除不必要的文件
# https://blog.csdn.net/skylin19840101/article/details/75099240

# 删除文件
find . -name .flattened-pom.xml | xargs rm -f
find . -name .project | xargs rm -f

# 删除目录
find . -name ".settings" | xargs rm -rf
find . -name "target" | xargs rm -rf