Reputation: 1734
I was trying to run netcat by using ssh and seems that my code always fails in my C code. Here are the commands which I am executing using system() in this order.
system("ssh machine 'nc -l 61001|dd bs=64M of=temp' &")
system("/bin/dd if=filename bs=64M|nc IP_address 61001")
I noticed that the first command works correctly as the temp file is created on the remote machine. The second dd command fails and states that 0 bytes has been written on the remote machine. These commands work correctly when executed from the terminal, but fail as soon as I put it in system() calls in C.
Upvotes: 1
Views: 1058
Reputation:
a & disown
(instead of just &) should do.
system()
spawns a shell that just executes one command and then exits. The &
tells the shell to fork the command into background (means, it doesn't wait for its completion), but it's still part of the session and process group of the shell. When the group leader (the shell) exits, all children are killed. The disown
causes the shell to start new process group, the child process is now owned by init
(the first process in the system).
This is about programming. You are forking processes like crazy to accomplish something a C program could easily do using builtin library calls (except for the ssh
but there are better ways, too). Go read on BSD sockets.
Upvotes: 2