Starting a node.js via cron fails silently

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am currently trying to set up a cronjob that will start a node.js server whenever that server is down.



For that purpose, I have written a simple script to test and start the node server.



#!/bin/bash

ts=$(date +%T)

if pgrep -f "node"
then
echo $ts": node running"
else
echo $ts": node not running"
'/usr/bin/node' '/home/pi/project/serveronly/index.js' > '/home/pi/project/node.log'
fi


The corresponding crontab is:



m h dom mon dow command
* * * * * /home/pi/project/sanity_check.sh >> /home/pi/project/cron.log 2&>1


my logging shows the following:
If I simply start the script it will start the server and log the node.js output properly.



For the script started by cron it looks this way:
If an instance of node.js is currently running it will detect that and log accordingly.
If none is detected it will log into cron.log properly, but it will log nothing into node.log and it will not start a server.







share|improve this question





















  • What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
    – kubanczyk
    Jul 5 at 12:33











  • What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
    – Junge
    Jul 5 at 12:47














up vote
0
down vote

favorite












I am currently trying to set up a cronjob that will start a node.js server whenever that server is down.



For that purpose, I have written a simple script to test and start the node server.



#!/bin/bash

ts=$(date +%T)

if pgrep -f "node"
then
echo $ts": node running"
else
echo $ts": node not running"
'/usr/bin/node' '/home/pi/project/serveronly/index.js' > '/home/pi/project/node.log'
fi


The corresponding crontab is:



m h dom mon dow command
* * * * * /home/pi/project/sanity_check.sh >> /home/pi/project/cron.log 2&>1


my logging shows the following:
If I simply start the script it will start the server and log the node.js output properly.



For the script started by cron it looks this way:
If an instance of node.js is currently running it will detect that and log accordingly.
If none is detected it will log into cron.log properly, but it will log nothing into node.log and it will not start a server.







share|improve this question





















  • What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
    – kubanczyk
    Jul 5 at 12:33











  • What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
    – Junge
    Jul 5 at 12:47












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently trying to set up a cronjob that will start a node.js server whenever that server is down.



For that purpose, I have written a simple script to test and start the node server.



#!/bin/bash

ts=$(date +%T)

if pgrep -f "node"
then
echo $ts": node running"
else
echo $ts": node not running"
'/usr/bin/node' '/home/pi/project/serveronly/index.js' > '/home/pi/project/node.log'
fi


The corresponding crontab is:



m h dom mon dow command
* * * * * /home/pi/project/sanity_check.sh >> /home/pi/project/cron.log 2&>1


my logging shows the following:
If I simply start the script it will start the server and log the node.js output properly.



For the script started by cron it looks this way:
If an instance of node.js is currently running it will detect that and log accordingly.
If none is detected it will log into cron.log properly, but it will log nothing into node.log and it will not start a server.







share|improve this question













I am currently trying to set up a cronjob that will start a node.js server whenever that server is down.



For that purpose, I have written a simple script to test and start the node server.



#!/bin/bash

ts=$(date +%T)

if pgrep -f "node"
then
echo $ts": node running"
else
echo $ts": node not running"
'/usr/bin/node' '/home/pi/project/serveronly/index.js' > '/home/pi/project/node.log'
fi


The corresponding crontab is:



m h dom mon dow command
* * * * * /home/pi/project/sanity_check.sh >> /home/pi/project/cron.log 2&>1


my logging shows the following:
If I simply start the script it will start the server and log the node.js output properly.



For the script started by cron it looks this way:
If an instance of node.js is currently running it will detect that and log accordingly.
If none is detected it will log into cron.log properly, but it will log nothing into node.log and it will not start a server.









share|improve this question












share|improve this question




share|improve this question








edited Jul 5 at 13:15
























asked Jul 5 at 12:22









Junge

12




12











  • What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
    – kubanczyk
    Jul 5 at 12:33











  • What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
    – Junge
    Jul 5 at 12:47
















  • What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
    – kubanczyk
    Jul 5 at 12:33











  • What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
    – Junge
    Jul 5 at 12:47















What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
– kubanczyk
Jul 5 at 12:33





What is the pwd inside cron? Please add it to the question body. Why don't you add cd / to the script?
– kubanczyk
Jul 5 at 12:33













What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
– Junge
Jul 5 at 12:47




What do you mean by pwd? (as for the cd, I will try that out. It should help me to avoid some mistakes. Thank you)
– Junge
Jul 5 at 12:47










1 Answer
1






active

oldest

votes

















up vote
0
down vote













  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.


  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.


I would use this, instead:



/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log





share|improve this answer





















  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
    – Junge
    Jul 5 at 12:44










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453612%2fstarting-a-node-js-via-cron-fails-silently%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.


  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.


I would use this, instead:



/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log





share|improve this answer





















  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
    – Junge
    Jul 5 at 12:44














up vote
0
down vote













  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.


  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.


I would use this, instead:



/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log





share|improve this answer





















  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
    – Junge
    Jul 5 at 12:44












up vote
0
down vote










up vote
0
down vote









  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.


  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.


I would use this, instead:



/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log





share|improve this answer













  1. You're not providing the full path to /home/pi/project/serveronly/index.js -- you're missing the initial forward-slash, so the node program is failing to find your script.


  2. the command is successfully overwriting a file under your home directory, home/pi/project/node.log; presumably that's what you meant with project.log. If you want to append to that log, use two greater-than signs, not one.


I would use this, instead:



/usr/bin/node /home/pi/project/serveronly/index.js >> /home/pi/project/node.log






share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 5 at 12:33









Jeff Schaller

30.8k846104




30.8k846104











  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
    – Junge
    Jul 5 at 12:44
















  • Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
    – Junge
    Jul 5 at 12:44















Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
– Junge
Jul 5 at 12:44




Thank you. this explains the "weird" logging behaviour. Unfortunately the missing forward-slash is just an error I made while copying. I edited my question accordingly.
– Junge
Jul 5 at 12:44












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453612%2fstarting-a-node-js-via-cron-fails-silently%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)