I'm having a problem with my script. Currently, a PHP script turns on a node script and outputs the process ID like this:
<?php
exec("/home/node /home/app/server.js 1>/dev/null & echo $!",$pid);
if($pid) echo $pid;
?>
The script launches but it does not log anything.
Node.js script:
const winston = require('winston');
const {combine,timestamp,printf,colorize} = winston.format;
function getLogger(name,filename)
{
let self = this;
let o = {
transports:[
new winston.transports.Console({
levels: winston.config.syslog.levels,
colorize: true,
label: name,
format:winston.format.combine(
colorize(),
timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
printf(info => {
return `${info.timestamp} [${info.level}] : ${info.message}`;
})
)
})
]
};
filename = filename || "";
if(filename) o.transports.push(new winston.transports.File({
filename:"./logs/"+filename,
format:winston.format.combine(
timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
printf(info => {
return JSON.stringify({timestamp:info.timestamp,level: info.level,msg:info.message});
})
)
}));
winston.loggers.add(name, o);
return winston.loggers.get(name);
}
var log = getLogger("SERVER","server.log");
log.info("Hello world");
At first I thought that output transporting to /dev/null is the problem, however, launching the exact same script through shell it works and logs fine. However, launched from PHP it does not.
What could be the cause of this?
EDIT:
This is how I call the PHP script:
$.ajax({
url:'startnode.php',
method:"POST",
success:function(data){
console.log(data);
}
})
EDIT 2:
Could this be because of www-data user permissions? When I launch the node.js script from php, ps aux
command says the node.js script is running from www-data user.
EDIT 3:
I changed to www-data user and tried running the script from the shell:
www-data@vps1130207:~$ /home/node /home/app/server.js
2018-11-13 19:43:05 [info] : Hello world
events.js:167
throw er; // Unhandled 'error' event
^
Error: EACCES: permission denied, stat 'logs/server.log'
Emitted 'error' event at:
at DerivedLogger.transportError (/home/app/node_modules/winston/lib/winston/logger.js:529:12)
at File.emit (events.js:187:15)
at stat (/home/app/node_modules/winston/lib/winston/transports/file.js:342:21)
at fs.stat (/home/app/node_modules/winston/lib/winston/transports/file.js:371:16)
at FSReqWrap.oncomplete (fs.js:152:21)