QProcess Wrong Behavior - c++

My application runs different bash files when i run my application under QTCreator everything works fine but when i run my application directly i cant read the QProcess output . even when i run my application via Terminal it works fine , so where is the problem ?
i'm using QT 5.7 / OSX Platform
here is my code
QProcess proc ;
proc.start(QCoreApplication::applicationDirPath() + "/check.sh");
proc.waitForFinished();
QString output = QString(proc.readAll());
qDebug() << output ;

There are some possibilities you should investigate
Can you confirm that scripts are running though when you run it standalone?
QProcess always was a little skittish about creating processes when supplied scripts, depending on platform. Does script have shebang in it? Does it match the shell you're running your program from? You may need to create process based on shell, supplying script's file name as a parameter.
QProcess::readAll() May return nothing if output buffer wasn't flushed by the process. Outputting EOL at end would force the flush.

Related

Xcode 8.3: Run target with input from file

I wrote a simple C++ program that requires some input to run. In the terminal I simply run in ./myProgram < fileWithData.txt. However I could not figure out how to specify and input file for the target executed in Xcode. I used the command line project. Of course I could use a different target, for example run Terminal.app and then pass it the executable with the input file but then I can no longer debug it.
This question: Cannot get lldb to read file input explains how to set the input path in lldb, but I could not find a way to specify lldb commands that are executed before the process is started.
I don't think there's a way to do this entirely from within Xcode. However if you set the Run Scheme in Xcode to the launch mode "Wait for executable to be launched," hit run, and then run your program from Terminal.app with the appropriate piping, the Xcode-embedded lldb will connect to it.

C++ executable, sh 1:not found

I created a c++ programm that works with ros. The first step would be to open a roscore in a terminal and move on from there. I do so with system("roscore &");
I compiled my file and can run it just fine with ./file.
However, I want to be able to run it as an application (double click). I created a .desktop file and the program shows up in my application list. When i start it though, all I get is a terminal that opens with the message
sh: 1: roscore: not found
etc.
The same applies for the roslaunch commands. I also fork and exec a roslaunch command, which does not work as well.
I tried system("ls"); which worked. All cout messages work as well.
Any idea what is wrong here?
roscore executable is not located in std paths (/bin:/usr/bin:). Use the absolute path - system("/path/to/roscore &")

Strange CMD errors only when CMD is opened from my program

This is a weird one for sure.
If I open a command prompt window directly (searching cmd in start, right click > open command window here, cmd within bat file, etc....) all commands entered run perfectly fine.
If I open a command prompt window from within my C++ application (system("cmd"); or QProcess::startDetached("cmd"); etc....) the commands I enter throw errors.
Here are a few commands that don't work in the cmd opened from my app:
vssadmin delete shadows /all
vssadmin list shadows
wmic
shadowcopy
and so on... I get Class not registered and Initialization failure errors all around. Anything to do with shadow copies isn't working at all. But again, the weird thing is, those same commands work perfectly fine when cmd was opened traditionally (not from a program). Both instances of cmd have admin privileges.
So my question is, how come the way I open cmd affects whether or not some commands work? Everything I can see says there should be no difference.
32-bit applications running on WOW64 will be put under file system redirection. Therefore if your app is a 32-bit one, the call system("c:\\windows\\system32\\cmd.exe"); will be redirected to C:\Windows\SysWOW64\cmd.exe and 32-bit cmd will always be invoked. You have some solutions:
Use system("c:\\windows\\sysnative\\cmd.exe"); to access the real system32 folder and get the 64-bit cmd
Turn off file system redirection explicitly (should be avoided in general)
Or better compiling it as a 64-bit app.

How do I open a terminal window with C++ in Ubuntu?

I recently decided to start teaching myself C++ and thought a simple encryption project would be a good place to start, since it covers most of the basics (cout, cin, opening files, etc). Is there a way to have the code open a terminal window similar to the one opened when I compile and run from sublime text?
I have tried this so far, but it hasn't changed anything.
string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system(cmd.c_str());
Essentially, I would like to be able to run the program by clicking the .exe, and have the terminal where all of the input and output goes pop up.
You don't need to write any code, you just need to configure the shortcut to launch the program in a terminal. Here's a Gnome dialog that shows that option:
Problem seems to be gnome-terminal, or then just my failure to give it the right arguments. For example gnome-terminal -x sh -c 'ls -l ; exec bash' from command line in another terminal just opens an empty gnome-terminal and spits out a bunch of glib warnings to original terminal... (Note to readers: if you can give the right command that works for gnome-terminal, please let me know in comments or just edit this paragraph.)
However, using xterm works, for example xterm -e sh -c 'ls -l; exec bash', or a line for your code:
string cmd = "xterm -e sh -c 'ls -l; exec bash'";
As a side note, the command to open the default x terminal window of the DE is x-terminal-emulator, but it quite often has the practical problem of different terminals taking different arguments, so sadly you're probably better of using a specific terminal, like that xterm, and requiring that to be installed, or letting user to configure what terminal to use, with what arguments (though letting user to specify any command to be run can also be a security risk, if user is not always trusted).
Just be very careful with escaping. For example, when you test the command form command line, and then copy-paste it to C++ string literal, you need to escape every " and \ one more time for C++. If you have trouble with this, check out C++11 raw strings.
Escaping becomes extra important if you construct the command string at runtime, and especially if you accept user input and add that to the string. In that case, better search for and use some existing library like GLib, or sanitize the user input very carefully (ie. just paranoidically reject anything with chars, which may have a special meaning in shell in some context).
If you are actually asking, how can my program open a console window for itself similar to how Windows console programs behave, and redirect it's own stdin, stdout and stderr there, as if it was launched from command line, that that is not very easy from the same binary, and it is not commonly done like that in Unix.
If you want a behaviour like that, you could create a desktop shortcut, but more general way is to write a wrapper shell script, which starts your binary in a terminal. What kind of script exactly, depends on how you want it to behave exactly: what will it do with stdio, will it return or wait for program to exit, how do you want it to find the binary, how does it behave when run from command line instead of double-clicking from GUI, etc.

C++ Keep process alive after it kills its parent

I'm working on implementing a self-updater for a daemon on OS X. The update is published as a .pkg file, so what I'm trying to do is as follows:
When the daemon is notified that an update is available, it calls installer via the system() call to install the package. The package contains a newer version of the daemon, a preupgrade script that stops the daemon (launchctl unload /Library/LaunchDaemons/foo.plist), and a postflight script that starts it back up after the new version is installed. The problem I'm having is that the installer process is quitting prematurely. I suspect that it may be because the installer kills its parent process in order to update it, and then gets killed itself instead of continuing as its own orphan process. I've tried the following with no luck:
Postpending the installer command with '&' to run it in the
background
Wrapping the installer command with nohup
The install command completes consistently without error when I run it from the command line, and fails consistently when run from the installer. When called from the installer, I'm piping the output to a file, and sometimes it has nothing, and sometimes it shows the install getting to about 41% completion before output stops. Any ideas on how I can figure out what's happening to the process or make sure it stays alive without its parent?
When you call launchctl unload, it kills the entire process group (unlike a simple kill). You want to move your subprocess into a separate process group. The easiest way is with the C call setsid().
If you're in the middle of a shell script, you should look at the following approaches. I haven't tried these since I was dealing with a C program and could setsid():
Prior to calling the installer, use set -m. This is supposed to turn on monitor mode, which says "Background processes run in a separate process group and a line containing their exit status is printed upon their completion."
Try this sub-interative shell trick: New process group in shell script