發佈日期: 2 則留言

Linux 設定排程 – crontab

查看與編輯 crontab

查看自己的 crontab
crontab -l
查看指定使用者的 crontab
sudo crontab -u gtwang -l
編輯 crontab 內容
crontab -e
編輯指定使用者的 crontab
crontab -u gtwang -e
刪除 crontab 內容
crontab -r

crontab設定檔撰寫教學

# For details see man 4 crontabs

# Example of job definition:
# .—————- minute (0 – 59)
# | .————- hour (0 – 23)
# | | .———- day of month (1 – 31)
# | | | .——- month (1 – 12) OR jan,feb,mar,apr …
# | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

範例如下:
# 每天早上 8 點 30 分執行
30 08 * * * /home/gtwang/script.sh –your –parameter

# 每週日下午 6 點 30 分執行
30 18 * * 0 yourcommand

# 每週日下午 6 點 30 分執行
30 18 * * Sun yourcommand

# 每年 6 月 10 日早上 8 點 30 分執行
30 08 10 06 * yourcommand

# 每月 1 日、15 日、29 日晚上 9 點 30 分各執行一次
30 21 1,15,29 * * yourcommand

# 每隔 10 分鐘執行一次
*/10 * * * * yourcommand

# 從早上 9 點到下午 6 點,凡遇到整點就執行
00 09-18 * * * yourcommand

crontab設定檔的特殊字元

