請 Claude Code 製作 QPKG:在 Public 共用資料夾寫入 Hello world、建立 crontab 編輯器
Claude Code 在您 NAS 上的 Bucket & Code Server IDE 環境中執行。請從面板、編輯器的終端機、claude.ai/code 或手機應用程式啟動工作階段,再用一般用語說明套件要做什麼。本指南示範兩段實際使用的提示詞。HelloPublic 將「Hello world」寫入 Public 共用資料夾中的檔案。CronEdit 依照 QNAP 的規則,將自己的排程項目寫入 NAS 的 crontab。
- 工作階段在專案資料夾中
- 提示詞描述套件功能
- 檢查閱讀並測試程式碼
- 建置bvs qbuild
- 安裝並驗證套件
截圖取自 Bucket & Code Server IDE 1.0.0-102,使用的帳號是 ola。環境中已安裝 Claude Code,但沒有人登入 Claude Code,因此本指南以文字呈現工作階段。提示詞和回覆都取自 Claude Code 的實際執行紀錄。
01開始之前
- 在 新專案 中選擇 QPKG 套件 範本來建立專案。專案包含
.claude/skills/qnap-dev技能和AGENTS.md檔案。Claude Code 讀取技能後,會針對 NAS 環境撰寫程式碼,使用 BusyBoxsh、/etc/config和系統記錄。 - 在環境中安裝 Claude Code:工具鏈 → 選擇環境 → AI 工具 → Claude Code → 安裝。接著在同一個對話方塊中選擇 登入 Claude,使用自己的帳號登入。
- 請使用角色為 developer 或 admin 的帳號。只有管理員可以在 NAS 上安裝套件。
02啟動工作階段
以下方式都會啟動同一個環境中的 Claude Code。請在專案資料夾中啟動 Claude Code,讓它讀取專案的技能。
- 從面板啟動。 在 AI 工具 的 Claude Code 分頁中,從 資料夾 欄位選擇專案,再按 啟動新的工作階段。工作階段在環境的隔離區中執行。關閉頁面後,工作階段仍會繼續執行。您可以在 終端機 頁面找到它。

