Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ALARM! Have a problen in Debian. processes is flowing...
- // borning new fork processes and don't killing them
- // In Ubuntu have not this problem
- cmds := []*exec.Cmd{
- exec.Command("journalctl", "-u", "docker.service", "--since", "8 days ago"),
- exec.Command("grep", "level=error"),
- }
- // Buffer is a goroutine safe bytes.Buffer
- type Buffer struct {
- buffer bytes.Buffer
- mutex sync.Mutex
- }
- // Write appends the contents of p to the buffer, growing the buffer as needed. It returns
- // the number of bytes written.
- func (s *Buffer) Write(p []byte) (n int, err error) {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- return s.buffer.Write(p)
- }
- // String returns the contents of the unread portion of the buffer
- // as a string. If the Buffer is a nil pointer, it returns "<nil>".
- func (s *Buffer) String() string {
- s.mutex.Lock()
- defer s.mutex.Unlock()
- return s.buffer.String()
- }
- func pipeline(cmds ...*exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
- var output Buffer
- var stderr Buffer
- if len(cmds) < 1 {
- return output.buffer, stderr.buffer, errors.New("empty commands")
- }
- last := len(cmds) - 1
- for i, cmd := range cmds[:last] {
- var err error
- if cmds[i+1].Stdin, err = cmd.StdoutPipe(); err != nil {
- return output.buffer, stderr.buffer, err
- }
- cmd.Stderr = &stderr
- }
- cmds[last].Stdout = &output.buffer
- cmds[last].Stderr = &stderr.buffer
- for _, cmd := range cmds {
- if err := cmd.Start(); err != nil {
- return output.buffer, stderr.buffer, err
- }
- }
- for _, cmd := range cmds {
- if err := cmd.Wait(); err != nil {
- return output.buffer, stderr.buffer, err
- }
- }
- return output.buffer, stderr.buffer, nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement