An example / demo project for managing a child process in the same way across Windows/Mac/Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
forest 2bffd57df8 fix log output 3 years ago
.gitignore correct package and gitignore 3 years ago
README.md fix log output 3 years ago
go.mod correct package and gitignore 3 years ago
go.sum first commit 3 years ago
main.go first commit 3 years ago
process_posix.go first commit 3 years ago
process_windows.go first commit 3 years ago

README.md

compiling

GOOS=windows GOARCH=amd64 go build  -tags 'osusergo netgo'  -ldflags='-extldflags=-static' -o proctest.exe

output on linux

forest@thingpad:~/Desktop/proctest$ go run . 1
2021/10/08 13:49:38 /tmp/go-build1719441506/b001/exe/proctest
2021/10/08 13:49:38 child process is running!
2021/10/08 13:49:39 I am the Child!! i=0
2021/10/08 13:49:39 
2021/10/08 13:49:39 golang sez: hasProcess: true, processPid: 139005, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 13:49:39 Child process (139005) is ALIVE!!!
2021/10/08 13:49:39 
2021/10/08 13:49:39 I am the Child!! i=1
2021/10/08 13:49:40 I am the Child!! i=2
2021/10/08 13:49:40 parent waiting for child to actually die after calling terminateProcessOSIndependent...
2021/10/08 13:49:40 2021/10/08 13:49:40 child process recieved signal: interrupt
2021/10/08 13:49:40 2021/10/08 13:49:40 I AM NOT GOING DOWN WITHOUT A FIGHT! Sleeping for 1 seconds before exiting...
2021/10/08 13:49:40 I am the Child!! i=3
2021/10/08 13:49:41 .
2021/10/08 13:49:41 I am the Child!! i=4
2021/10/08 13:49:41 child process is exiting
2021/10/08 13:49:41 .
2021/10/08 13:49:41 child process 'test child' ended with exit code 0
2021/10/08 13:49:42 the child process is dead from the parents POV
2021/10/08 13:49:42 
2021/10/08 13:49:42 golang sez: hasProcess: true, processPid: 139005, osProcessExists: true,
                    hasProcessState: true, processStatePid: 139005, processStateExited: true, processStateExitCode: 0
2021/10/08 13:49:42 Child process (139005) is no longer running.
2021/10/08 13:49:42 
2021/10/08 13:49:42 
2021/10/08 13:49:42 golang sez: hasProcess: true, processPid: 139005, osProcessExists: true,
                    hasProcessState: true, processStatePid: 139005, processStateExited: true, processStateExitCode: 0
2021/10/08 13:49:42 Child process (139005) is no longer running.
2021/10/08 13:49:42 
2021/10/08 13:49:43 
2021/10/08 13:49:43 golang sez: hasProcess: true, processPid: 139005, osProcessExists: true,
                    hasProcessState: true, processStatePid: 139005, processStateExited: true, processStateExitCode: 0
2021/10/08 13:49:43 Child process (139005) is no longer running.
2021/10/08 13:49:43 
forest@thingpad:~/Desktop/proctest$ go run . 10
2021/10/08 13:49:50 /tmp/go-build278945089/b001/exe/proctest
2021/10/08 13:49:50 child process is running!
2021/10/08 13:49:50 I am the Child!! i=0
2021/10/08 13:49:51 
2021/10/08 13:49:51 golang sez: hasProcess: true, processPid: 139116, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 13:49:51 Child process (139116) is ALIVE!!!
2021/10/08 13:49:51 
2021/10/08 13:49:51 I am the Child!! i=1
2021/10/08 13:49:51 I am the Child!! i=2
2021/10/08 13:49:52 parent waiting for child to actually die after calling terminateProcessOSIndependent...
2021/10/08 13:49:52 2021/10/08 13:49:52 child process recieved signal: interrupt
2021/10/08 13:49:52 2021/10/08 13:49:52 I AM NOT GOING DOWN WITHOUT A FIGHT! Sleeping for 10 seconds before exiting...
2021/10/08 13:49:52 I am the Child!! i=3
2021/10/08 13:49:52 .
2021/10/08 13:49:52 I am the Child!! i=4
2021/10/08 13:49:53 .
2021/10/08 13:49:53 I am the Child!! i=5
2021/10/08 13:49:53 .
2021/10/08 13:49:53 I am the Child!! i=6
2021/10/08 13:49:54 .
2021/10/08 13:49:54 I am the Child!! i=7
2021/10/08 13:49:54 .
2021/10/08 13:49:54 
2021/10/08 13:49:54 golang sez: hasProcess: true, processPid: 139116, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 13:49:54 Child process (139116) is ALIVE!!!
2021/10/08 13:49:54 
2021/10/08 13:49:54 parent process is fed up, now calling killProcessOSIndependent
2021/10/08 13:49:54 
2021/10/08 13:49:54 golang sez: hasProcess: true, processPid: 139116, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 13:49:54 Child process (139116) is ALIVE!!!
2021/10/08 13:49:54 
2021/10/08 13:49:54 command.Wait() returned 'signal: killed' for child process 'test child'
2021/10/08 13:49:54 child process 'test child' ended with exit code -1
2021/10/08 13:49:55 
2021/10/08 13:49:55 golang sez: hasProcess: true, processPid: 139116, osProcessExists: true,
                    hasProcessState: true, processStatePid: 139116, processStateExited: false, processStateExitCode: -1
