Anomaly in RunCommand and the exit status of the child process

70 views
Skip to first unread message

Subramanian Sridharan

unread,
Jan 29, 2020, 3:20:25 AM1/29/20
to golang-nuts
Hi guys,

I'm in need of using the "script" command (more info about script in the end) in Linux to run a couple of commands and get the exit status of the second command.

Here is my code snippet:

package main


import (
 
"fmt"
 
"os/exec"
)


func main
() {
 op
, err := exec.Command("script", "-e", "-q", "-c", "date; exit 1").CombinedOutput()
 
if err != nil {
 fmt
.Println("Error while running command:", err)
 fmt
.Println(string(op))
 
return
 
}
 fmt
.Println("Successfully ran the command.")
 fmt
.Println(string(op))
}

When I compile this code and run it on two Red Hat VMs, they give me different results.

Working as expected in a Red Hat 8 VM:
[root@dc-rhel8-1 tmp]# ./script_command
Error while running command: exit status 1
Wed Jan 29 21:41:20 IST 2020


[root@dc-rhel8-1 tmp]# script -e -q -c "date; exit 1"
Wed Jan 29 21:42:11 IST 2020
[root@dc-rhel8-1 tmp]# echo $?
1
[root@dc-rhel8-1 tmp]# script --version
script
from util-linux 2.32.1

Incorrect working in a Red Hat 7 VM:
[root@dc-rhel7-dev tmp]# ./script_command
Successfully ran the command.
Wed Jan 29 21:41:41 IST 2020


[root@dc-rhel7-dev tmp]# script -e -q -c "date; exit 1"
Wed Jan 29 21:42:22 IST 2020
[root@dc-rhel7-dev tmp]# echo $?
1
[root@dc-rhel7-dev tmp]# script --version
script
from util-linux 2.23.2

Is this behaviour an issue in Go? Or am I missing something?

Thanks in advance!

Some information on the script command:
By default, when I pipe output from RunCommand, the default terminal width is 80, due to which the output will be scrambled and hard to parse.
So, I'm using the script command and stty to set the width to a bigger value.
-e is used to return the exit status of the child process
-q is used to keep output minimal
-c is used to specify the command that is to be run

Ian Lance Taylor

unread,
Jan 29, 2020, 9:20:24 AM1/29/20
to Subramanian Sridharan, golang-nuts
I don't know what is happening here. It's fairly unlikely that it's a
problem with Go, but it's not entirely impossible. The next debugging
step would be to run the Go program under `strace -f` and see what the
wait4 system call returns when the "script" command completes.

Ian

Brian Candler

unread,
Feb 3, 2020, 2:26:03 PM2/3/20
to golang-nuts
I just tried this on CentOS 7.4:

# script -e -q -c "date; exit 1"; echo $?
Mon  3 Feb 19:21:17 GMT 2020
1

# script -e -q -c "date; exit 1" </dev/null; echo $?
0
# Mon  3 Feb 19:21:21 GMT 2020

# script -e -c "date; exit 1" </dev/null; echo $?
Script started, file is typescript
Script done, file is typescript
0
# Mon  3 Feb 19:22:55 GMT 2020

This looks similar to what you observe - but without bringing golang into the equation.  It might be that this version of script reads from stdin before executing the command; or maybe it's unhappy if stdin is not a tty.
Reply all
Reply to author
Forward
0 new messages