I have a bash script postinstall.sh that executes normally when run from the terminal
./postinstall.sh
the script runs a bunch of commands, creating directorys, chown,chmod files and directorys,
at the end of the script is
echo "Done"
exit 0
When i run this as a exec.Cmd in go, and call the cmd.Wait() funtion, it never returns.
So I modifed my code to use the package from https://github.com/go-cmd/cmd since I wanted to be able to log the output of the scripts I run in 'real time' but also detect when they are complete. So after a bunch of work I can now log the stdOut of the Cmd to my logging at logLevel STATUS and the stdErr of the Cmd to my logLevel ERROR. I can also set a timeout for the command, and log its progress in real time.
This new process works as expected for some simple cases, but when I thow the big postinstall.sh script at it, it runs all the way to the end, and then hangs, and eventually times out. The process takes a few seconds when run manually on the command line, I set the timeout to be 30 seconds. the logging shows the
STATUS- Done
which is the line right before the
exit 0
The docs for the go-cmd package show a non locking way to tell that the process has finished, which I am checking (and works my test cases) I also am checking the cmd.Status().finished flag (bool) and neither of these are reporting that the process has finished. This is the same result I was seeing by just using the cmd.Wait() function from the built in exec.Cmd, only now I can see that the script makes it all the way to the end, but for some reason is either not returning, or the go program can not tell that is has returned.
Some of what the script is doing is starting or restarting services in /etc/init.d, if one of those launches a process in the backround, since the parent process is the postinstall script could that cause the process to not fully complete in the eye of the go program?
Or any other types of commands that could cause this type of behavior?
I may just have to start removing parts of the script and see if I can get it to complete to narrow down what is causing this to act different then my test script, but its around 100 lines, and the process to get the script into the device is a multi step process.
testscript was something along the lines of
#!/bin/bash
echo "stdOut"
sleep 1
>2& echo "stdErr"
sleep 2
echo "Finsihed"
sleep 1
and this worked as expected(timed out when limit was 1 second, showed completed when timeout was set to 4 seconds)
Should Also note that the process works on other scripts. this is part of an updating process that does a preinstall.sh, then uses this same process to call tar -xvf on the file containing the update, and then goes on to this postinstall script. So most of the time my process succesfully detects that the process has exited, there is just something about the postinstall thats the issue, but I cant think of what would make it hang in go, but exit fine when run from the terminal.