Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go Standard Library Cookbook

You're reading from   Go Standard Library Cookbook Over 120 specific ways to make full use of the standard library components in Golang

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781788475273
Length 340 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Radomír Sohlich Radomír Sohlich
Author Profile Icon Radomír Sohlich
Radomír Sohlich
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Interacting with the Environment FREE CHAPTER 2. Strings and Things 3. Dealing with Numbers 4. Once Upon a Time 5. In and Out 6. Discovering the Filesystem 7. Connecting the Network 8. Working with Databases 9. Come to the Server Side 10. Fun with Concurrency 11. Tips and Tricks 12. Other Books You May Enjoy

Retrieving child process information

The recipe Calling an external process describes how to call the child process, synchronously and asynchronously. Naturally, to handle the process behavior you need to find out more about the process. This recipe shows how to obtain the PID and elementary information about the child process after it terminates.

The information about the running process could be obtained only via the syscall package and it is highly platform-dependent.

Getting ready

Test if the sleep (timeout for Windows) command exists in the Terminal.

How to do it...

  1. Open the console and create the folder chapter01/recipe09.
  2. Navigate to the directory.
  3. Create the main_running.go file with the following content:
        package main

import (
"fmt"
"os/exec"
"runtime"
)

func main() {

var cmd string
if runtime.GOOS == "windows" {
cmd = "timeout"
} else {
cmd = "sleep"
}
proc := exec.Command(cmd, "1")
proc.Start()

// No process state is returned
// till the process finish.
fmt.Printf("Process state for running process: %v\n",
proc.ProcessState)

// The PID could be obtain
// event for the running process
fmt.Printf("PID of running process: %d\n\n",
proc.Process.Pid)
}
  1. Run the code by executing go run main_running.go.
  2. See the output in the Terminal:
  1. Create the main.go file with the following content:
        func main() {

var cmd string
if runtime.GOOS == "windows" {
cmd = "timeout"
} else {
cmd = "sleep"
}

proc := exec.Command(cmd, "1")
proc.Start()

// Wait function will
// wait till the process ends.
proc.Wait()

// After the process terminates
// the *os.ProcessState contains
// simple information
// about the process run
fmt.Printf("PID: %d\n", proc.ProcessState.Pid())
fmt.Printf("Process took: %dms\n",
proc.ProcessState.SystemTime()/time.Microsecond)
fmt.Printf("Exited sucessfuly : %t\n",
proc.ProcessState.Success())
}
  1. Run the code by executing go run main.go.
  2. See the output in the Terminal:

How it works...

The os/exec standard library provides the way to execute the process. Using Command, the Cmd structure is returned. The Cmd provides the access to process the representation. When the process is running, you can only find out the PID.

There is only a little information that you can retrieve about the process. But by retrieving the PID of the process, you are able to call the utilities from the OS to get more information.

Remember that it is possible to obtain the PID of the child process, even if it is running. On the other hand, the ProcessState structure of the os package is available, only after the process terminates.

See also

There are Reading/writing from the child process and Calling an external process recipes in this chapter that are related to process handling.

You have been reading a chapter from
Go Standard Library Cookbook
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781788475273
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime