Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Go Systems Programming

You're reading from   Go Systems Programming Master Linux and Unix system level programming with Go

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787125643
Length 466 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (20) Chapters Close

Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Getting Started with Go and Unix Systems Programming 2. Writing Programs in Go FREE CHAPTER 3. Advanced Go Features 4. Go Packages, Algorithms, and Data Structures 5. Files and Directories 6. File Input and Output 7. Working with System Files 8. Processes and Signals 9. Goroutines - Basic Features 10. Goroutines - Advanced Features 11. Writing Web Applications in Go 12. Network Programming Index

Process management


Go is not that good at dealing with processes and process management in general. Nevertheless, this section will present a small Go program that lists all the processes of a Unix machine by executing a Unix command and getting its output. The name of the program will be listProcess.go. It works on both Linux and macOS systems, and will be presented in three parts.

The first part of the program is the following:

package main 
 
import ( 
   "fmt" 
   "os" 
   "os/exec" 
   "syscall" 
) 

The second part of listProcess.go has the following Go code:

func main() { 
 
   PS, err := exec.LookPath("ps") 
   if err != nil { 
         fmt.Println(err) 
   } 
fmt.Println(PS) 
 
   command := []string{"ps", "-a", "-x"} 
   env := os.Environ() 
   err = syscall.Exec(PS, command, env) 

As you can see, you first need to get the path of the executable file using exec.LookPath() to make sure that you are not going to accidentally execute another binary file and then define the command you want...

lock icon The rest of the chapter is locked
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 $15.99/month. Cancel anytime
Visually different images