2021/10/08 13:49:55 Child process (139116) is no longer running.
2021/10/08 13:49:55 

output on windows

PS C:\Program Files (x86)\greenhouse-desktop\background-service> ./test4.exe 1
2021/10/08 11:10:11 C:\Program Files (x86)\greenhouse-desktop\background-service\test4.exe
2021/10/08 11:10:11 child process is running!
2021/10/08 11:10:12 I am the Child!! i=0
2021/10/08 11:10:12
2021/10/08 11:10:12 golang sez: hasProcess: true, processPid: 3940, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 11:10:12 Child process (3940) is ALIVE!!!
2021/10/08 11:10:12
2021/10/08 11:10:12 I am the Child!! i=1
2021/10/08 11:10:13 I am the Child!! i=2
2021/10/08 11:10:13 parent waiting for child to actually die after calling terminateProcessOSIndependent...
2021/10/08 11:10:13 child process recieved signal: interrupt
2021/10/08 11:10:13 I AM NOT GOING DOWN WITHOUT A FIGHT! Sleeping for 1 seconds before exiting...
2021/10/08 11:10:13 I am the Child!! i=3
2021/10/08 11:10:14 .
2021/10/08 11:10:14 I am the Child!! i=4
2021/10/08 11:10:14 child process is exiting
2021/10/08 11:10:14 child process 'test child' ended with exit code 0
2021/10/08 11:10:14 the child process is dead from the parents POV
2021/10/08 11:10:14
2021/10/08 11:10:14 golang sez: hasProcess: true, processPid: 3940, osProcessExists: true,
                    hasProcessState: true, processStatePid: 3940, processStateExited: true, processStateExitCode: 0
2021/10/08 11:10:14 Child process (3940) is no longer running.
2021/10/08 11:10:14
2021/10/08 11:10:14
2021/10/08 11:10:14 golang sez: hasProcess: true, processPid: 3940, osProcessExists: true,
                    hasProcessState: true, processStatePid: 3940, processStateExited: true, processStateExitCode: 0
2021/10/08 11:10:14 Child process (3940) is no longer running.
2021/10/08 11:10:14
2021/10/08 11:10:15
2021/10/08 11:10:15 golang sez: hasProcess: true, processPid: 3940, osProcessExists: true,
                    hasProcessState: true, processStatePid: 3940, processStateExited: true, processStateExitCode: 0
2021/10/08 11:10:15 Child process (3940) is no longer running.
2021/10/08 11:10:15
PS C:\Program Files (x86)\greenhouse-desktop\background-service> ./test4.exe 10
2021/10/08 11:10:30 C:\Program Files (x86)\greenhouse-desktop\background-service\test4.exe
2021/10/08 11:10:30 child process is running!
2021/10/08 11:10:31 I am the Child!! i=0
2021/10/08 11:10:31
2021/10/08 11:10:31 golang sez: hasProcess: true, processPid: 8404, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 11:10:31 Child process (8404) is ALIVE!!!
2021/10/08 11:10:31
2021/10/08 11:10:31 I am the Child!! i=1
2021/10/08 11:10:32 I am the Child!! i=2
2021/10/08 11:10:32 parent waiting for child to actually die after calling terminateProcessOSIndependent...
2021/10/08 11:10:32 child process recieved signal: interrupt
2021/10/08 11:10:32 I AM NOT GOING DOWN WITHOUT A FIGHT! Sleeping for 10 seconds before exiting...
2021/10/08 11:10:32 I am the Child!! i=3
2021/10/08 11:10:33 .
2021/10/08 11:10:33 I am the Child!! i=4
2021/10/08 11:10:33 .
2021/10/08 11:10:33 I am the Child!! i=5
2021/10/08 11:10:34 .
2021/10/08 11:10:34 I am the Child!! i=6
2021/10/08 11:10:34 .
2021/10/08 11:10:34 I am the Child!! i=7
2021/10/08 11:10:35 .
2021/10/08 11:10:35
2021/10/08 11:10:35 golang sez: hasProcess: true, processPid: 8404, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 11:10:35 Child process (8404) is ALIVE!!!
2021/10/08 11:10:35
2021/10/08 11:10:35 I am the Child!! i=8
2021/10/08 11:10:35 parent process is fed up, now calling killProcessOSIndependent
2021/10/08 11:10:35
2021/10/08 11:10:35 golang sez: hasProcess: true, processPid: 8404, osProcessExists: true,
                    hasProcessState: false, processStatePid: nil, processStateExited: nil, processStateExitCode: nil
2021/10/08 11:10:35 Child process (8404) is ALIVE!!!
2021/10/08 11:10:35
2021/10/08 11:10:35 command.Wait() returned 'exit status 1' for child process 'test child'
2021/10/08 11:10:35 child process 'test child' ended with exit code 1
2021/10/08 11:10:36
2021/10/08 11:10:36 golang sez: hasProcess: true, processPid: 8404, osProcessExists: true,
                    hasProcessState: true, processStatePid: 8404, processStateExited: true, processStateExitCode: 1
2021/10/08 11:10:36 Child process (8404) is no longer running.
2021/10/08 11:10:36