启动时自动创建相关软链接

This commit is contained in:
GoEdgeLab
2023-08-01 10:46:11 +08:00
parent f7c4b7c793
commit 9bd3491e5e

View File

@@ -1,6 +1,7 @@
package apps package apps
import ( import (
"errors"
"fmt" "fmt"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const" teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
@@ -10,6 +11,7 @@ import (
"github.com/iwind/gosock/pkg/gosock" "github.com/iwind/gosock/pkg/gosock"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@@ -87,8 +89,8 @@ func (this *AppCmd) Print() {
fmt.Println("") fmt.Println("")
fmt.Println("Options:") fmt.Println("Options:")
spaces := 20 var spaces = 20
max := 40 var max = 40
for _, option := range this.options { for _, option := range this.options {
l := len(option.Code) l := len(option.Code)
if l < max && l > spaces { if l < max && l > spaces {
@@ -126,7 +128,7 @@ func (this *AppCmd) On(arg string, callback func()) {
// Run 运行 // Run 运行
func (this *AppCmd) Run(main func()) { func (this *AppCmd) Run(main func()) {
// 获取参数 // 获取参数
args := os.Args[1:] var args = os.Args[1:]
if len(args) > 0 { if len(args) > 0 {
switch args[0] { switch args[0] {
case "-v", "version", "-version", "--version": case "-v", "version", "-version", "--version":
@@ -191,7 +193,7 @@ func (this *AppCmd) runStart() {
_ = os.Setenv("EdgeBackground", "on") _ = os.Setenv("EdgeBackground", "on")
cmd := exec.Command(os.Args[0]) var cmd = exec.Command(this.exe())
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
Foreground: false, Foreground: false,
Setsid: true, Setsid: true,
@@ -203,6 +205,9 @@ func (this *AppCmd) runStart() {
return return
} }
// create symbolic links
_ = this.createSymLinks()
fmt.Println(this.product+" started ok, pid:", cmd.Process.Pid) fmt.Println(this.product+" started ok, pid:", cmd.Process.Pid)
} }
@@ -277,3 +282,58 @@ func (this *AppCmd) ParseOptions(args []string) map[string][]string {
} }
return result return result
} }
func (this *AppCmd) exe() string {
var exe, _ = os.Executable()
if len(exe) == 0 {
exe = os.Args[0]
}
return exe
}
// 创建软链接
func (this *AppCmd) createSymLinks() error {
if runtime.GOOS != "linux" {
return nil
}
var exe, _ = os.Executable()
if len(exe) == 0 {
return nil
}
var errorList = []string{}
// bin
{
var target = "/usr/bin/" + teaconst.ProcessName
old, _ := filepath.EvalSymlinks(target)
if old != exe {
_ = os.Remove(target)
err := os.Symlink(exe, target)
if err != nil {
errorList = append(errorList, err.Error())
}
}
}
// log
{
var realPath = filepath.Dir(filepath.Dir(exe)) + "/logs/run.log"
var target = "/var/log/" + teaconst.ProcessName + ".log"
old, _ := filepath.EvalSymlinks(target)
if old != realPath {
_ = os.Remove(target)
err := os.Symlink(realPath, target)
if err != nil {
errorList = append(errorList, err.Error())
}
}
}
if len(errorList) > 0 {
return errors.New(strings.Join(errorList, "\n"))
}
return nil
}