I have an issue with a program written in C++
. I want to open a SOCKS5 proxy on a free port then check if is ok (check with curl), then release the I/O blocking.
This is the code :
C++
main()
{
char* s_sockshost = "127.0.0.1";
socks_port = find_empty_port();
if(fork())
{
// child process continues and opens a socks
open_proxy();
}
else
{
// parrent process just checks something then dies
for(int i = 0; i < 20; i++)
{
proxytest = curlsockstest(s_sockshost,socks_port);
if(proxytest)
{
break;
}
sleep(1);
}
if(proxytest)
{
if(hitdebug >= 3) printf("check_result : is opened on %s",socks_port);
exit(0); // kill just this process
}
else
{
if(hitdebug >= 3) printf("check_result : is bad\n");
kill(getppid(), SIGKILL); // kill both processes
}
}
}
If i do this from cmd like
./proxy; ls -al;
then it is executing and executes the command after it, but if i do it from PHP or NODEJS it is hanging, like expecting to finish.
NODEJS:
var exec = require('child_process').exec;
var cmd = './proxy; ls -al;';
setTimeout(function(){
console.log("Timer");
exec(cmd, function(error, stdout, stderr) {
console.log("error: ");
console.log(error);
console.log();
console.log("stdout: ");
console.log(stdout);
console.log();
console.log("stderr: ");
console.log(stderr);
console.log();
});
console.log("Timer end");
},2000);
PHP:
<?php
echo "Run start\n";
$array_exec = array();
// exec("./proxy",$array_exec);
system("./proxy");
var_dump($array_exec);
echo "Run end\n";
?>
What is the explination and how can i solve this?
I am thinking to make PHP and NODEJS comunicating with this C++ app with sqlite
or something like that...
The system()
function would wait until the process is finished and collect the entire output.
You should either use daemon for your C++ or you can simply change it to: ./proxy &
and if you don't need output, ./proxy & > /dev/null
<?php
echo "Run start\n";
$array_exec = array();
shell_exec("./proxy &",$array_exec);
var_dump($array_exec);
echo "Run end\n";
?>
Node.js
const { spawn } = require('child_process');
const ls = spawn('proxy');//@see https://nodejs.org/api/child_process.html#child_process_class_childprocess
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}. We should stop the child_process`);
ls.kill('SIGTSTP');
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Update
instead of kill can it be something as deatach() instead of
ls.kill('SIGTERM');
? like send it in background? i will try also your variant, it will help me also
This is not part of Node.js staff. There is a part of the Unix signals list:
SIGINT
means interrupt. By default, this signal causes the process to terminate. In terminal you can press Ctrl-C (in older Unixes, DEL) for sending SIGINT;SIGTSTP
means terminal stop. By default, this signal causes the process to suspend execution. In terminal you can press Ctrl-Z for sending SIGTSTP;SIGQUIT
means quit. This signal causes the process to terminate and dump core. In terminal you can press Ctrl-| for sending SIGTSTP.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With