Linux - Monitoring

How linux work 這本書看到了第八章,這一章主要在介紹一些可以監測 CPU / process / 資源 的工具,趁這個機會把其中一些指令記錄下來。

  • lsof : list open files,這個指令會列出使用中的檔案與使用這個檔案的 process
  • top : 可以持續地監看目前所有 process 狀況,包括 PID / CPU / memory / port 等
  • ps : process status,雖然也是列出 process 的狀況,但是是快照的形式而不會持續性變動
  • uptime : kernal 跑了多久、過去 1/5/15 分鐘的 load average

Linux - Symbolic Link

ln -s <path to the file/folder to be linked> <the path of the link to be created>

例如

ln -s hello.txt newhello.txt     

可以用 ls -l 列出檔案查看: 最前面的 l 代表這個檔案是 symlink,後面的箭頭代表這個檔案指向哪個檔案。

unlink <path-to-symlink>

直接使用 rm 指令也行。

find . -type l ! -exec test -e {} \; -print

find -L

- L flag 代表搜索過程中會遵循 symlink,也就是進入 symlink 指向的資料夾/文件作進一步搜索,使用這個 flag 可能會進入無限迴圈,或是不經意地破壞作業系統,應避免使用!

Reference

How can I find broken symlinks Symlink Tutorial in Linux – How to Create and Remove a Symbolic Link

Linux - Shell input & output

把指令的輸出存到某個檔案:

command > file

例如:

echo hello > hello.txt

可以用 cat hello.txt 印出檔案內容。

注意!hello.txt 如果已經存在,會覆蓋掉原本的檔案!

不想覆蓋掉檔案,而是加在原檔案後面的話,將原本的指令的 > 改為 »

例如:

echo hello >> hello.txt

Pipe character

將某指令的輸出當作另一個指定的標準輸入,可以使用 pipe character (|)

例如:

cat hello.txt | wc

wc 會印出會印出他的行數、單詞數、字符串數(包含 newline),會發現是 1, 1, 6 把字串送進檔案、不包含 newline 的另一個作法是這個指令:

printf hello > hello.txt    

Reference

[Linux]cat 搭配wc用法

Linux - Manual & Info

查看 ls 指令的 manual page :

man ls 

最上方 LS 旁邊的 (1) 代表這個指令他的類別,1 是 User commands (Programs)。詳細分類如下:

不確定指令名字的時候,使用關鍵字搜尋 manual page:

man -k keyword

另一種可以使用的指令是 info :

info ls

Reference

man-pages(7) — Linux manual page

React - 使用 Wagmi 連接錢包並保持連線

依照 wagmi document 指示安裝好 package後,首先在 React 建立連線錢包按鈕的 Component:

import { useAccount, useConnect } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import React from 'react'
import { useDisconnect } from 'wagmi'

export default function ConnectButton() {
  const { address, isConnected } = useAccount()
  const { connect } = useConnect({
    connector: new InjectedConnector(),
  })

  const { disconnect } = useDisconnect()

  async function connectWallet() {
    try {
      connect()
      localStorage.setItem('isWalletConnected', true) 
      // 連接錢包後將 localStorage 的 isWalletConnected 欄位設定為 true
    } catch (ex) {
      console.log(ex)
    }
  }

  async function disconnectWallet() {
    try {
      disconnect()
      localStorage.setItem('isWalletConnected', false)
      // 解除連接錢包後將 localStorage 的 isWalletConnected 欄位設定為 false
    } catch (ex) {
      console.log(ex)
    }
  }

  if (isConnected) return( // 如果 isConnected == true 表示在連線狀態,再次點擊會斷開連接
    <div>
      <button  id='wallet_address' onClick={() => disconnectWallet()}>Connected to {address}</button>
    </div>
  )
  return(
    <div>
      <button id='personal_btn' onClick={() => connectWallet()}>Connect Wallet</button>
    </div>
  )
}

在需要連接錢包、保持連線的地方新增以下程式碼

import * as React from 'react';
import { useEffect } from "react";
import { InjectedConnector } from 'wagmi/connectors/injected'
import ConnectButton from "./component/connect";
import { useConnect  } from 'wagmi'

function Main() {
	const { connect } = useConnect({
    connector: new InjectedConnector(),
  })

	useEffect(() => { 
    // 每次進入頁面都檢查 localStorage 是否有已連線的資訊,沒有則自動連線錢包
		const connectWalletOnPageLoad = async () => {
			if (localStorage?.getItem('isWalletConnected') === 'true') {
				try {
					connect()
				} catch (ex) {
					console.log(ex)
				}
			}
		}

		connectWalletOnPageLoad()
	}, [])

	return(
		<div>
			<div><ConnectButton /></div> 
		</div>
	  );
};

export default Main;

Reference

Keep Wallet Connected on Page Refresh — Web3 Dev