- 從編輯器的終端機啟動。 點選按鈕下方的 也可以在編輯器的終端機中啟動 連結,即可在編輯器的終端機啟動工作階段。關閉編輯器分頁後,這個工作階段就會結束。
- 從 claude.ai/code 或 Claude 應用程式啟動。 登入後,同一個分頁會顯示 Remote Control:「此環境的工作階段可從 claude.ai/code 和 Claude 應用程式啟動並操控。」開啟這個功能,再到 claude.ai/code 或 Claude 應用程式選擇此環境。
- 從手機啟動。 在面板的 手機 App 頁面配對 Bucket & Code 手機 App 後,開啟 App 的 AI 分頁。選擇環境、資料夾和工具,再按 開始新對話。
03請 Claude Code 製作 HelloPublic
以下是在名為 HelloPublic 的新專案資料夾中使用的英文提示詞原文:
This folder is a QPKG project that Bucket & Code's New project made. Make the package HelloPublic: when it starts, it writes the line "Hello world" into the text file hello.txt in the Public shared folder. - Take the Public folder's path from QTS: getcfg Public path -f /etc/config/smb.conf. Never a fixed volume such as /share/CACHEDEV1_DATA. - If the NAS has no Public share, the start says so in the NAS's System Logs and fails. - Stop and uninstall leave hello.txt where it is: it is the user's file then. - Follow the qnap-dev skill in .claude/skills. Do not build or install anything: I build it with bvs qbuild and install it myself. At the end, tell me which files you changed, how to build it and how to check it on the NAS.
Claude Code 將啟動邏輯寫入 shared/HelloPublic.sh。程式從 QTS 取得 Public 的路徑,並覆寫檔案,因此第二次啟動後仍只有一行:
service_start() {
local dir file
dir=$("$QPKG_SYS/sbin/getcfg" Public path -f "$QPKG_SYS/etc/config/smb.conf")
if [ -z "$dir" ]; then
qpkg_log error "this NAS has no Public shared folder (no path for [Public] in /etc/config/smb.conf); hello.txt is not written"
return 1
fi
qpkg_sys "$dir"
if [ ! -d "$QPKG_P" ]; then
qpkg_log error "the Public shared folder $dir does not exist or its volume is not mounted; hello.txt is not written"
return 1
fi
file=$QPKG_P/hello.txt
…
if ! echo "Hello world" >"$file"; then
qpkg_log error "cannot write $dir/hello.txt"
return 1
fi
# the user's file: not only root may change it (root writes it, with the umask 022 qpkg_init set)
chmod 666 "$file"
qpkg_log info "wrote \"Hello world\" into $dir/hello.txt"
}
}
以下是回覆節錄。Claude Code 在這次工作階段中可以編輯檔案和檢查語法,但無法執行檔案,並在回覆中說明:
HelloPublic now writes "Hello world" into `hello.txt` in the Public shared folder when it starts. I haven't run it: the syntax check (`sh -n`) passes, but the test against a fake NAS folder needed your approval and wasn't run. Nothing is built, installed or committed.
## Files changed
- **`shared/HelloPublic.sh`** (the start script):
- **Start** gets the Public folder's path from `/sbin/getcfg Public path -f /etc/config/smb.conf`, with no fixed volume anywhere. It then overwrites `hello.txt` there with the one line, so starting twice still leaves one line. It sets the file to mode 666 so other users can change it, not only root.
- **Start fails** (exit 1, with an error in System Logs) when:
- the NAS has no Public share;
- Public's folder doesn't exist, for example because its volume isn't mounted. The script never creates that folder, because a folder made then would land in RAM;
- `hello.txt` is a link or not a regular file. Public is open to every user, so this stops anyone tricking the root-run start into overwriting some other file.
- **Stop and uninstall** don't touch `hello.txt`.
…
## Check it on the NAS
1. Install it: `qpkg_cli -m /share/<folder>/HelloPublic_0.1.0_<arch>.qpkg --keep`, or use App Center's manual install. The start runs as part of the install.
…
04請 Claude Code 製作 CronEdit
提示詞逐條列出 QNAP 的 cron 規則,方便事後在程式碼中逐一核對:
This folder is a QPKG project that Bucket & Code's New project made. Make the package CronEdit, a small crontab editor for a QNAP NAS that follows QNAP's rules for cron: 1. The app keeps its own schedule lines in a file of its own in the package's folder, one cron line per line. A command, cronedit, manages them: cronedit add "<five time fields> <command>", cronedit list, cronedit remove <number>. 2. The lines go into /etc/config/crontab, the file that survives a restart of the NAS. Never crontab -e, never the files under /tmp/cron or /var/spool/cron. 3. Every line the app writes into /etc/config/crontab ends with the app's own tag, so its lines can always be told from the others. 4. At every start, the service script checks each of the app's lines and adds it only when /etc/config/crontab does not have it yet. A second start adds nothing: never a duplicate. 5. On stop and on uninstall it removes its own tagged lines, and only those (cleanup), including lines that are no longer in its own file. 6. After every change of /etc/config/crontab it reloads cron the QNAP way: crontab /etc/config/crontab && /etc/init.d/crond.sh restart. When nothing changed, it does not reload. 7. It never changes, moves or removes a line it did not write. 8. cronedit add and cronedit remove change the app's file and, while the app is started, bring /etc/config/crontab in line at once, by the same rules. Follow the qnap-dev skill in .claude/skills (the NAS has BusyBox and no bash; cron runs commands with a short PATH). Do not build or install anything: I build it with bvs qbuild and install it myself. At the end, tell me which files you changed, how to build it and how to check it on the NAS.
以下是回覆節錄:
I've written CronEdit, but **I haven't run it.** Running scripts needed an approval this session didn't give, so the only check was a syntax check with `sh -n`, which passed for both scripts. Everything else comes from reading the code. A test script for a fake NAS file tree is in `build/t/run.sh` (git ignores `build/`), so you can run it with `sh build/t/run.sh`. Nothing is committed. ## Files changed - **`shared/bin/cronedit`** (new, POSIX `sh` for BusyBox): the `add`, `list` and `remove <number>` commands, plus `sync`, `cleanup` and `check`, which the service script calls. - **Its own file:** the lines live in `<install folder>/cronedit.tab`. The package doesn't ship that file, so an upgrade can't overwrite it. - **Tag:** every line it writes into `/etc/config/crontab` ends in `# managed-by:CronEdit`. It rejects a line that is badly formed, starts with `@reboot` or `#`, contains the tag, or ends in `\`. It also rejects a line that is already in its file. - **Other lines are safe:** before it replaces `/etc/config/crontab`, it checks that every untagged line is still there, unchanged and in the same order. If not, it leaves the file alone. It keeps a backup while writing and puts it back if the write fails. - **Reload:** it runs `crontab /etc/config/crontab && /etc/init.d/crond.sh restart` only when lines were actually added or removed. - **Started or stopped:** `add` and `remove` update `/etc/config/crontab` straight away only while the package is started. While it's stopped, the change waits for the next start. - A command that doesn't start with `/` gets a note that cron's PATH is short. - **`shared/CronEdit.sh`**: the service script, written by hand on top of `lib/bvs-qpkg.sh`. - **start:** runs `sync`, which adds only the missing lines, so a second start adds nothing. It also removes tagged lines that are no longer in `cronedit.tab`, and links `/usr/bin/cronedit`. - **stop and remove (uninstall):** run `cleanup`, which removes every tagged line and only those. - **status:** runs `check`. - **Switched-off package:** if a start is refused because the package is off, it still removes the tagged lines first, in case a power cut skipped the stop. … **One thing for you:** deleting the template placeholder `shared/bin/CronEdit` needed an approval I didn't get, so it's still there. It does no harm, but you can remove it with `git rm shared/bin/CronEdit`.
05程式碼中的 crontab 規則
CronEdit 將自己的排程項目存放在套件資料夾中的 cronedit.tab。程式 bin/cronedit 將這些項目寫入 /etc/config/crontab,這個檔案在 NAS 重新啟動後仍會保留。程式寫入的每一行都以標記結尾,且只會修改以該標記結尾的行:
TAG_WORD='managed-by:CronEdit'
TAG="# $TAG_WORD" # the end of every line CronEdit writes; a user's line may not hold TAG_WORD
SYS=${BVS_QPKG_SYSROOT-}
CONF=$SYS/etc/config/qpkg.conf
CRONTAB=$SYS/etc/config/crontab
程式用 awk 掃描一次,產生新檔案。程式將未加標記的每一行留在原位。套件需要的每一行若有標記,程式只保留第一次出現的那一行。程式移除其餘加了標記的行,包括已從 cronedit.tab 刪除的行。程式把缺少的排程項目加到檔尾:
changes=$(awk -v tag="$TAG" -v out="$new" "$AWK_OURS"'
FILENAME == ARGV[1] { if (!($0 in want)) { want[$0] = 1; order[++n] = $0 }; next }
ours($0) {
if (($0 in want) && !($0 in seen)) { seen[$0] = 1; print > out } else { dropped++ }
next
}
{ print > out }
END {
for (i = 1; i <= n; i++) if (!(order[i] in seen)) { print order[i] > out; added++ }
print dropped + 0, added + 0
}' "$want" "$old")
若沒有新增或移除任何行,檔案保持原樣,cron 也不會重新啟動:
set -- $changes
DROPPED=${1:-0}
ADDED=${2:-0}
if [ "$DROPPED" = 0 ] && [ "$ADDED" = 0 ]; then
rm -f "$new"
return 0
fi
寫入前,程式再用 awk 掃描一次,確認未加標記的每一行都還在,且內容和順序都相同。若檢查未通過,程式就不修改原檔。寫入後,程式依照 QNAP 的方式重新載入 cron:
reload() {
# on the NAS: /usr/bin/crontab /etc/config/crontab && /etc/init.d/crond.sh restart
if "$SYS/usr/bin/crontab" "$CRONTAB" && "$SYS/etc/init.d/crond.sh" restart >/dev/null 2>&1; then
return 0
fi
say "the reload of cron failed (crontab /etc/config/crontab && /etc/init.d/crond.sh restart)"
event 2 "the reload of cron failed (crontab /etc/config/crontab && /etc/init.d/crond.sh restart)"
return 1
}
服務指令碼會呼叫該程式。start 同步 crontab 中的排程項目。stop 和 remove(解除安裝)會移除所有加了標記的行。即使 App Center 的開關停用了套件,啟動程序仍會移除這些行,以免斷電時未執行停止程序:
service_start() {
# the command on PATH; a file of another at /usr/bin/cronedit is left alone, and bin/cronedit still works
qpkg_link "$CRONEDIT" /usr/bin/cronedit || :
"$CRONEDIT" sync
}
service_stop() {
"$CRONEDIT" cleanup
qpkg_unlink /usr/bin/cronedit "$CRONEDIT"
return 0
}
…
service_remove() {
"$CRONEDIT" cleanup
qpkg_unlink /usr/bin/cronedit "$CRONEDIT"
return 0
}
# A switched-off package's start is refused by qpkg_start; its tagged lines must not run either (a power cut skips
# the stop), so they go first.
if [ "${1-}" = start ] && ! qpkg_enabled; then
"$CRONEDIT" cleanup
fi
套件執行時,cronedit add 和 cronedit remove 會修改 cronedit.tab,並立即同步 /etc/config/crontab。套件停止時,變更會等到下次啟動才同步。
06檢查 Claude Code 撰寫的程式碼
先查看程式碼的變更,再執行 Claude Code 在該工作階段無法執行的測試。CronEdit 的測試使用模擬的 NAS 目錄結構。請執行 sh build/t/run.sh。這項測試已通過。檢查後有以下變更:
- CronEdit:已移除範本中的
bin/CronEdit。 沒有任何程式會執行這個檔案。它的檔名和bin/cronedit只有字母大小寫不同。在 Windows 或 macOS 上,其中一個檔案會覆寫另一個。Claude Code 發現了這個問題,但刪除檔案需要核准。README 原先介紹這個檔案的兩行,現在改為說明bin/cronedit。 - HelloPublic:程式碼無變更。
- 安裝方式。 回覆建議用
qpkg_cli -m … --keep安裝。這個指令在 QTS 6.1 上無法安裝未簽署的套件,qpkg_cli -s會傳回「notfound」。第 08 節說明可用的安裝方式。
07建置套件
在專案頁面按 建置,系統會在專案的建置環境中執行 bvs qbuild。若從終端機操作,請在專案資料夾中執行相同的指令:
bvs qbuild ls build/ CronEdit_0.1.0_x86_64.qpkg CronEdit_0.1.0_x86_64.qpkg.md5
若專案沒有程式碼簽署憑證,套件就沒有簽章,測試部署 會維持停用,並顯示「需要已簽署程式碼的套件」。已簽署的建置可透過 測試部署 安裝。簽署方式請參閱 App Center 指南。
08安裝並驗證套件
若要在自己的 NAS 上試用未簽署的套件,請將套件檔案複製到共用資料夾。接著以 admin 帳號透過 SSH 登入 NAS,再執行該檔案。QTS 會安裝並啟動套件:
# 透過 SSH 以 admin 身分在存放檔案的共用資料夾中執行
cd /share/Public/packages
sh ./CronEdit_0.1.0_x86_64.qpkg
…
Link service start/stop script: CronEdit.sh
Set QPKG information in /etc/config/qpkg.conf
CronEdit: started
- 用這個方式安裝時,QTS 不會在
/etc/config/qpkg.conf中為該套件寫入Status這一行。App Center 安裝套件時,會寫入Status = complete。 - 請勿為了安裝這些套件,在 App Center 中開啟允許安裝無有效數位簽章應用程式的選項。請為這些套件加上數位簽章。
- 在 App Center 中移除套件,或執行
qpkg_cli -R CronEdit。
HelloPublic 啟動後:
P=$(getcfg Public path -f /etc/config/smb.conf) cat "$P/hello.txt" Hello world
在 CronEdit 執行期間新增兩筆排程項目:
cronedit add "*/15 * * * * /bin/true" cronedit: added: */15 * * * * /bin/true cronedit: /etc/config/crontab: 1 line(s) added, 0 removed cronedit add "* * * * * /bin/date -u >> /share/Public/cronedit-demo.txt" cronedit: added: * * * * * /bin/date -u >> /share/Public/cronedit-demo.txt cronedit: /etc/config/crontab: 1 line(s) added, 0 removed cronedit list CronEdit is started; its lines (/share/CACHEDEV1_DATA/.qpkg/CronEdit/cronedit.tab): 1 */15 * * * * /bin/true 2 * * * * * /bin/date -u >> /share/Public/cronedit-demo.txt grep -n 'managed-by:CronEdit' /etc/config/crontab 57:*/15 * * * * /bin/true # managed-by:CronEdit 58:* * * * * /bin/date -u >> /share/Public/cronedit-demo.txt # managed-by:CronEdit /etc/init.d/CronEdit.sh status CronEdit: Enable = TRUE CronEdit: started CronEdit: every line of /share/CACHEDEV1_DATA/.qpkg/CronEdit/cronedit.tab is in /etc/config/crontab, once: yes
09測試結果
安裝前,我們在執行 QTS 6.1 的 NAS 上備份 /etc/config/crontab(共 58 行),每完成一個步驟,就將檔案與備份比對。
| 步驟 | 步驟完成後的 /etc/config/crontab | cron 是否重新啟動 |
|---|---|---|
| 安裝,尚未新增排程 | 與備份相同 | 否 |
套件執行期間,執行兩次 cronedit add | 多出兩行已加標記的排程 | 是,每次操作後 |
| 停止 | 與備份逐位元組一致 | 是 |
| 啟動 | 原先的兩行排程項目重新出現在檔案中 | 是 |
| 再次啟動 | 無變更,沒有重複的行 | 否 |
若停止程序未執行(例如斷電後),從 cronedit.tab 移除一行,再啟動套件 | 該行已移除,另一行保留 | 是 |
套件執行期間,先執行 cronedit add,再執行 cronedit remove | 該行先新增,再移除 | 是,每次操作後 |
qpkg_cli -R CronEdit | 與備份逐位元組一致,crontab -l 的輸出也相同 | 是 |
- 每個步驟完成後,其餘 58 行的內容和順序都與備份相同。
* * * * * /bin/date -u >> /share/Public/cronedit-demo.txt在一分鐘內寫入第一筆時間戳記,顯示 cron 會執行已加標記的排程。crontab載入檔案後,QTS 的crond.sh start會將 QTS 自己的vs_refresh和backup_conf.sh排程項目移到檔尾。因此,只要檔案中有 CronEdit 的排程項目,crontab -l列出的項目就可能與檔案中的項目相同,但順序不同。- HelloPublic 第一次啟動後,
hello.txt內有Hello world。第二次啟動後,檔案仍只有一行。停止和解除安裝後,檔案仍留在 Public 共用資料夾中。
10下載原始碼
ai-qpkg-examples.zip(170 KB)包含檢查後的兩個專案、兩段提示詞和一份 README。README 列出 Claude Code 撰寫的內容,以及檢查時做的變更。請將其中一個專案複製到您的專案資料夾,再執行 bvs qbuild 建置。