C/C++ - popen - launch process in specific directory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What I want to do is launch cmd in a specific directory. Basically, I want to change to the directory and launch the command. The "raw shell" equivalent (*nix or Windows) being:
Code:
cd "SomeDir"
cmd
Is there any way to do this with the popen or exe/execl commands? I couldn't find anything in the man pages or online about it.
Or you could have the parent process cwd, then launch via popen/exec*.
I wasn't sure if changing directories on the parent process would do it or not. After a quick test, you are right... it does. I will use this solution for run.
popen executes the shell in a subprocess -- concatenate two commands into one
Suppose your command looks like this now:
Code:
ls -l somefile
Make the command string look like this:
Code:
cd /path/to/somewhere && ls -l somefile
hello..
i tried using && and it works ex:cd /tmp && ls -l.. thank you..
but i was trying this.. chroot /var/chroot && ls -l.. here the ls -l works only after i give an 'exit'
any suggestions, plz..
thanks..
hello..
i tried using && and it works ex:cd /tmp && ls -l.. thank you..
but i was trying this.. chroot /var/chroot && ls -l.. here the ls -l works only after i give an 'exit'
any suggestions, plz..
thanks..
That's because chroot will give you a sub-shell by default when you don't specify a command to execute. In this case, it's like typing bash && ls.
Kevin Barry
i have another question.. i used popen in my c file to chroot and do some actions..
the executable generated from my c file when run from terminal works fine..
but when i call the executable from a php file (which is a part of my Wordpress blog plugin run on Apache server) the chroot does not work and the popen returns 32512 which i think is a failure to fork a subshell for chroot..
any suggestions plz..
thanks, bala
Distribution: RedHat, CentOS, Mandriva, and Ubuntu
Posts: 13
Rep:
revof11 I found your code quite handy...
Code:
string getStdoutFromCommand(string cmd) {
string data;
FILE *stream;
int MAX_BUFFER = 256;
char buffer[MAX_BUFFER];
cmd.append(" 2>&1");
stream = popen(cmd.c_str(), "r");
if (!stream){
exit(1);
}
while (!feof(stream)){
if (fgets(buffer, MAX_BUFFER, stream) != NULL){
data.append(buffer);
}
}
pclose(stream);
return data;
}
One question I have though is if a command is issued that does not return or takes too long is there a way to kill the command and break from the function.
What I am doing with the code above:
I have written a client server application that allows me to exec a command on many servers at once and get the output from that command at the client machine as follows:
Code:
[root@mobile-david ~]# for HOST in `echo "localhost thor"`
> do
> rcomm $HOST hostname -s
> done
localhost: mobile-david
thor: thor
which works amazingly well until I have a jr admin that does something like grepping our entire NFS tree for a string.
I need a way to kill that process from C++ code on say a timer event.
The timer portion is no problem, but killing that process has done little more than leaving it <defunct> until I restart the server, and if it gets called again and again... many <defunct> processes.
I need a way to kill that process from C++ code on say a timer event.
The timer portion is no problem, but killing that process has done little more than leaving it <defunct> until I restart the server, and if it gets called again and again... many <defunct> processes.
You need to wait() for your defunct child processes. Call waitpid() with the PID of the process you just killed.
Distribution: RedHat, CentOS, Mandriva, and Ubuntu
Posts: 13
Rep:
I'm sorry, I did not mention that I have been using waitpid(); The problem was resolved once I removed that popen() from a function outside main() and placed it directly in the main function.
Seems that for some reason if I have it external to main and call the function it is in from main I cannot escape from it properly. Is probably just my poor code though.
I'm not an advanced developer, merely a tinkering tester
Oh -- with popen(), you don't need to kill or wait for the subprocess. pclose() does that for you. Just pclose() the stream and you should be OK. Closing the stream sends a SIGPIPE to the subprocess which should cause it to exit. The process will not get the SIGPIPE until it actually tries to write to the pipe which, for long running processes, may be a while. If you really do need to kill the subprocess, make sure you still call pclose().
You probably want to change the reader while() loop in your code to something like this:
Code:
while (fgets(buffer, MAX_BUFFER, stream) != NULL) {
data.append(buffer);
}
if (ferror(stream)) {
// Handle error.
}
pclose(stream);
I'm not sure that feof() is true when fgets() returns due to an error, such as the subprocess being killed.
You should consider using exceptions in your code. Don't exit() from C++ applications. It will prevent some destructors from being called. Well-formed C++ applications always return from main().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.