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.
Related
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 simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever
I am trying to run node server using forever command.
I installed forever globally using:
npm install forever -g
After installing forever I try to run my node script by using below command:
node_modules\.bin\forever start app.js
Below is my console:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info: Forever processing file: app.js
Please help me to resolve this issue!
There is no problem here other than warnings for configs forever recommends you declare. If you see the final message there it tells you it has processed your script. Just run forever list and you should see your script running.
I ran into this same thing when installing npm via yum repository ( yum install npm ) and then installing forever whereas when I install node and npm via shell scripts and then install forever it doesn't occur. It must have something to do with the formulas for the package installer or potentially missing alias with flags with installer to set those values behind the scene.
Those don't mean it's not working. See below I created a js file using sample code from node's site and ran it manually (I flushed firewall to open port for app temporarily but you don't need that):
[root#app1 ~]# vi example.js
[root#app1 ~]# apf -f
apf(23924): {glob} flushing & zeroing chain policies
apf(23924): {glob} firewall offline
[root#app1 ~]# node example.js
Server running at http://127.0.0.1:1337/
I then start app using forever:
^C[root#app1 ~]# forever start example.js
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: example.js
Now I check to see if my app is running:
[root#app1 ~]# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] dan1 /usr/bin/node example.js 23976 23978 /root/.forever/dan1.log 0:0:0:27.320
[root#app1 ~]#
This solved my issue:
forever start -c node [path/to/app]
"-c" means - Run commnad; and then just run via nodejs
This way - you get the Respawn by default of min. 1000ms uptime
Taken from: https://github.com/nodejitsu/forever/issues/422, by "Basarat"
If you are using node js with express framework then script will not start using :
forever start app.js
First stop all running apps:
forever stopall
When this Express framework used it must be started with:
forever start ./bin/www
First stop all running apps:
forever stopall
then use this command. It works for me and solved my issue:
forever -w ./bin/www
and you should find this in package.json file:
"scripts": {
"start": "node ./bin/www"
}
I hope it helps you.
One thing that also produces same kind of output, but doesn't start the application is if forever is unable to write to the specified log file. I had a case where the log file had become too big and this prevented the process from starting.
Firstly change your package.json scripts like
"scripts": {
"start": "forever ./bin/www.js"
}
than start this command on linux console:sudo npm start
for windows just :npm start
I just ran into this today on an AWS Lightsail server, and NONE of the answers here or elsewhere had any effect. Everything worked fine until upgrading from NodeJS 10.x to 13.x. I tried removing and reinstalling forever, changing the permissions on the files and directories, etc, and I kept getting the EACCES error. The issue seemed to be that forever could not create directories within its .forever directory. The only thing that worked was to do the following:
1) Remove the .forever folder and all its subfolders and contents. For me, this was accomplished as follows:
sudo rm -rf /home/bitnami/.forever
2) Manually recreate the .forever folder:
sudo mkdir /home/bitnami/.forever
3) Manually set the permissions on the .forever folder:
sudo chmod -R o+rwx /home/bitnami/.forever
4) Manually recreate the .forever/pids folder:
sudo mkdir /home/bitnami/.forever/pids
5) Manually set the permissions on the .forever/pids folder:
sudo chmod -R o+rwx /home/bitnami/.forever/pids
6) Manually recreate the .forever/sock folder:
sudo mkdir /home/bitnami/.forever/sock
7) Manually set the permissions on the .forever/sock folder:
sudo chmod -R o+rwx /home/bitnami/.forever/sock
8) Run my NodeJS app via forever again with the sudo command.
9) List the processes forever is running, and verify that my app was there.
I'm not sure why I had to go through all of this, as setting the permissions recursively should have done the same thing, but after doing this, forever started running perfectly as it did before.
Hope this helps someone.
forever stopall
cd /<app-folder>
forever -w ./bin/www
This just worked for me in a Google Cloud Bitnami VM
If you setup a module globaly (-g option) "forever" is in the $path
forever start app.js
should work.
i have written an upstart script for ubuntu to launch my node.js server manually or on startup. But it always terminates with status 127 and i can't find more information about what is going wrong. If i execute it manually then it works and i also tested it on ubuntu 12.10 where it also works ... it only fails to work on ubuntu 10.04 which is the production server i'm using.
The script:
description ""
author ""
start on started mountall
stop on shutdown
respawn
respawn limit 20 5
# Max open files are # 1024 by default. Bit few.
limit nofile 32768 32768
env HOME=/home/projects/<project_name>/data/current
script
export HOME=$HOME
chdir $HOME
exec sudo -u <user_to_launch_the_script> /usr/bin/node /home/projects/<project_name>/data/current/server.js 2>&1 >> /var/log/node.log
end script
any idea where to find more information about the status 127? Or how i can fix this? i have looked in /var/log/daemon.log and in /var/log/syslog.log .. but there is no relevant info except for 'main process (29520) terminated with status 127'.
kind regards,
Daan
127 in bash means: "command not found", illegal_command, possible problem with $PATH or a typo.
Source: http://tldp.org/LDP/abs/html/exitcodes.html
This might be a question for server fault, as it is bash related, but this question / answer might help you:
https://serverfault.com/questions/277706/cron-fails-with-exit-status-127
Had the same error messages, tracked it down in custom upstart log that failed with /usr/bin/env: node: No such file or directory, this was my fix:
https://github.com/joyent/node/issues/3911
Had this issue. I am deploying web app with gunicorn in ubuntu server 14.04.
Move your core instructions to a bash script. And remember to make the script executable. I had neglected to make the bash script executable and so I was getting the 127.
description "Gunicorn app running myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid <user>
setgid <group>
exec bash /path/to/bash/script/
then my bash script
#!/bin/bash
# description "bash script that handles loading env and running gunicorn"
# load up the project's virtualenv
source /path/to/virtualenv/bin/activate
# minimal settings
exec gunicorn app:app
I would like to automatically run node server when instances are created (using forever). I am on Ubuntu 11.10 (Canonical), I followed the instructions here exactly on creating launch config using user script: http://alestic.com/2011/11/ec2-schedule-instance
I can't seem to get this to work. Below is my startup script:
#!/bin/bash
set -e -x
/home/MyUserName/node_modules/.bin/forever stopall
/home/MyUserName/node_modules/.bin/forever start node.js/app.js
The launch config is created using this cmd:
as-create-launch-config MyLC --image-id ami-b6a3f8f2 --user-data-file user-data-script.sh --instance-type m1.small
Found the issue, I have to run forever as the user, not root, wonder why...like so:
sudo -u MyUserName /home/MyUserName/node_modules/.bin/forever start node.js/app.js
Are you fully qualifying the app.js file? Could it just be this line?
/home/MyUserName/node_modules/.bin/forever start /home/MyUserName/node.js/app.js