1

I have spent the past few days trying to develop a script that automatically executes a series of commands. However, I have been having a problem with trying to input a command into a running subprocess previously executed with Popen().

The program starts a process in a terminal window ppd terminal which serves as the terminal for a cryptocurrency mining program. My issue is that after reading the first two lines to search for an error successfully I need to input the command startmining into the ppd program that is running ('ppd terminal').

However all the options I have tried either wait for the command ('ppd terminal') to finish, which never does unless there is an error, or is unable to input another command into the program once it is running. I have tried to write this script in C and Python making use of .popen(), .run(), system(), and other alternatives.

This is the closest I think I have got in both C and Python3 on this specific issue.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>

#define MAX_LINE 350

//run the program inside rsnode directory
int main(){

    FILE *output;
    
    char buffer[MAX_LINE];
    int buffer_search;
    int read_line = 2;
    char error_message[] = "[ERROR]";
    
    
    output = popen("ppd terminal","r");
    sleep(5);
  

    bool keep_reading = true; 
    int current_line = 1;
    do{
        fgets(buffer, MAX_LINE, output);
        buffer_search = strncmp(error_message, buffer, 7);

        if (current_line > 2){
                keep_reading = false;
        }

        else if (buffer_search == 0){
        system("gnome-terminal -- ./smt1"); //this comand is to restart this same program on a new terminal
        system("ppd start");
        sleep(5);
                
                while(1);
        }
        current_line++;
    

    }while (keep_reading == true);

    
    sleep(30);

    system("ppd terminal && startmining");
    sleep (300);
    
    FILE *output2;
    char buffer2[MAX_LINE];
    char error_message2[] = "dial unix /home";
    int buffer_search2;
    int timer = 1;
    bool no_error = true;

    do{
        output2 = popen("startmining","r");
        while(timer < 300){
        fgets(buffer2, MAX_LINE, output2);
        buffer_search2 = strncmp(error_message2, buffer2, 15);
            if (buffer_search2 == 0){
                system("exit");
                system ("gnome-terminal -- ./smt1"); 
                system ("exit");
                no_error = false;
        pclose(output2);
            }
        sleep (1);
        timer++;
        }
    }while(no_error == true);

    

    return 0;
    }

and Python:

import os
import subprocess
import time
import signal

ppd_terminal = subprocess.Popen('ppd terminal', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, stdin=subprocess.PIPE)
out, err = ppd_terminal.communicate('startmining')

time.sleep(10)

cmd = subprocess.Popen('startmining', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, stdin=subprocess.PIPE).comunicate("startmining")
out, err = cmd.communicate(input='startmining')
p.stdin.write("startmining")
time.sleep(10)


print(ppd_terminal.stdout)
print(cmd.stdout)

I have to add this is the first time I develop a program in C and I have very basic knowledge of Python, so please forgive my poor syntax and answer in a way that is easy to understand if possible.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • If you need to write to the program as well as read from it, then using the POSIX function `popen()` is not the answer. With that, you can either read the output of the program it runs or write to the program's input; you can't do both with the same process. You can write a function analogous to `popen()` that provides both an input stream and an output stream to the program. But there isn't such a function in the POSIX library (let alone the standard C library). – Jonathan Leffler Mar 18 '22 at 14:58
  • You can find an approximation to what you need in my answer to [Trying to use pipe to read from/write to another program?](https://stackoverflow.com/q/44087031/15168). You can find my code for that in my [SOQ](https://github.com/jleffler/soq) (Stack Overflow Questions) repository on GitHub as files `rwpipe17.` and `rwpipe53.c` in the [src/so-4408-7031](https://github.com/jleffler/soq/tree/master/src/so-4408-7031) sub-directory. – Jonathan Leffler Mar 18 '22 at 16:19
  • And do you know of any alternative @JonathanLeffler that might work for me in Python, I heard that they offer a greater range of libraries for subprocesses and pipes but I am new into this and I don't know where to start my research. – Alvaro Ortiz de Lanzagorta Mar 19 '22 at 05:02
  • Start with the Python `subprocess` module. After that, I'm not sure. I can read Python just about, but my coding skills in Python are anæmic at best. – Jonathan Leffler Mar 19 '22 at 05:36
  • @JonathanLeffler I just went trough your post and it seems that your code `rwpipe53.c` is the answer to my problem. However I am quite inexperienced on programming in general and I am having a hard time understanding the code. Could you please elaborate on how I could implement your idea in my script? – Alvaro Ortiz de Lanzagorta Mar 19 '22 at 06:56

0 Answers0