I want to switch to the bash shell from my custom shell in go.
I am not sure how to sleep my parent process (custom shell) and switch to the child process (bash)
This is my part of the code.
cmd := exec.Command("bash", "-c", "/bin/bash")
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf(err.Error())
}
fmt.Printf("%s
", stdoutStderr)
I want to do it as follows:
myshell >> /bin/bash
$ /bin/myshell
myshell >>
the code is exec but not fork
binary, lookErr := exec.LookPath("/bin/bash")
if lookErr != nil {
panic(lookErr)
}
args := []string{"/bin/bash"}
env := os.Environ()
execErr := syscall.Exec(binary, args, env)
if execErr != nil {
panic(execErr)
}
so if I exit the bash shell, of course, my custom shell is killed.
myshell> bash
bash-3.2$ exit
exit
This is a recipe for how to stop a process and let the parent shell process take over control. I'm not sure if you can use it to do the opposite.
The idea is to send a stop signal to yourself.
pid := os.Getpid()
process, err := os.FindProcess(pid)
if err != nil {
err = errors.Wrapf(err, "error pausing process %+v", process)
return
}
process.Signal(syscall.SIGSTOP)
Earlier you need to listen in a goroutine to continue signal to reinitialize your process if needed
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGCONT, syscall.SIGTERM)
for signal := range signals {
switch signal {
case syscall.SIGCONT:
// reinitialize
case syscall.SIGTERM:
// stop
}
}
I have resolved this problem by this way.
package main
import (
"os/exec"
)
func main() {
cmd := exec.Command("sh", "-c", "bash")
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
}