Why Process doesn't work when it should?

I created a command that starts a php server… which worked only I added cmd, I don’t why it didn’t work with cd dispatch && ... the rest

var options = {
		args: ["php", "-S", "localhost:9000"],
		cwd: dir,
		shell: true
	  };
	  console.log(options.args.join(" "));
	  
	phpServerProcess = new Process("/usr/bin/env", options);
	phpServerProcess.start();
	
	phpServerProcess.onStdout(function (data) {
		console.log(data);
	});

But now the problem is that the stdout is empty. How should I configure it to work? For other tools it works.

Second I needed a command to stop the php server with the following options

["kill" ,"$(ps" ,"aux", "|", "grep" ,"'localhost:9000'", "|", "awk", "'{print $2}')"]

which is successfully convert to

kill $(ps aux | grep 'localhost:9000' | awk '{print $2}')

If I run the command by hand in terminal it successfully stops the php server but when I run the command inside nova… it doesn’t. I’m sure that it reaches and runs the code because I print the process args

console.log(options.args.join(" "));

But there is not stdout, errors… nothing.

In the end I stopped the process with:

phpServerProcess.kill();

But still I’m curious why the other implementations didn’t work? I still can’t see the stdout…

I also tried with this

stdio: ["pipe", "pipe", "pipe"],

OK, there is quite a lot to unpack here, so I will be trying to tackle this bit by bit.

Terminating your process

As you have discovered, there is not need for a complex shell statement to terminate a process launched via a Nova script: Process.terminate() will do that because, unlike in the shell, that process is already under your direct control. Note: use terminate(), which sends SIGTERM, not kill(), which sends SIGKILL (see here for a good overview why).

Process launches processes, not statements

The reason why neither

cd dispatch && …

nor

kill $(ps aux | grep 'localhost:9000' | awk '{print $2}')

work is that these are shell statements: complex instructions sets that need to be interpreted by a program, in this case, a shell (the former is a boolean combined statement, the latter a pipe). Nova’s Process API launches a process, meaning one single program. If you want to execute interpreted code, that program needs to be the interpreter, and you need to pass the interpreted code to it as an argument. This

let options = {
  args: ["-c", "\"kill $(ps aux | grep 'localhost:9000' | awk '{print $2}')\ lines \""],
  shell: true
}
killProcess = new Process("/bin/sh", options)
killProcess.start()

would be the way to go – but, as noted above, it is unnecessary for this specific purpose.

stdout is empty

Try setting up your stdout handler before starting the process.

2 Likes

I moved the stdout handler before the start of the process but the result is the same.

var options = {
	args: ["php", "-S", host + ":" + port],
	cwd: dir,
	shell: true,
	stdio: ["pipe", "pipe", "pipe"],
};

phpServerProcess = new Process("/usr/bin/env", options);
phpServerProcess.onStdout(function (data) {
	console.log(data);
});
phpServerProcess.start();

… then I’d say your process doesn’t output anything to stdout. If that is not expected behaviour (some processes don’t when they notice they are not running in an interactive shell), you will have to find out what exactly went wrong: is it running (check Activity Monitor or ps output)? Does it perform according to expectations, e.g. react to input? Is there output on stderr? I’m sorry to say I can’t help with that.

1 Like

For anyone looking at the same problem. The @kopischke suggestion turned out to be the solution. Normally the php server will show messages of the opening file like:

When everything is okay

[Sat May 22 19:29:16 2021] 127.0.0.1:50344 [200]: /file.php

or when there is a mistake in the script

[Sat May 22 19:29:09 2021] PHP Parse error:  syntax error, unexpected ';' in file.php on line 232

Regardless of the type of the message successfull/error only onStderr is being called. Very confusing.

1 Like