The examples in this article describe the Golang daemon usage. Share to everyone for your reference, specific as follows:
With node wrote a socket background service, but sometimes hang, node an exception on the game over, so wrote a waiting.
Copy Code code as follows:
Package Main
Import (
"Log"
"OS"
"Os/exec"
"Time"
)
Func Main () {
LF, err: = OS. OpenFile ("Angel.txt", OS. O_create | Os. O_rdwr | Os. O_append, 0600)
If Err!= nil {
Os. Exit (1)
}
Defer LF. Close ()
Log
L: = log. New (LF, "", OS. O_append)
for {
CMD: = Exec.command ("/usr/local/bin/node", "/*****.js")
ERR: = cmd. Start ()
If Err!= nil {
l.printf ("%s Start command failed", time. Now (). Format ("2006-01-02 15:04:05"), err)
Time. Sleep (time. Second * 5)
Continue
}
l.printf ("%s process started", time.) Now (). Format ("2006-01-02 15:04:05"), err)
Err = cmd. Wait ()
l.printf ("%s process exits", time.) Now (). Format ("2006-01-02 15:04:05"), err)
Time. Sleep (time. Second * 1)
}
}
There is also a shell implementation. Remember to give executive permission Oh, chmod +x You_command
Copy Code code as follows:
#! /bin/bash
While true; Todo
./you_command
Done
I hope this article will help you with your go language program.