Starting a node.js via cron fails silently

Clash 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.
cron raspberry-pi node.js
add a comment |Â
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.
cron raspberry-pi node.js
What is thepwdinside cron? Please add it to the question body. Why don't you addcd /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
add a comment |Â
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.
cron raspberry-pi node.js
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.
cron raspberry-pi node.js
edited Jul 5 at 13:15
asked Jul 5 at 12:22
Junge
12
12
What is thepwdinside cron? Please add it to the question body. Why don't you addcd /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
add a comment |Â
What is thepwdinside cron? Please add it to the question body. Why don't you addcd /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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.the command is successfully overwriting a file under your home directory,
home/pi/project/node.log; presumably that's what you meant withproject.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
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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.the command is successfully overwriting a file under your home directory,
home/pi/project/node.log; presumably that's what you meant withproject.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
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
add a comment |Â
up vote
0
down vote
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.the command is successfully overwriting a file under your home directory,
home/pi/project/node.log; presumably that's what you meant withproject.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
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.the command is successfully overwriting a file under your home directory,
home/pi/project/node.log; presumably that's what you meant withproject.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
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.the command is successfully overwriting a file under your home directory,
home/pi/project/node.log; presumably that's what you meant withproject.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
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What is the
pwdinside cron? Please add it to the question body. Why don't you addcd /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