Pass Terminal ouput in GUI - python-2.7

I have created a GUI that runs a command in Linux Terminal.
EXAMPLE=> -n 20 -id 15 -domain square(10,20) -dim 2 -o execution -format geo,tess
This command is for the execution of a code for package. And when it executes a series of outputs undergo in the Linux terminal. The output is either successful and the output is generated or an error occurs due to a dump error or bad argument.
Examples of Terminal Outputs:
Error : matheval failed to process the expression. The expression
syntax is probably incorrect. This may also be caused by an unproper
matheval installation (expression = 1 -ori fibre(,,)). Aborted (core
dumped)
Another Error
Error : Bad arguments!
Aborted (core dumped)
What i am trying to do is return this error back into the GUI as either a MessageBox and/or update the bottom Status bar of the GUI with the error.
I am not familiar with the wx and subprocess modules, but my research so far has failed to find a solution.
I am running Python 2.7 and using Tkinter.

This seems to get the work done.
# Create a string and pass variables
neper_exec = "neper -T -n %s -id %s -format %s -o %s -dim %s -domain %s " % (n, id, output_form, o, dim, domain)
# Execute the subporcess using the variable
p = subprocess.Popen(neper_exec, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # when shell is dropped Linux gives and error
Thanks to #BrianMcFarland for guidance on the subprocess command.

Related

GDB Error appears while debugging OMNET++ simulation

I am working on OMNET++ project called Exfogsim and there is no problem when I run the simulation with (Run), but when I am trying to Debug it the following error appears:
with the following error
Error with command: gdb --version
Cannot run program "gdb": Launching failed
Your simulation was terminated with exit code: -2147483645. The hex representation of this number is: 0x80000003. So try search how to deal with the Windows exception 0x80000003, for example take a look at that one.

Checking video integrity using ffmpeg with Python

I am trying to check video integrity and made it successfully using mac terminal with the line of code below:
ffmpeg -v error -i filename.mp4 -f null - 2>error.log
Now i am trying to do the same in python with no success making the cmd command. I try to do:
cmds = ["ffmpeg", "-i","filename.mp4", "-v", "error", "-f", "null", "-", "2>", "error.log"]
subprocess.Popen(cmds)
But i am getting the error - 2>: Invalid argument
I also tried also "2>error.log" instead of "2>", "error.log" but then im getting 2>error.log: Invalid argument
I found a work around to fix the problem.I am running the command with python from the terminal using:
import os
os.system("ffmpeg -v error -i filename.mp4 -f null - 2>error.log")
log_file = open("error.log","r")
print log_file.read()

No stdout when attempting to run a python file with subprocess.run()

I'm trying to run a python file from within my python script. I'm doubtful that the file is even getting run in the first place and it is not showing anything in the stdout either for me to debug.
I have tried the command 'ls' in subprocess and it worked, and was in the proper directory that the temp.py file i am trying to run is.
When i have tried to set the argument 'shell=True' it takes me into the python repl for some reason i am not sure why.
Here is the string output:
Terminal output: CompletedProcess(args=['python3', 'temp.py'], returncode=0, stdout=b'')
And here's the code used to produce it:
result = subprocess.run(['python3', 'temp.py'], stdout=subprocess.PIPE, check=True)
print('Terminal output: '+str(result))
EDIT
I also just tried
process = Popen(['python3', 'temp.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print('Terminal output: '+str(stdout)+str(stderr))
No cigar:
Terminal output: b''b''
So, I found an alternative that worked. I dumped all the contents of the file into the python3 command with the '-c' augment.
process = Popen(['python3','-u', '-c', strCode], stdout=PIPE, stderr=PIPE)
strCode being the contents of the file.

core dumped message is not captured in STDERR

I used to call my program with this line in bash shell to capture all stdout and stderr messages in the log file
./a.out input.txt 2>&1 | tee log
The log file shows no error but by examining the log, it's obvious that there's a problem and program terminates abruptly in the middle of execution.
I have also tried these but the result is the same:
./a.out input.txt > log 2>&1
./a.out input.txt |& tee log
When I run it without any redirection like this:
./a.out input.txt
I see the error message in my terminal window at the end:
*** Error in `./a.out': free(): invalid pointer: 0x000000000169b268 ***
Aborted (core dumped)
So, why I cannot capture the "core dumped" message in my log? What should I do to do so?
There are 2 error messages here:
*** Error in `./a.out': free(): invalid pointer: 0x000000000169b268 ***
This comes from glibc, and is printed on the current tty, if it exists.
If you want it printed to stderr (wherever stderr is redirected), you must set
the LIBC_FATAL_STDERR_ prior to starting the program.
e.g. in bash do:
export LIBC_FATAL_STDERR_=1
The other message
Aborted (core dumped)
This comes from the shell that started your program, by the shell examining the status of wait().
If the program isn't run by a shell, or e.g. is run by a shell that have terminated, you'll not be able to capture that message. Even if the shell havn't terminated, the stderr of that shell isn't redirected to your log file.
You might get around that by doing:
{ ./a.out input.txt ; } >log 2>&1
See also redirection of ./a.out is not capturing segmentation fault)

Redirect execution errors to file c++

How can I redirect execution errors of a c++ executable in bash? I've found that 2> helps while trying identify compilation errors:
g++ example.cpp 2> compErr.txt
But running the executable with that command still sends the errors to stdout:
$ ./a.out 2> e.txt
Floating point exception (core dumped)
Actually, the error "Floating point exception (core dumped)" does not come from the executable but from the shell! The messages from bash won't be suppressed by output redirection but there is a flag to enable/disable these messages.
You can install signal handlers for some of the errors which would cause the program to exit and write something to a suitable destination there. Some signals can't be intercepted and some other are hard to handle. That's the approach you can do from inside your code.
If you want to go further you could fork() your program first thing and have the actual work done in the child process. The parent process would essentially just waitpid() for the child process and use the information in the result structure received to report errors to a file.
I found something that worked in my terminal, here: http://bytes.com/topic/c/answers/822874-runtime-error-stderr-gcc-ubuntu-bash
In summary, a participant explained:
In this particular case, the reason that the string "Floating point exception" is not >redirected is that it is not produced by the process that runs ./{file} or anything that it invokes. Instead,it is being produced by the command-interpreter itself.
You can see this by telling the command interpreter to run another command interpreter, redirecting this sub-interpreter's error output. However, a bit of a >trick is required:
$ bash -c './{file}; true' >out 2>err
$ cat out
$ cat err
bash: line 1: 28106 Floating point exception./test_fail