Golang Daemon Usage Example _golang

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.