Skip to content

Useful Command

useful tools

Memory monitor command

Note that Linux likes to use any extra memory to cache hard drive blocks. So you don’t want to look at just the free Mem. You want to look at the free column of the -/+ buffers/cache: row.

This shows how much memory is available to applications. So I just ran free -m and got this:

watch -n 5 free -m

Reminder: To stop this watch cycle you can just press Ctrl+C.

The watch command can be also used for monitoring other data with time update


Process name(PID)

ps listed every process that is running

ps aux  | grep <program name>

with pgrep and pidof, it will only return PID of the program name

pgrep <program_name>
pidof <program_name>

It can also reach by program itself

c code
#include <stdio.h>
#include <unistd.h>

int main() {
    pid_t pid = getpid();
    printf("The PID of this process is: %d\n", pid);
    return 0;
}
python
import os

pid = os.getpid()
print(f"The PID of this process is: {pid}")

We can write it to a script also.

#!/bin/bash

# 啟動程序並將其放入後台
./my_program &
# 獲取後台進程的 PID
PID=$!

echo "The PID of my_program is: $PID"


Process Info in Linux

Files located in /proc/[pid]/ provides tons of information about process including kernel content, complete docs can be found here

The directory /proc contains (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID).

if there is any doubt, review the kernel documentation in the directory /usr/src/linux/Documentation.

status

overview of process information, such as virtual memory, code and heap…so on

cat /proc/[pid]/status

# 每0.5秒更新一次數據
watch -n 0.5 cat /proc/[pid]/status

This shows you nearly the same information you would get if you viewed it with the ps command. In fact, ps uses the proc file system to obtain its information. But you get a more detailed view of the process by reading the file /proc/PID/status.

maps

further information about memory mapping, address range…so on

The /proc/PID/maps file contains the currently mapped memory regions and their access permissions.

cat /proc/[pid]/maps

#每0.5秒更新一次數據
watch -n 0.5 cat /proc/[pid]/maps

smaps

much further information about each memory location of info, RSS(Resident Set Size), PSS(Proportional Set Size)…so on

The /proc/PID/smaps is an extension based on maps, showing the memory consumption for each of the process’s mappings.

cat /proc/[pid]/smaps

#每0.5秒更新一次數據
watch -n 0.5 cat /proc/[pid]/smaps

Like before, we can also write sript for automation, this time it’s done by using python script


VS Code

  • ctrl+shift+del : clear terminal

ssh

scp

copy files between ssh server and client

#從本地端複製到遠端
scp /path/file1 user@192.168.0.1:/path/file2
#從遠端複製到本地端
scp user@192.168.0.1:/path/file2 /path/file1
* -r : 複製整個目錄
* -p : 保留檔案時間與權限
* -c : 資料壓縮


git

拉遠端的branch, 又想保存本地端修改

git stash #保存本地端修改
git pull --rebase #拉取遠端
git stash pop #還原本地端修改

修改commit訊息

如果是要修改已經push上去的commit message,需要用rebase的方式修改 Ref

git rebase -i head~N
#N為要修改的commit訊息

而修改當前訊息,僅需要用--amend修改即可

git commit --amend -m "New commit message."


VIM

replace string in current line

:s/input/output

replace all matched line

:%s/input/output

replcae all matched character and line

:%s/input/output/g

select and highlight the current bracket, use v+i+w