Starting a node.js script through PHP disables logs, permission issue apparrently












0















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)









share|improve this question

























  • How do you execute this script from your .php file?

    – David R
    Nov 13 '18 at 15:55











  • I've updated the question (unless you asked about the php script iself which is at the top.)

    – Nedas
    Nov 13 '18 at 16:11













  • Can you try removing the method:"POST" line from your $.ajax(... statement?

    – David R
    Nov 13 '18 at 16:15











  • I did, same issue.

    – Nedas
    Nov 13 '18 at 16:31











  • here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

    – Fermin Perdomo
    Nov 13 '18 at 18:37


















0















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)









share|improve this question

























  • How do you execute this script from your .php file?

    – David R
    Nov 13 '18 at 15:55











  • I've updated the question (unless you asked about the php script iself which is at the top.)

    – Nedas
    Nov 13 '18 at 16:11













  • Can you try removing the method:"POST" line from your $.ajax(... statement?

    – David R
    Nov 13 '18 at 16:15











  • I did, same issue.

    – Nedas
    Nov 13 '18 at 16:31











  • here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

    – Fermin Perdomo
    Nov 13 '18 at 18:37
















0












0








0








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)









share|improve this question
















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)






php node.js winston






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:23







Nedas

















asked Nov 13 '18 at 15:36









NedasNedas

10312




10312













  • How do you execute this script from your .php file?

    – David R
    Nov 13 '18 at 15:55











  • I've updated the question (unless you asked about the php script iself which is at the top.)

    – Nedas
    Nov 13 '18 at 16:11













  • Can you try removing the method:"POST" line from your $.ajax(... statement?

    – David R
    Nov 13 '18 at 16:15











  • I did, same issue.

    – Nedas
    Nov 13 '18 at 16:31











  • here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

    – Fermin Perdomo
    Nov 13 '18 at 18:37





















  • How do you execute this script from your .php file?

    – David R
    Nov 13 '18 at 15:55











  • I've updated the question (unless you asked about the php script iself which is at the top.)

    – Nedas
    Nov 13 '18 at 16:11













  • Can you try removing the method:"POST" line from your $.ajax(... statement?

    – David R
    Nov 13 '18 at 16:15











  • I did, same issue.

    – Nedas
    Nov 13 '18 at 16:31











  • here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

    – Fermin Perdomo
    Nov 13 '18 at 18:37



















How do you execute this script from your .php file?

– David R
Nov 13 '18 at 15:55





How do you execute this script from your .php file?

– David R
Nov 13 '18 at 15:55













I've updated the question (unless you asked about the php script iself which is at the top.)

– Nedas
Nov 13 '18 at 16:11







I've updated the question (unless you asked about the php script iself which is at the top.)

– Nedas
Nov 13 '18 at 16:11















Can you try removing the method:"POST" line from your $.ajax(... statement?

– David R
Nov 13 '18 at 16:15





Can you try removing the method:"POST" line from your $.ajax(... statement?

– David R
Nov 13 '18 at 16:15













I did, same issue.

– Nedas
Nov 13 '18 at 16:31





I did, same issue.

– Nedas
Nov 13 '18 at 16:31













here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

– Fermin Perdomo
Nov 13 '18 at 18:37







here is the same problem stackoverflow.com/questions/16740802/… you need to give permission for the php user to allow it to run shell cmd.

– Fermin Perdomo
Nov 13 '18 at 18:37














1 Answer
1






active

oldest

votes


















0














Issue has been fixed.



Apparently I was giving a wrong filename for winston, since the home folder for www-data user was /var/www, it was using that folder to make the logs instead of /home/app.



This fixed my issue:



if(filename) 
o.transports.push(new winston.transports.File({
filename:__dirname+"/../logs/"+filename, //added __dirname here
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});
})
)
}));





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284450%2fstarting-a-node-js-script-through-php-disables-logs-permission-issue-apparrentl%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Issue has been fixed.



    Apparently I was giving a wrong filename for winston, since the home folder for www-data user was /var/www, it was using that folder to make the logs instead of /home/app.



    This fixed my issue:



    if(filename) 
    o.transports.push(new winston.transports.File({
    filename:__dirname+"/../logs/"+filename, //added __dirname here
    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});
    })
    )
    }));





    share|improve this answer




























      0














      Issue has been fixed.



      Apparently I was giving a wrong filename for winston, since the home folder for www-data user was /var/www, it was using that folder to make the logs instead of /home/app.



      This fixed my issue:



      if(filename) 
      o.transports.push(new winston.transports.File({
      filename:__dirname+"/../logs/"+filename, //added __dirname here
      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});
      })
      )
      }));





      share|improve this answer


























        0












        0








        0







        Issue has been fixed.



        Apparently I was giving a wrong filename for winston, since the home folder for www-data user was /var/www, it was using that folder to make the logs instead of /home/app.



        This fixed my issue:



        if(filename) 
        o.transports.push(new winston.transports.File({
        filename:__dirname+"/../logs/"+filename, //added __dirname here
        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});
        })
        )
        }));





        share|improve this answer













        Issue has been fixed.



        Apparently I was giving a wrong filename for winston, since the home folder for www-data user was /var/www, it was using that folder to make the logs instead of /home/app.



        This fixed my issue:



        if(filename) 
        o.transports.push(new winston.transports.File({
        filename:__dirname+"/../logs/"+filename, //added __dirname here
        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});
        })
        )
        }));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 15:07









        NedasNedas

        10312




        10312






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284450%2fstarting-a-node-js-script-through-php-disables-logs-permission-issue-apparrentl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python