I'm running Ghost blog on a MEAN stack. I'm using forever to keep the blog alive after starting it. I've also setup crontab to launch the forever start command on server reboot.
I can't work out how to get it to start in production mode with crontab.
If I did this straight into the command line, I'd do this:
NODE_ENV=production forever start index.js
That works great, but the following line in my crontab ignores the production mode part and starts it in development mode:
#reboot NODE_ENV=production /usr/local/bin/forever start /path/to/blog/index.js
If you want to set the environment variable for all lines, do it like this:
NODE_ENV=production
#reboot /usr/local/bin/forever start /path/to/blog/index.js
Unfortunately you need to make a .sh to set it per line.
Related
I have installed forever on shared hosting Cpanel for node js application when I run forever start app.js, node js application works on the server.
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
But when I close terminal or console then node app stopped working. Any suggestions around it?
Closing the terminal will typically close the application running. Consider using tmux or screen to launch the app or also nohup.
Launching this way should be considered a short-term solution. You probably want to look at how your specific Linux distribution handles startup scripts and services.
like maldina said closing forever will stop your app consider
consider upstart (it runs tasks when the computer is started)
you basiclly create a conf file and place it in your init folder "var/etc/init"
the file content should look like this
#!upstart
description "my-app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/server.js >> /var/log/myapp.log 2>&1
you can use the following commands to manage your app
sudo start my-app
sudo stop my-app
sudo restart my-app
What I have now is a node server (with forever.js in Ubuntu 14.04 LTS) which is needed to start each time server PC starts.
So, to do that what we need is this command (every time PC start or restarts)-
forever start /var/www/websocket/websocket.js
If I run this command in direct command line, it works fine.
But I want it automatically start when server PC turn on or restart.
So what I have done (according to https://stackoverflow.com/a/13388741/2193439 ) is-
Run crontab -e and put this code in the console-
#reboot forever start /var/www/websocket/websocket.js
like this-
And to check my corn log, I have done this-
sudo grep --color -i cron /var/log/syslog
And found something like this-
But I am finding the server is not running by this-
forever list
and having - No forever processes running
But if I run this-
forever start /var/www/websocket/websocket.js
And then run this-
forever list
Then I am having this-
And I am confirming you that crontab is also running because if I change this-
#reboot forever start /var/www/websocket/websocket.js
To this-
#reboot cd /var/www/websocket/ && touch cron_try.txt
I am having the file each time PC restarts.
I have already tried this-
Automatically start forever (node) on system restart
Automatically restart node server
http://www.hacksparrow.com/make-forever-reboot-proof-with-cron.html
https://github.com/foreverjs/forever/issues/58
And this-
cronjob does not execute a script that works fine standalone
Is almost my problem. But I had set it during reboot and for Node forever.js.
So it does not solve my problem.
Can anyone please help?
I have solved this by this way-
First, find the forever location by this-
which forever
And get this-
/usr/local/bin/forever
And then put this path in crontab like this-
#reboot /usr/local/bin/forever start /var/www/websocket/websocket.js
And we are done :)
I have a dedicated Godaddy server.
I need to run a node app on it.
I can do that by SSH running
node app.js
The problem is that when the ssh connection is disconnected ... The app stops working.
How do I run it so that it does not stops.
Create a shell script (eg. yourScript.sh), and put your command "node app.js" inside.
Example yourScript.sh:
#!/usr/bin/env bash
node app.js
Make sure you have execute permission:
chmod +x yourScript.sh
Then run with:
nohup ./yourScript.sh &
This will mean the process doesn't exit when you disconnect. Nohup catches the HUP signals. Nohup doesn't put the job automatically in the background. We need to tell that explicitly using &
I use Supervisor to run Node.js app in production. It has convenient command line API to show status of the process, start/stop it, allows restart on reboot, etc.
Config file looks like this:
[program:myapp]
directory=/home/myapp/app/current
command=node server/index.js
autostart=true
autorestart=true
environment=
PORT=3000,
MY_ANOTHER_VAR="something"
stderr_logfile=/var/log/myapp.err.log
stdout_logfile=/var/log/myapp.out.log
user=myapp
I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:
#reboot /var/www/ghost/launch.sh
The script has the following code:
#!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi
When I sudo reboot the server, and use forever list to find the processes running, I see the following:
data: [0] sHyo /usr/bin/nodejs index.js 1299 1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957
When I nano to that log file, the log says the following:
^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m
Error: Could not locate a configuration file.
at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
at Object.cb [as oncomplete] (fs.js:168:19)
error: Forever detected script was killed by signal: null
It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????
When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:
^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m
What is wrong with my script or crontab that it cannot launch the script properly?
I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.
That's the thing, your cron job is not doing the same:
#reboot /var/www/ghost/launch.sh
This script is executed from your home directory. One way to fix is to change your crontab:
#reboot cd /var/www/ghost; ./launch.sh
Another way is to add this line near the top of launch.sh, anywhere before launching forever:
# change to the directory of this script
cd $(dirname "$0")
Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.
I've been setting up my server recently and today I had to restart it... then I realised all of my Node apps I had running weren't running anymore. I'm using Node Forever module to keep the apps running, but then I realised I still need to have them starting when my server restarts or shut downs and powers up again.
I have been researching the best way to do this, but what I'm trying just doesn't seem to work. I've created an Upstart script in my /etc/init/ folder on my Ubuntu Server 10.04LTS remote server and tried restarting and it doesn't seem to do anything. Nothing is getting listed when I run forever list.
Here is my current Upstart script I was trying out today:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
script
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
end script
I use Forever in a Node script as I find it easier to configure it how I want. It's confirmed that the script runs just fine if I do this outside the script, there is just something wrong with the Upstart script itself. It seems to have the same permissions as all the other Upstart scripts in /etc/init/ folder.
As an additional note, I have gone through almost all the answers I could find here on StackOverflow, and that it how I got together the script that I have at present.
UPDATE:
With Tom's answer, I have now tried:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
But it's still not working.
So I don't know why this isn't running when I restart my server. Please help!
This is not a very happy setup. The way upstart works is that it starts your process running it using the process id for the start command. Forever JS works similarly, it is probably inspired by Upstart.
When you try to run forever.js with upstart, the forever process you create in your upstart script exits immediately after starting. Upstart counts on having the process continue to run.
When I tried to run forever using upstart, I wound up with five different forever process running because upstart thought it had failed to start forever, and it retried five times.
Did you try doing it without the start script lines?
description "my server"
author "name"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
#respawn if you were not using forever
exec sudo /usr/local/bin/node myapp.forever.js
Source: http://caolanmcmahon.com/posts/deploying_node_js_with_upstart
I've opted to use an #reboot statement in the user's crontab file, which will execute forever on server restarts.
#reboot forever start app.js
Additional Reading - http://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/