特殊字元 代表意義
星號(* 代表接受任意時刻,例如若在月份那一欄填入星號,則代表任一月份皆可。
逗號(, 分隔多個不同時間點。例如若要指定 3:00、6:00 與 9:00 三個時間點執行指令,就可以在第二欄填入 3,6,9
減號(- 代表一段時間區間,例如若在第二欄填入 8-12 就代表從 8 點到 12 點的意思,也就是等同於 8,9,10,11,12
斜線加數字(/n n 代表數字,這樣寫的意思就是「每隔 n 的單位」的意思,例如若在第一欄填入 */5 就代表每間隔五分鐘執行一次的意思,也可以寫成 0-59/5
發佈日期:

解決Windows下路徑名稱異常問題

名稱有中文或空白的問題

使用cmd指令時,有許多command line執行的工具,當路徑若出現中文或是空白時,會導致執行失敗。
例如C:\Program Files因為中間有一個空格,就很容易造成在執行時出現錯誤

若遇到這種狀況,建議可以改用資料夾的縮寫
例如
C:\PROGRA~1取代C:\Program Files
C:\PROGRA~2取代C:\Program Files (x86)

如何查找資料夾名稱縮寫

C:\Users\claire.chang>dir /x

以上圖來說,資料夾.android的縮寫即為ANDROI~1

發佈日期:

Linux下大檔案的log分割處理

使用指令分割大檔案

按檔案大小分割
[root@localhost]$ split -C 100M large_file.txt stxt
按行數分割
[root@localhost]$ split -l 1000 large_file.txt stxt
二進位檔案分割(以-b引數來指定分割後的檔案大小)
[root@localhost]$ split -b 100M data.bak sdata

固定時間切割LOG檔案

logrotate官方說明: https://linux.die.net/man/5/logrotate.conf

logrotate旨在簡化對生成大量日誌文件的系統的管理。它允許自動旋轉,壓縮,刪除和郵寄日誌文件。每個日誌文件可以每天,每週,每月或當文件太大時進行處理。

通常,logrotate作為日常cron作業運行。除非該日誌的標準基於日誌的大小並且每天要多次運行logrotate,或者除非使用了-f或–force選項,否則它不會在一天內多次修改日誌。

命令行上可以提供任意數量的配置文件。較新的配置文件可能會覆蓋較早的文件中提供的選項,因此列出logrotate配置文件的順序 很重要。通常,應使用包含所需其他任何配置文件的單個配置文件。有關如何使用include指令完成此操作的更多信息,請參見下文。如果在命令行上給出了目錄,則該目錄中的每個文件都將用作配置文件。

如果未提供命令行參數,logrotate將打印版本和版權信息以及簡短的使用摘要。如果輪換日誌時發生任何錯誤,logrotate將以非零狀態退出。

閱讀全文 Linux下大檔案的log分割處理

發佈日期: 2 則留言

Linux刪除檔案後空間未釋放

查詢硬碟使用狀況

[root@localhost ~]# df

使用df可以查看硬碟的使用狀況

查詢刪除卻正在被使用的檔案

lsof(lsof的全稱是list open files),此工具可以用來查看正在運行中的進程打開了哪些文件、目錄和套接字;是系統監測工具之一。
請參見: 好用的網管指令-lsof

查找打開,但是不能連接的文件
一個進程打開一個文件, 然後將其設為 unlinked 狀態, 則此文件資源仍能被進程使用, 但是其訪問路徑已經被刪除了.
因此, 使用ls不能將其列出. 只有當進程結束時, 才能釋放文件佔用的資源

查找unlinked 文件, 選項 +L, 作用: 列出打開文件的連接數

[root@localhost ~]# lsof +L


指定連接數的上限

[root@localhost ~]# lsof +L1
閱讀全文 Linux刪除檔案後空間未釋放
發佈日期:

Socket.io錯誤訊息意義

I have found:

  • “ping timeout”: client stopped responding to pings in the allotted amount of time (per the pingTimeout config setting).
  • “transport close”: this appears to happen if the client side stopped sending data at all… or maybe there’s some kind of callback causing this to happen. I can see it happen if I just close a tab or follow a link from a page where I have an active connection to the server. But I’m not clear if this is always a case of the client causing it to happen.

    Sorry for re-opening the issue. Regarding the transport close that is the reason when page is closed/reloaded, it also happens some times in bad network conditions specifically when the ping packets are not delivered to the client. For the latter I need to handle it in the server side by waiting for the client to reconnect. Is there a way to properly distinguish between these two?

  • “Client namespace disconnect”: When the client sends a disconnect packet (client.disconnect())
  • “server namespace disconnect”: Looks to be when the server performs a socket.disconnect() action.
  • “Transport error”: An error occurred, I assume this is a server side error, but I’m not totally clear, as I’ve not been able to trigger one on my own.
  • “io server disconnect” This occurs using a third party library socketIOAuth when authentication fails

閱讀全文 Socket.io錯誤訊息意義

發佈日期:

Socket.io介紹

Socket.io

socket.io是基於Websocket的Client-Server實時通信庫

Socket.io承繼了Node.js的事件處理方法,把Client端與Server端的程式統一成一至的操作方式,讓使用者可以只需專注在處理「事件」,就可以快速開發出應用,他也支援『房間』的概念,可以使用同一條WebSocket卻擁有不被彼此干擾的資料傳輸(多種聊天頻道的概念)。另外,他也提供了很好的fallback機制,即使用戶的瀏覽器不支援WebSocket,他還是可以利用Flash、XMLHttpRequest等方式來傳送資訊(速度會比較慢就是了)。這些機制都他都包裝好了,所以寫程式時並不需要知道這些細節,只需要設定好就可以運作。

Socket.io 特性整理

  • Events 自訂事件。
  • Rooms Room 的概念只存在於伺服器端。可以理解為訊息處理時的聽眾分組,可對同一個分組內的聽眾進行廣播。
  • Namespaces 命名空間,我理解為底層連線的分組管理,不同命名空間可以走同一條 Engine.io 連線或是各自連線,每個命名空間可以各自驗證是否接受連線。
  • ACK 回調 如同 HTTP 之於 TCP,HTTP 為 TCP 提供了一套請求與響應的模型。ACK 也為 Socket.io 提供了一套請求與響應的通訊模型。
  • 連線維護
  • 自動斷線重連
  • ping/pong 心跳

閱讀全文 Socket.io介紹

發佈日期:

Socket.io自行增加header

伺服器端

範例程式碼:

import express from "express";
import http from "http";

const app = express();
const server = http.createServer(app);

const sio = require("socket.io")(server, {
    handlePreflightRequest: (req, res) => {
        const headers = {
            "Access-Control-Allow-Headers": "Content-Type, Authorization",
            "Access-Control-Allow-Origin": req.headers.origin, //or the specific origin you want to give access to,
            "Access-Control-Allow-Credentials": true
        };
        res.writeHead(200, headers);
        res.end();
    }
});

sio.on("connection", () => {
    console.log("Connected!");
});

server.listen(3000);

閱讀全文 Socket.io自行增加header

發佈日期:

Engine.io介紹

Engine.io介紹

Socket.io是在engine.io的基礎上去實作的
Gitlab連結: Engine.IO: the realtime engine
engine.iosocket.io提供跨瀏覽器/跨設備的雙向通信的底層庫。engine.io使用了WebsocketXHR方式封裝了一套socket協議。在低版本的瀏覽器中,不支持Websocket,為了兼容使用長輪詢( polling )替代。

關於長輪詢可參考我的另一篇文章:WebSocket與Ajax的不同
過去WebSocket未出來時,許多聊天室使用的都是長輪詢的方式去實作,而engine.io則可依據客戶端環境兼容使用這兩種方式。
閱讀全文 Engine.io介紹