node.js app runs from command line but gives errors when run from cron job on reboot on ubuntu 14.04 LTS












0















When starting my app manually using forever, it works fine. It runs on localhost:8080 and nginx does the routing.



When using a cronjob my starter.sh script runs but in the forever log I get an error. If I run the starter.sh script from the command line my app also works. It only does not work when run from a cronjob. Here are the details:



forever list command output:



data:    [0] pQHz /root/.nvm/v0.10.26/bin/node server.js 953     1077 /root/.forever/pQHz.log 0:0:0:7.90 


starter.sh bash script that runs on reboot:



#!/bin/sh

if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "n") -eq 0 ]
then
export PATH=/root/.nvm/v0.10.26/bin/:$PATH
forever start --spinSleepTime 10000 --sourceDir /root/neet server.js >> /root/cronlog.txt 2>&1
fi


crontab -e:



@reboot sh /root/neet/starter.sh


starter.sh output:



^[[33mwarn^[[39m:    --minUptime not set. Defaulting to: 1000ms
^[[32minfo^[[39m: Forever processing file: ^[[90mserver.js^[[39m


forever ERROR log:



Error: ENOENT, no such file or directory './client/index.html'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at self.populateCache (/root/neet/server.js:42:40)
at self.initialize (/root/neet/server.js:174:14)
at Object.<anonymous> (/root/neet/server.js:191:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8
error: Script restart attempt #1


This is the line of code in my app that the error references:



self.zcache['index.html'] = fs.readFileSync('./client/index.html');


I'm really stumped as to why it can't read the file only when running from a cronjob on reboot. Some help would be very much appreciated.










share|improve this question
















bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

    – supb
    Apr 28 '14 at 20:50
















0















When starting my app manually using forever, it works fine. It runs on localhost:8080 and nginx does the routing.



When using a cronjob my starter.sh script runs but in the forever log I get an error. If I run the starter.sh script from the command line my app also works. It only does not work when run from a cronjob. Here are the details:



forever list command output:



data:    [0] pQHz /root/.nvm/v0.10.26/bin/node server.js 953     1077 /root/.forever/pQHz.log 0:0:0:7.90 


starter.sh bash script that runs on reboot:



#!/bin/sh

if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "n") -eq 0 ]
then
export PATH=/root/.nvm/v0.10.26/bin/:$PATH
forever start --spinSleepTime 10000 --sourceDir /root/neet server.js >> /root/cronlog.txt 2>&1
fi


crontab -e:



@reboot sh /root/neet/starter.sh


starter.sh output:



^[[33mwarn^[[39m:    --minUptime not set. Defaulting to: 1000ms
^[[32minfo^[[39m: Forever processing file: ^[[90mserver.js^[[39m


forever ERROR log:



Error: ENOENT, no such file or directory './client/index.html'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at self.populateCache (/root/neet/server.js:42:40)
at self.initialize (/root/neet/server.js:174:14)
at Object.<anonymous> (/root/neet/server.js:191:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8
error: Script restart attempt #1


This is the line of code in my app that the error references:



self.zcache['index.html'] = fs.readFileSync('./client/index.html');


I'm really stumped as to why it can't read the file only when running from a cronjob on reboot. Some help would be very much appreciated.










share|improve this question
















bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

    – supb
    Apr 28 '14 at 20:50














0












0








0


0






When starting my app manually using forever, it works fine. It runs on localhost:8080 and nginx does the routing.



When using a cronjob my starter.sh script runs but in the forever log I get an error. If I run the starter.sh script from the command line my app also works. It only does not work when run from a cronjob. Here are the details:



forever list command output:



data:    [0] pQHz /root/.nvm/v0.10.26/bin/node server.js 953     1077 /root/.forever/pQHz.log 0:0:0:7.90 


starter.sh bash script that runs on reboot:



#!/bin/sh

if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "n") -eq 0 ]
then
export PATH=/root/.nvm/v0.10.26/bin/:$PATH
forever start --spinSleepTime 10000 --sourceDir /root/neet server.js >> /root/cronlog.txt 2>&1
fi


crontab -e:



@reboot sh /root/neet/starter.sh


starter.sh output:



^[[33mwarn^[[39m:    --minUptime not set. Defaulting to: 1000ms
^[[32minfo^[[39m: Forever processing file: ^[[90mserver.js^[[39m


forever ERROR log:



Error: ENOENT, no such file or directory './client/index.html'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at self.populateCache (/root/neet/server.js:42:40)
at self.initialize (/root/neet/server.js:174:14)
at Object.<anonymous> (/root/neet/server.js:191:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8
error: Script restart attempt #1


This is the line of code in my app that the error references:



self.zcache['index.html'] = fs.readFileSync('./client/index.html');


I'm really stumped as to why it can't read the file only when running from a cronjob on reboot. Some help would be very much appreciated.










share|improve this question
















When starting my app manually using forever, it works fine. It runs on localhost:8080 and nginx does the routing.



When using a cronjob my starter.sh script runs but in the forever log I get an error. If I run the starter.sh script from the command line my app also works. It only does not work when run from a cronjob. Here are the details:



forever list command output:



data:    [0] pQHz /root/.nvm/v0.10.26/bin/node server.js 953     1077 /root/.forever/pQHz.log 0:0:0:7.90 


starter.sh bash script that runs on reboot:



#!/bin/sh

if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "n") -eq 0 ]
then
export PATH=/root/.nvm/v0.10.26/bin/:$PATH
forever start --spinSleepTime 10000 --sourceDir /root/neet server.js >> /root/cronlog.txt 2>&1
fi


crontab -e:



@reboot sh /root/neet/starter.sh


starter.sh output:



^[[33mwarn^[[39m:    --minUptime not set. Defaulting to: 1000ms
^[[32minfo^[[39m: Forever processing file: ^[[90mserver.js^[[39m


forever ERROR log:



Error: ENOENT, no such file or directory './client/index.html'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at self.populateCache (/root/neet/server.js:42:40)
at self.initialize (/root/neet/server.js:174:14)
at Object.<anonymous> (/root/neet/server.js:191:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
error: Forever detected script exited with code: 8
error: Script restart attempt #1


This is the line of code in my app that the error references:



self.zcache['index.html'] = fs.readFileSync('./client/index.html');


I'm really stumped as to why it can't read the file only when running from a cronjob on reboot. Some help would be very much appreciated.







bash cron nodejs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 28 '14 at 18:19









troylatroy

1,00611018




1,00611018










asked Apr 28 '14 at 17:27









supbsupb

112




112





bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

    – supb
    Apr 28 '14 at 20:50



















  • self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

    – supb
    Apr 28 '14 at 20:50

















self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

– supb
Apr 28 '14 at 20:50





self.zcache['index.html'] = fs.readFileSync('./client/index.html'); should be self.zcache['index.html'] = fs.readFileSync(__dirname + '/client/index.html');

– supb
Apr 28 '14 at 20:50










2 Answers
2






active

oldest

votes


















0














When your running the script from cron, your current working directory will be the home directory of user who the script is called for. So when your script is trying to find the relative path ./client/index.html the file does not exist.






share|improve this answer

































    0














    It is better to configure it in a version independent way (you probably have multiple node versions installed since you are using nvm).



    This is the same snippet that the nvm-installer adds to your /root/.bashrc script



    @doc Required to enable node/nvm for a cronjob ()



    @doc cron uses by default a short PATH = /usr/bin:/bin and does not execute /root/.bashrc (equal to /root/.bashrc)



    @doc nvm (node version manager) does not configure itself in a crontab job (that is only done for interactive shells in my /root/.bashrc).



    export NVM_DIR="/root/.nvm" # or perhaps ~./nvm in your environment.
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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%2faskubuntu.com%2fquestions%2f457159%2fnode-js-app-runs-from-command-line-but-gives-errors-when-run-from-cron-job-on-re%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      When your running the script from cron, your current working directory will be the home directory of user who the script is called for. So when your script is trying to find the relative path ./client/index.html the file does not exist.






      share|improve this answer






























        0














        When your running the script from cron, your current working directory will be the home directory of user who the script is called for. So when your script is trying to find the relative path ./client/index.html the file does not exist.






        share|improve this answer




























          0












          0








          0







          When your running the script from cron, your current working directory will be the home directory of user who the script is called for. So when your script is trying to find the relative path ./client/index.html the file does not exist.






          share|improve this answer















          When your running the script from cron, your current working directory will be the home directory of user who the script is called for. So when your script is trying to find the relative path ./client/index.html the file does not exist.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 6 '14 at 13:33







          user280493

















          answered Aug 6 '14 at 12:39









          user312909user312909

          1




          1

























              0














              It is better to configure it in a version independent way (you probably have multiple node versions installed since you are using nvm).



              This is the same snippet that the nvm-installer adds to your /root/.bashrc script



              @doc Required to enable node/nvm for a cronjob ()



              @doc cron uses by default a short PATH = /usr/bin:/bin and does not execute /root/.bashrc (equal to /root/.bashrc)



              @doc nvm (node version manager) does not configure itself in a crontab job (that is only done for interactive shells in my /root/.bashrc).



              export NVM_DIR="/root/.nvm" # or perhaps ~./nvm in your environment.
              [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm






              share|improve this answer




























                0














                It is better to configure it in a version independent way (you probably have multiple node versions installed since you are using nvm).



                This is the same snippet that the nvm-installer adds to your /root/.bashrc script



                @doc Required to enable node/nvm for a cronjob ()



                @doc cron uses by default a short PATH = /usr/bin:/bin and does not execute /root/.bashrc (equal to /root/.bashrc)



                @doc nvm (node version manager) does not configure itself in a crontab job (that is only done for interactive shells in my /root/.bashrc).



                export NVM_DIR="/root/.nvm" # or perhaps ~./nvm in your environment.
                [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm






                share|improve this answer


























                  0












                  0








                  0







                  It is better to configure it in a version independent way (you probably have multiple node versions installed since you are using nvm).



                  This is the same snippet that the nvm-installer adds to your /root/.bashrc script



                  @doc Required to enable node/nvm for a cronjob ()



                  @doc cron uses by default a short PATH = /usr/bin:/bin and does not execute /root/.bashrc (equal to /root/.bashrc)



                  @doc nvm (node version manager) does not configure itself in a crontab job (that is only done for interactive shells in my /root/.bashrc).



                  export NVM_DIR="/root/.nvm" # or perhaps ~./nvm in your environment.
                  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm






                  share|improve this answer













                  It is better to configure it in a version independent way (you probably have multiple node versions installed since you are using nvm).



                  This is the same snippet that the nvm-installer adds to your /root/.bashrc script



                  @doc Required to enable node/nvm for a cronjob ()



                  @doc cron uses by default a short PATH = /usr/bin:/bin and does not execute /root/.bashrc (equal to /root/.bashrc)



                  @doc nvm (node version manager) does not configure itself in a crontab job (that is only done for interactive shells in my /root/.bashrc).



                  export NVM_DIR="/root/.nvm" # or perhaps ~./nvm in your environment.
                  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 9 '16 at 9:45









                  RolfRolf

                  53267




                  53267






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f457159%2fnode-js-app-runs-from-command-line-but-gives-errors-when-run-from-cron-job-on-re%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

                      香粉寮

                      GameSpot