Problem with system("command")

Sorry, I don't speak English very well but I will try to explain my problem!
If I write a C++ program and I use che system call system("command") I have this problem:

If I would launch a program I do this:

system("~/Agostino/program/test")

and all work fine!!

But if I divide this command in two part like these:

system(cd ~/Agostino/program/);
system("test");

no work fine because "test" is not found. "test" is searched in the default home path. The second call to system is executed like if it was invoked in another terminal session!

I showed this trivial example but, generally, I would that the second call to system start when the first terminated and as a result.

I hope I've explained well my problem! Thank you all for your cooperation!

The system() function runs the command in a separate process. When you run a second call, it gets its own process and anything (environment, working directory etc.) set in the first process isn't available.

Try putting the two commands together

system( "cd ~/Agostino/program; test" );

This will change directories and then run the test command; all in the same process.

Hope this helps.

---------- Post updated at 21:04 ---------- Previous update was at 20:58 ----------

It occurs to me that the system() function usually executes /bin/sh, and depending on your environment the shell installed as /bin/sh might not support the [/code]~[/icode] as meaning "home directory." Best to pass that information into your programme, or have the programme use get the value of HOME from the environment and use it.

Thanks a lot!!
I will test it when arrive at home!!
And IfI want to spend some time between the first and the second command?? I would be sure that the first command is completed before the beginning of the second.

Yes, the syntax command1; command2 'blocks' until the first command has completed. You could try this from the command line and see the behaviour:

date; sleep 4; date

It work fine!! But I have other problem!!

This is my code:

#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, char* argv[]) {
	system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; ssh AgostinoPolizzano@192.168.1.100; cmd");
	cout<<"end";
	return 0;
}

If I execute it, this is the output:


Last login: Tue Oct 12 03:49:13 on ttys000
MacBook-Pro-di-Agostino-Polizzano:~ AgostinoPolizzano$ ./test
file.pdf                                      100%  331KB 331.3KB/s   00:00    
Last login: Tue Oct 12 03:49:39 2010 from 192.168.1.103

AgostinoPolizzano@SERVER ~
$ 


The third command does not run and the program stops at the end of the second command!! Why?? It's a SSH question??
Thanks a lot!!

What exactly is "cmd" supposed to do? It's not any UNIX command I've ever heard of.

If you're trying to run it on the remote server there shouldn't be a ; between it and the ssh command, since it will try to run it locally once ssh has finished, not inside ssh!

cmd is Window's Command Prompt!! The ssh connection is between a Windows Server (OpenSSH is emulated by CygWin) and MacOs or Linux (is the same!!!!).

This command

system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; ssh AgostinoPolizzano@192.168.1.100 cmd");

work fine!!!

Missing the last command that interests me and we have finished!!! It launch a print job on the server:

start C:\"Program Files"\Adobe\"Reader 9.0"\Reader\AcroRd32.exe /p /h file.pdf

How can I launch the last command after "cmd"???

Thanks very very much!!

It looks to me like you are being logged into the remote host and it is leaving you at the prompt. If you type exit at the prompt does 'control' then return to your programme, or if you type hostname at the prompt is the output the hostname of 192.168.1.100 rather than the host that is running your programme?

I agree with Corona688, if your intent is to execute cmd on the remote host, then it needs to go before the semicolon. Depending on what your C++ programme is doing, especially with regard to standard input, you need to be careful with using ssh in this manner. If you are executing a remote command, not wanting to log in and have a prompt, then you might consider using the -n option on the ssh command to prevent it from reading standard input. You might also have issues if ssh finds the need to prompt for a password.

---------- Post updated at 22:38 ---------- Previous update was at 22:36 ----------

Looks like we crossed posts -- scratch what I said as you've explained.

Unfortunately, my windows experience is next to zero. From what I remember about cmd (from DOS 3.x days) I don't think you can give it a command to execute like a unix shell (ksh -c "command string"). Maybe someone else will chime in; sorry.

how about you try it without the "cmd"? logging in with ssh may mean you already have one...

Thanks to Agama, to!!

"cmd" command is necessary to launch one application in windows!! If I don't use "cmd" I can't launch any application, but only work with files!! I don't know why!!!

I haven't problem with password because I use the authentication with asymmetric key!!

How I can use the "-n" option??? For example, if I would launch the command "dir" (like "ls" in Unix) after "cmd" how can I do??

This
system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; ssh -n AgostinoPolizzano@192.168.1.100 cmd dir");

don't work!!

Any suggestion, please??

Thanks very very much!!

---------- Post updated at 06:00 AM ---------- Previous update was at 05:02 AM ----------

I have solved in this way:

system("scp file.pdf AgostinoPolizzano@192.168.1.100:./; echo 'start C:\\Programmi\\Adobe\\Reader\\Reader\\AcroRd32.exe /p /h file.pdf' | ssh -t AgostinoPolizzano@192.168.1.100 cmd");

now all work fine!!!

Thanks a lot for your suggestions!!

1 Like