常用linux命令

查询

1. 查看某个服务的pid

ps -ef|grep "service-name"

 

2. 搜索包含某个字符串的所有文件

grep -rn "abc" *

 3. 搜索包含abc的所有c文件

find ./ -name *.c | xargs grep -rni 'abc'

 

 

操作

1. 将linux文件下载到本地

sz filename

 

2. 后台运行springboot的jar包

nohup java -jar -Dfile.encoding=UTF-8 springboot-service.jar > springboot-service-nohup.out 2>&1 &

 

3. 删除文件夹abc

rm -rf abc

 

4. 改变文件或目录abc拥有者为page

chown [-R] page abc

 

5. 后台运行springboot工程

nohup java -jar 222.jar &

  nohup表示不挂断的运行命令,&表示在后台运行

6. 停止特定服务

stopService() {
    cd /opt/test
    ps -ef |grep java |grep -v grep | grep $1
    if [ $? -eq 0 ]
    then
        ps -ef |grep java |grep -v grep | grep $1 |awk '{print $2}'|xargs kill -9
        echo "Stop $1 finished"
    else
        echo "$1 is not running"
fi
}
stopService "hello-world-0.0.1-SNAPSHOT.jar"

其中,grep -v grep 表示反查找,即不包含grep

if [ $? -eq 0]  表示存在这样的进程

awk '{print $2}'  表示打印第二个字段,可以看到第二个字段就是进程号

page@page-virtual-machine:/opt/test$ ps -ef|grep java |grep -v grep |grep hello-world-0.0.1-SNAPSHOT.jar
page       4560      1 15 10:57 pts/0    00:00:06 java -jar -Dfile.encoding=UTF-8 hello-world-0.0.1-SNAPSHOT.jar

page@page-virtual-machine:/opt/test$ ps -ef|grep java |grep -v grep |grep hello-world-0.0.1-SNAPSHOT.jar|awk '{print $2}'
4560

xargs kill -9  因为kill无法接受标准输入,而xargs命令可以通过管道接受字符串,并将接收到的字符串通过空格分割成许多参数(默认情况下是通过空格分割) 然后将参数传递给其后面的命令,作为后面命令的命令行参数

干掉指定的进程,有三种解决方式:

  1. 通过 kill `ps -ef | grep 'ddd'`    
  #这种形式,这个时候实际上等同于拼接字符串得到的命令,其效果类似于  kill $pid

  2. for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill -9 $procid; done   
  #其实与第一种原理一样,只不过需要多次kill的时候是循环处理的,每次处理一个

  3. ps -ef | grep 'ddd' | xargs kill

 

7. 开放某个端口

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

 

 

 

 参考文档:

xargs命令详解,xargs与管道的区别

https://www.cnblogs.com/wangqiguo/p/6464234.html

posted @ 2019-11-14 17:50  到三国卖栗子  阅读(133)  评论(0编辑  收藏  举报