-1

I have a C program that uses the Linux terminal to run the a python program, but I want to make sure that the python program runs into no errors. If an error message is printed to the terminal after the python program is run, I would like the C program to know.

run.py:

g = input(">")

if (g == 0):
    print("error")
    exit(1)
else:
    print("good")
    exit(0)

checker.c:

#include <stdio.h>
#include <stdlib.h>


int main(void){
    char run[30];
    snprintf(run, 30, "sudo python2.7 run.py");
    // I need to make sure that the next statement is run without errors
    system(run);
Drakke37
  • 13
  • 2
  • This might have a similar solution as C checking for an error in the command "sudo mv nonexistant.x", but I'm not sure how to do that either – Drakke37 Aug 09 '21 at 17:46
  • 8
    Try [popen](https://stackoverflow.com/q/671461/10077). Or just check the return code of `system()`. Anything other than zero indicates an error. – Fred Larson Aug 09 '21 at 17:46
  • you just sent me a link to c++, I am using C. – Drakke37 Aug 09 '21 at 18:07
  • 1
    Using `popen` is an option, but you could write a C program to use in pipe. Piped programs receive the output of a program on stdin. So you could use: `pyprog | Cprog` from the command line, by doing this the C program will receive all the output of the python program on stdin. – Sir Jo Black Aug 09 '21 at 18:09
  • 2
    The code given there will work identically in C. – Fred Larson Aug 09 '21 at 18:10
  • 2
    What indicates an error message? Output on the stream `stderr`? Any output? A message that starts with `error`, `Error`, `ERROR` or the token `Failure` instead? Or just the return value of the child process? In the latter case, [man system](https://man7.org/linux/man-pages/man3/system.3.html) explains the return values of the call. – Erdal Küçük Aug 09 '21 at 18:36
  • `"sudo python2.7 run.py"` is 21 characters long. – William Pursell Aug 09 '21 at 19:31
  • Just fixed it William – Drakke37 Aug 10 '21 at 16:07
  • Erdal, I was going at this issue with both as possibilities for solutions, if I can either read the exit code or the output to the terminal, then I can determine whether or not there was an error, I really just need one to work. – Drakke37 Aug 10 '21 at 16:10

1 Answers1

0

-As I said in the comments, instead of using system or popen, you might use the system pipe command: |.

Using the pipe command means redirecting the output (stdout) of one program to the input (stdin) of another program.

The simple program (program_that_monitors), that I've insert below, rewrites on the console all the output generated by another program (program_to_monitor) piped (pipe is | ) by it.

from command line:

prog_to_monitor | program_that_monitors

With this trivial command, to have the stderr too, it will be necessary to redirect it to the stdout. All very simple:

from command line:

prog_to_monitor 2>&1 | program_that_monitors

where 2>&1 redirects stderr to stdout and | performs pipelining from program_to_monitor to program_that_monitors

Obviously, instead of the part that rewrites the output coming from program_to_monitor, you can insert your control logic.

Here is the very simple C code:

#include <stdio.h>
#include <string.h>
int main()
{
    char buff[1000];

    while( fgets(buff, 1000, stdin) ) {
        printf(">> ");
        fwrite(buff,1,strlen(buff),stdout);
    }

    return 0;
}
Sir Jo Black
  • 2,024
  • 2
  • 15
  • 22