如何编写可以切换到bash的shell?

时间:2019-06-06 07:33:31

标签: bash shell unix go

我想从我的自定义外壳切换到bash外壳。

我不确定如何休眠我的父进程(自定义外壳程序)并切换到子进程(bash)

这是我代码的一部分。

public class AddPaginationHeader : Attribute, IAsyncResultFilter
{
    // AddPaginationHeader Implementation (Result Filter)
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        //PagingModel model = new PagingModel();
        //await controller.TryUpdateModelAsync(model);
        var result = context.Result as ObjectResult;
        if (result?.Value != null && result?.StatusCode >= 200 &&
            result?.StatusCode < 300)
        {
            // Trying to get the pagingModel (but getting a ParameterDescriptor type)
            var controller = context.Controller as Controller;
            var parameterDescriptor = context.ActionDescriptor.Parameters.Where(t => t.Name.Equals("pagingModel")).FirstOrDefault();
            var parameter = Activator.CreateInstance(parameterDescriptor.ParameterType);
            await controller.TryUpdateModelAsync(parameter, parameterDescriptor.ParameterType, "");
            var pagingModel = parameter;
            //Getting the media type
            string mediaType = context.HttpContext.Request.Headers["Accept"];
            // Getting the query string itself
            string queryString = context.HttpContext.Request.QueryString.ToString();
            //Implementation of the actual logic that needs the paging model
            // ...
            next();
        }
        //return Task.CompletedTask;
    }
}

我要这样做:

    cmd := exec.Command("bash", "-c", "/bin/bash")
    stdoutStderr, err := cmd.CombinedOutput()
    if err != nil {
        fmt.Printf(err.Error())
    }
    fmt.Printf("%s\n", stdoutStderr)

代码是exec而不是fork

myshell >> /bin/bash
$ /bin/myshell
myshell >>

因此,如果我退出bash shell,我的自定义shell将被杀死。

    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)
    }

2 个答案:

答案 0 :(得分:0)

这是如何停止进程并让父Shell进程接管控制的方法。我不确定是否可以用它来做相反的事情。

想法是向自己发送停止信号。

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)

来源https://github.com/grzegorz-zur/bare-minimum/blob/95fb116631b6146707c47455d8ffce6bb5b8717c/editor.go#L152

更早之前,您需要侦听goroutine以继续发出信号,以便在需要时重新初始化您的进程

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
    }
}

来源https://github.com/grzegorz-zur/bare-minimum/blob/95fb116631b6146707c47455d8ffce6bb5b8717c/editor.go#L87

答案 1 :(得分:0)

我已经通过这种方式解决了这个问题。

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()
}