在Go中启动后守护进程

时间:2018-10-21 10:16:39

标签: go daemon

我有一个需要作为守护程序运行的系统,该系统已经过开发。通常,我可以通过诸如systemd之类的东西甚至只是像' ./ project&'之类的简单东西来管理它,但是这个特定的项目需要先获得一些输入,然后才能成为守护程序-它需要密码要输入。

该密码不能作为命令行参数,放置在配置文件中或在源代码中进行硬编码等。必须由在系统启动时了解密码的人手动提供。

在启动时,项目需要收集密码,验证是否正确,只有提供了正确的密码(通过尝试解密证书进行验证),项目才能真正开始作为守护程序运行。 / p>

是否有任何方法可以启动项目,接受来自stdin的输入,执行所需的任何验证等,然后才成为守护进程(与stdin分离等)?

当前,我可以通过启动它来模拟所需的行为,并在输入密码后使用“ ctrl + z”将其挂起,然后使用bg将其推送到后台。当然,必须有一种从流程本身内部做到这一点的方法...

2 个答案:

答案 0 :(得分:1)

这是一个符合我的意图的程序。

因此该程序仅检查密码。实际的守护程序为 Sub b1CIF() Dim CustList As Workbook Dim thisWB As Workbook Dim thisWS As Worksheet Dim wsRR As Worksheet Dim bColor As Range Dim Msg, Style, Title, Response Msg = "OOOPS!" & vbNewLine & vbNewLine & "The CIF Number of " & LendStart.lsPBCIF.Value & " " & "is not correct or does not exist." & vbNewLine & "Please re-enter the CIF Number." Style = vbOKCancel + vbCritical Title = UCase("***CIF Data Entry Error!***") Application.ScreenUpdating = False Set CustList = Workbooks.Open("L:\Deposits\Information\Customers.xlsm") Set thisWB = ThisWorkbook Set thisWS = thisWB.Sheets("SavedInfo") Set wsRR = thisWB.Sheets("RiskRating") Set bColor = wsRR.Range("C3") On Error GoTo ErrHandler ' NAME GRAB If thisWS.Range("A2") <> "" Then thisWS.Range("PBName").Value = _ WorksheetFunction.VLookup(thisWS.Range("A2").Value, CustList.Sheets("CIFMAST").Range("A1:Z50000"), 2, False) With LendStart.lsPBName .Value = thisWS.Range("PBName") .Visible = True .Locked = True .BackColor = bColor.Interior.Color .Font.Bold = True .Font.Size = 9 .TextAlign = fmTextAlignCenter .TabStop = False End With thisWB.Sheets("BorrInfo").Range("PB").Value = thisWS.Range("PBName") ' TELEPHONE NUMBER GRAB thisWB.Sheets("BorrInfo").Range("PBPhone").Value = _ WorksheetFunction.VLookup(thisWS.Range("A2").Value, CustList.Sheets("CIFMAST").Range("A1:Z50000"), 9, False) End If CustList.Close Application.ScreenUpdating = True Exit Sub ErrHandler: LendStart.lsPBSCIF.Value = "" With LendStart.lsPBName .Value = "" .Locked = True End With Response = MsgBox(Msg, Style, Title) CustList.Close Application.ScreenUpdating = True End Sub Sub b2CIF() Dim CustList As Workbook Dim thisWB As Workbook Dim thisWS As Worksheet Dim wsRR As Worksheet Dim bColor As Range Dim Msg, Style, Title, Response Msg = "The CIF Number entered " & LendStart.lsPBSCIF.Value & " " & "is not correct." & vbNewLine & "Please re-enter the CIF Number." Style = vbOKCancel + vbCritical Title = UCase("***CIF data entry error!***") Application.ScreenUpdating = False Set CustList = Workbooks.Open("L:\Deposits\Information\Customers.xlsm") Set thisWB = ThisWorkbook Set thisWS = thisWB.Sheets("SavedInfo") Set wsRR = thisWB.Sheets("RiskRating") Set bColor = wsRR.Range("C3") On Error GoTo ErrHandler ' NAME GRAB If thisWS.Range("A3") <> "" Then thisWS.Range("PBSName").Value = _ WorksheetFunction.VLookup(thisWS.Range("A3").Value, CustList.Sheets("CIFMAST").Range("A1:Z50000"), 2, False) With LendStart.lsPBSName .Value = thisWS.Range("PBSName") .Visible = True .Locked = True .BackColor = bColor.Interior.Color .Font.Bold = True .Font.Size = 9 .TextAlign = fmTextAlignCenter .TabStop = False End With thisWB.Sheets("BorrInfo").Range("PBS").Value = thisWS.Range("PBSName") ' TELEPHONE NUMBER GRAB thisWB.Sheets("BorrInfo").Range("PBSPhone").Value = _ WorksheetFunction.VLookup(thisWS.Range("A3").Value, CustList.Sheets("CIFMAST").Range("A1:Z50000"), 9, False) End If CustList.Close Application.ScreenUpdating = True Exit Sub ErrHandler: LendStart.lsPBSCIF.Value = "" Response = MsgBox(Msg, Style, Title) CustList.Close Application.ScreenUpdating = True End Sub ,仅当密码签出时才被调用。

webserver

我在这里使用的守护程序是从这里获取的

https://github.com/sevlyar/go-daemon/blob/master/examples/cmd/gd-simple/simple.go

在等待输入和作为守护程序的情况下都无法运行单个程序。

答案 1 :(得分:0)

如果传递给定的标志(例如加密的凭据),则可以使用标志并谨慎地控制应用程序流,并将其自身作为守护程序运行。甚至将它们存储在临时文件,数据库或任何地方。

func main() {

    cred := flag.String("cred", "", "raw cred")

    flag.Parse()

    if *cred == "" {
        fmt.Print("Enter credentials:\n")
        decryptedCred, _ := bufio.NewReader(os.Stdin).ReadString('\n')

        if !validCred(decryptedCred) {
            os.Exit(1)
        }

        encryptedCred := encryptCredentials(decryptedCred)

        cmd := exec.Command("./project", fmt.Sprintf("-cred=%s", encryptedCred), "&")
        cmd.Start()

        fmt.Printf("Started project with pid: %d\n", cmd.Process.Pid)

        os.Exit(0)
    }

    for {
        // start app
        // use *cred here
    }
}

无论采用哪种方法,我都可能会跟踪pid等。

./project
# Enter credentials:
myCredentialsString
# Started project with pid: 11702
ps ax | grep project
# 11667 s001  R      0:02.47 ./project -cred=N/esPq8wsWn4/+Gco16ddl9UnJ0= &\012

希望这会有所帮助