0

I have apache and Node.js running on Ubuntu

Is there a way to programatically check if the apache service is running with Node? If it's not running, allow Node.js to start/restart the service, also could the same code be used to run other processes like MySQL?

I'd also like to find a way to make Node execute "ps -u user" and capture the output in a js string or object

I've had a look at Node.js child processes but I can't figure it out

Thanks in advance

3 Answers 3

1

You can use the "exec" method of "child_process" module to execute a command from your node app and have its output passed as a parameter to a callback function.

var exec = require("child_process").exec;

exec("ps -u user", function (error, stdout, stderr) {
    var myResult = stdout;
    //doSomething
});
Sign up to request clarification or add additional context in comments.

Comments

1

You could also use shelljs - https://www.npmjs.com/package/shelljs, which has many functions goes directly to shell and is cross-platform. ps command is not one of them, so use exec similar as with child_process.

require('shelljs/global');    
exec('ps -aux | grep apache')

With exec you could start/restart service too, however you will need to run node process as root, what's not best option. Or create usergroup specifically for apache.

To run 'ps -u user' use the same. It will return you object with all info you need about processes.

exec('ps -u user');

And than continue with output as you need.

Comments

0

with spawn you can run shell scripts. So if you had a shell script written to check if a processes is running...something like this you should us spawn to run it. something like... var child = spawn('sh', [__dirname+'/some-status-shell-script.sh'], {env: process.env});

Likewise you can use spawn to access your mysql db...similiar to this

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.