Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. //ALARM! Have a problen in Debian. processes is flowing...
  2. // borning new fork processes and don't killing them
  3. // In Ubuntu have not this problem
  4.  
  5. cmds := []*exec.Cmd{
  6. exec.Command("journalctl", "-u", "docker.service", "--since", "8 days ago"),
  7. exec.Command("grep", "level=error"),
  8. }
  9.  
  10. // Buffer is a goroutine safe bytes.Buffer
  11. type Buffer struct {
  12. buffer bytes.Buffer
  13. mutex sync.Mutex
  14. }
  15.  
  16. // Write appends the contents of p to the buffer, growing the buffer as needed. It returns
  17. // the number of bytes written.
  18. func (s *Buffer) Write(p []byte) (n int, err error) {
  19. s.mutex.Lock()
  20. defer s.mutex.Unlock()
  21. return s.buffer.Write(p)
  22. }
  23.  
  24. // String returns the contents of the unread portion of the buffer
  25. // as a string. If the Buffer is a nil pointer, it returns "<nil>".
  26. func (s *Buffer) String() string {
  27. s.mutex.Lock()
  28. defer s.mutex.Unlock()
  29. return s.buffer.String()
  30. }
  31.  
  32. func pipeline(cmds ...*exec.Cmd) (bytes.Buffer, bytes.Buffer, error) {
  33. var output Buffer
  34. var stderr Buffer
  35.  
  36. if len(cmds) < 1 {
  37. return output.buffer, stderr.buffer, errors.New("empty commands")
  38. }
  39.  
  40. last := len(cmds) - 1
  41. for i, cmd := range cmds[:last] {
  42. var err error
  43. if cmds[i+1].Stdin, err = cmd.StdoutPipe(); err != nil {
  44. return output.buffer, stderr.buffer, err
  45. }
  46. cmd.Stderr = &stderr
  47. }
  48.  
  49. cmds[last].Stdout = &output.buffer
  50. cmds[last].Stderr = &stderr.buffer
  51.  
  52. for _, cmd := range cmds {
  53. if err := cmd.Start(); err != nil {
  54. return output.buffer, stderr.buffer, err
  55. }
  56. }
  57.  
  58. for _, cmd := range cmds {
  59. if err := cmd.Wait(); err != nil {
  60. return output.buffer, stderr.buffer, err
  61. }
  62. }
  63.  
  64. return output.buffer, stderr.buffer, nil
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement