Skip to content

Commit 89b78c6

Browse files
committed
feat(app): add env var to specify PID file
1 parent 30b2605 commit 89b78c6

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Simple command line based HTTP file server to share local file system.
1414

1515
## Compile
1616
Minimal required Go version is 1.16.
17-
```bash
17+
```sh
1818
GO111MODULE=auto go build src/main.go
1919
```
2020
Will generate executable file "main" in current directory.
@@ -287,6 +287,9 @@ ghfs [options]
287287
To prevent outputting additional information on console, like accessible URLs, etc,
288288
set value to "1".
289289

290+
### GHFS_PID_FILE
291+
Specify PID file. PID will be written into the file on application startup.
292+
290293
## Shortcut key for default theme
291294
- ``, ``: move focus between path items
292295
- `Ctrl`/`Opt` + ``: move focus to first path item

README.zh-CN.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## 编译
1616
至少需要Go 1.16版本。
17-
```bash
17+
```sh
1818
GO111MODULE=auto go build src/main.go
1919
```
2020
会在当前目录生成"main"可执行文件。
@@ -284,6 +284,9 @@ ghfs [选项]
284284
### GHFS_QUIET
285285
为避免在控制台输出额外信息,例如可访问的URL等,可将值设为“1”。
286286

287+
### GHFS_PID_FILE
288+
指定进程ID文件。进程ID会在应用启动时被写入文件。
289+
287290
## 默认主题的快捷键
288291
- ``, ``:使焦点在路径项之间移动
289292
- `Ctrl`/`Opt` + ``:把焦点移动到第一个路径项

src/app/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"os"
1414
"path/filepath"
15+
"strconv"
1516
)
1617

1718
type App struct {
@@ -36,6 +37,8 @@ func (app *App) ReOpenLog() {
3637
}
3738

3839
func NewApp(params []*param.Param) *App {
40+
writePidFile()
41+
3942
verbose := !util.GetBoolEnv("GHFS_QUIET")
4043

4144
if serverHandler.TryEnableWSL1Fix() && verbose {
@@ -115,3 +118,18 @@ func NewApp(params []*param.Param) *App {
115118
logFileMan: logFileMan,
116119
}
117120
}
121+
122+
func writePidFile() {
123+
pidFilename := os.Getenv("GHFS_PID_FILE")
124+
if len(pidFilename) == 0 {
125+
return
126+
}
127+
128+
pidContent := strconv.Itoa(os.Getpid())
129+
pidFile, err := os.OpenFile(pidFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
130+
if !serverErrHandler.CheckFatal(err) {
131+
_, err := pidFile.WriteString(pidContent)
132+
err2 := pidFile.Close()
133+
serverErrHandler.CheckFatal(err, err2)
134+
}
135+
}

test/case/037.pid.file.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
cleanup() {
4+
rm -f "$fs"/vhost1/pid.txt
5+
}
6+
7+
source "$root"/lib.bash
8+
9+
GHFS_PID_FILE="$fs"/vhost1/pid.txt "$ghfs" -l 3003 -r "$fs"/vhost1 &
10+
sleep 0.05 # wait server ready
11+
12+
assert $(cat "$fs"/vhost1/pid.txt) $!
13+
14+
cleanup
15+
jobs -p | xargs kill

0 commit comments

Comments
 (0)