Maison  >  Article  >  développement back-end  >  L'exécution de l'exécutable sur la ligne de commande est une bonne chose, mais son exécution via un autre programme entraîne une absence de réponse.

L'exécution de l'exécutable sur la ligne de commande est une bonne chose, mais son exécution via un autre programme entraîne une absence de réponse.

WBOY
WBOYavant
2024-02-08 20:42:101003parcourir

Lexécution de lexécutable sur la ligne de commande est une bonne chose, mais son exécution via un autre programme entraîne une absence de réponse.

Contenu de la question

Exécutez simplement le fichier exécutable et les paramètres dans l'invite de commande Windows :

cgx_STATIC.exe -b C:\Users\m3\AppData\Local\Temp\shot-277325955.fbd

Cependant, lors de l'exécution du même exécutable via Golang, une fois que l'exécutable a créé des fichiers de sortie, l'exécutable ne répond plus.

<code>
// Run an executable and print its log into a file.
func RunWithLogFile(pthExe string, arg []string, fLog *os.File) error {
    cmd := exec.Command(pthExe, arg...)

    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return err
    }
    stderr, err := cmd.StderrPipe()
    if err != nil {
        return err
    }

    err = cmd.Start()
    if err != nil {
        return err
    }

    // Stream logs:
    // https://stackoverflow.com/a/48849811/3405291
    scannerOut := bufio.NewScanner(stdout)
    scannerErr := bufio.NewScanner(stderr)
    scannerOut.Split(bufio.ScanRunes)
    scannerErr.Split(bufio.ScanRunes)
    for scannerOut.Scan() {
        _, err = fLog.WriteString(scannerOut.Text())
        if err != nil {
            return err
        }
    }
    for scannerErr.Scan() {
        _, err = fLog.WriteString(scannerErr.Text())
        if err != nil {
            return err
        }
    }
    if scannerOut.Err() != nil {
        return err
    }
    if scannerErr.Err() != nil {
        return err
    }

    err = cmd.Wait()
    return err
}
</code>

Je me demande s'il y a une sorte de bug dans le code Go ci-dessus ou s'il n'est pas adapté pour exécuter un exécutable ?

Goroutine unique

Suite à la suggestion de @BurakSerdar, j'ai lu stdout et stderr dans des goroutines séparées, mais le problème n'a pas été résolu :

<code>
// Run an executable and print its log into a file.
func RunWithLogFile(pthExe string, arg []string, fLog *os.File) error {
    cmd := exec.Command(pthExe, arg...)

    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return err
    }
    stderr, err := cmd.StderrPipe()
    if err != nil {
        return err
    }

    var wg sync.WaitGroup
    wg.Add(2)

    go streamToLogFile(stdout, fLog, &wg)
    go streamToLogFile(stderr, fLog, &wg)

    err = cmd.Start()
    if err != nil {
        return err
    }

    wg.Wait()

    err = cmd.Wait()
    return err
}

func streamToLogFile(output io.ReadCloser, fLog *os.File, wg *sync.WaitGroup) {
    defer wg.Done()
    scanner := bufio.NewScanner(output)
    scanner.Split(bufio.ScanRunes)
    for scanner.Scan() {
        _, err := fLog.WriteString(scanner.Text())
        if err != nil {
            log.Printf("error: write to log file: %s", err.Error())
        }
    }
    err := scanner.Err()
    if err != nil {
        log.Printf("error: write to log file: %s", err.Error())
    }
}
</code>

Réponse correcte


Résolu le problème de non-réponse en exécutant l'exécutable avec administrateurpermissions. Je fais ça :

A C# code runs a Go code and the Go code runs a C Code, i.e. external executable.

L'exécutable C appelle certains appels OpenGL GLUT. Peut-être qu'ils ont besoin de droits d'administrateur.

Démarrer le code Go via C# résout ce problème comme suit : https://www.php.cn/link/ac90e5f00f7542d99231f63fb0dfeecf

<code>        public static void RunLogic(string exePath, string args, PostProcess pp)
        {
            cmd = new Process();

            try
            {
                cmd.StartInfo.FileName = exePath;
                cmd.StartInfo.Arguments = args;
                cmd.StartInfo.UseShellExecute = true;
                cmd.StartInfo.CreateNoWindow = false;
                cmd.StartInfo.RedirectStandardOutput = false;
                cmd.StartInfo.RedirectStandardError = false;
                cmd.StartInfo.RedirectStandardInput = false;
                // Vista or higher check.
                // https://www.php.cn/link/ac90e5f00f7542d99231f63fb0dfeecf
                if (System.Environment.OSVersion.Version.Major >= 6)
                {
                    // Run with admin privileges to avoid a non-responsive executable.
                    cmd.StartInfo.Verb = "runas";
                }
                cmd.EnableRaisingEvents = true;
                cmd.Exited += new EventHandler(cmd_Exited);
                cmd.Exited += new EventHandler(pp);

                cmd.Start();
            }

            catch (Exception ex)
            {
                RhinoApp.WriteLine("Error on process start: {0}", ex.Message);
            }
        }
</code>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer