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.