Skip to content

Latest commit

 

History

History
114 lines (75 loc) · 2.46 KB

commandline_tips.md

File metadata and controls

114 lines (75 loc) · 2.46 KB

Command Line Tips ( may incompatible on MacOSX and Linux)

show pid of process by name

  • On Linux, you can use pidof

  • On MacOS, try

    ps -ax | grep -m1 <process-name> | awk '{print $1}'
  • ps -aux : 查看进程状态

    parameter function
    -a 显示所有用户有终端控制的进程
    -u 显示用户以及其他详细信息
    -x 显示没有控制终端的进程
    • ps 允许省略参数前的- : ps aux

kill process by port

kill $(lsof -t -i :PORTNUMBER)

check port using

Linux:

netstat -tunlp | grep 8080

The closest equivalent you can get on macOS is:

netstat -p tcp -van | grep '^Proto\|LISTEN' | grep 8080

Or use lsof (list opened files)

sudo lsof -i:8080

NOTE: lsof just list files opened by current users, you need add sudo if you want to see all opened files.

wait previous command to finish

#!/bin/sh
echo “1”
sleep 5&
echo “3”
echo “4”
wait  #会等待wait所在bash上的所有子进程的执行结束,本例中就是sleep 5这句
echo”5”

concat 2 stdout to one file

NOTE: the last ; is key important to Linux

{ command1 && command2 ; } > dist.file

ssh to server and execute something on command

  • solution1: ssh one line command
    ssh 192.168.171.161 "uptime;hostname"
  • solution2: if the job is complex, add bash command to suppress Pseudo-terminal warning
    ssh 192.168.171.161 bash <<EOF
      uptime
      hostname
    EOF