Node.js Forever is not creating Log files - javascript

2 nodejs scripts are being handled by forever. The system is using forever v0.11.1 and node v0.10.29
# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] D34J userdown app/main.js 7441 10950 /root/.forever/D34J.log 0:2:31:45.572
data: [1] P0BX userdown app/main.js 11242 11261 /root/.forever/P0BX.log 0:2:20:22.157
# forever logs 0
error: undefined
# forever logs 1
error: undefined
Question: Why are the log files created by forever missing? Restarting the 2 processes still doesn't create any log files...
The directory /root/.forever does not show the log files too!
# ls -la /root/.forever
total 20
drwxr-xr-x 4 root root 4096 Jul 4 11:37 .
drwx------ 8 root root 4096 Jul 10 13:24 ..
-rw-r--r-- 1 root root 259 Jul 10 19:34 config.json
drwxr-xr-x 2 root root 4096 Jul 4 11:37 pids
drwxr-xr-x 2 root root 4096 Jul 10 17:12 sock

If you start your node process with forever your_script.js and don't specify a log file, forever will write your logs to the terminal (or cmd on Windows). The log file that is shown when you run forever list or forever logs does not reflect reality, since it's not created in that scenario. But if you specify log files, following the options:
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
, like forever -l console.log -e error.log your_script.log, they will be created.
If you want forever to automatically create a log file for you, you have to start your script as a daemon, with forever start your_script.js. In this case, you can also specify your log files.
In the docs page you can see all the command line options.

To answer Shreejibawa (since I can't comment yet)...
forever is very sensitive to the order of the arguments. Take a look at their documentation and notice that the options must come before the script.
forever [action] [options] SCRIPT [script-options]
Instead of: forever start bin/www -e logs/error.log -l logs/logs.log
Try: forever start -e /path/to/logs/error.log -l /path/to/logs/logs.log your_script.js

I had the same problem in OSX. In OSX (at least), the command forever app.js starts the forever process in the foreground and doesn't write the log to file, even when -l and -e are provided. In this case, forever list lists the file even though it isn't there and forever logs i produces error: undefined.
When the forever DAEMON is started using forever start app.js, the log files do appear and can be viewed using forever logs i.

Related

Python "print" does not log to Forever.js Logs

A Python 2.7 script outputs some debug info via print as it runs.
....
print datetime.now()
...
This script is started by forever.js:
forever start -c python myPrintingScript.py
However when I want to check the output of the Python script, nothing shows up in the logs.
Running forever logs 0 returns nothing. But the script definitely is running and has not crashed/restarted.
Question: Is there a way to get the Python script to print debug info such that it also appears in the log files of forever?
Please do as this:
import sys
print "xxxx"
sys.stdout.flush()
Then you will see xxxx in the log file that forever created.
You can specify output logs for forever
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
For example:
forever -o out.log -e err.log myPrintingScript.py

Node.js and Forever "exited with code: 0"

CentOs 6.5 using root acount, I have a working Node.js Express app:
root#vps [/home/test/node]# npm start app.js
> test#0.0.1 start /home/test/node
> node ./bin/www app.js
The app can be seen working on the internet browser. I stop the app and try to run it with forever:
root#vps [/home/test/node]# forever start app.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: app.js
root#vps [/home/test/node]#
Throws a couple of warnings that should not be a problem and looks like it should be working but its not showing on the browser, forever list:
root#vps [/home/test/node]# forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] OkGm /usr/local/bin/node app.js 32222 32227 /root/.forever/OkGm.log STOPPED
root#vps [/home/test/node]#
If I check OkGm.log:
error: Forever detected script exited with code: 0
Why is the app not working when I run it with forever?
Ok I found out what was happening. I was trying to run:
forever start app.js
When this Express app must be started with:
forever start ./bin/www
There was no useful info on internet when searching for this by the error log output ("exited with code: 0"), so I hope this answer helps begginers like me in what I think can be an easy mistake to make.

How to make this server process run forever?

I have this Javascript Signal server running using nodejs.
But daily it's crashing as a result whole service goes down. I am using following infinite loop to restart the nodejs script if it's crashed or not running. But it's not perfectly working.
Can anyone optimise it or is there any better way to keep the a.js up and running always if suddenly the process was not alive.
#!/bin/bash
while :
do
# 1
videoupload=$(pgrep -f "a.js")
if [ $videoupload ]; then
log1="running a $1 $2"
else
log1="re-launch a $1 $2"
nohup node /var/tmp/signal/a.js 2>&1 | tee -a /var/tmp/signal.log &
fi
echo $log1
sleep 1
done
If you're using the new CentOS 6, a much better way to handle this is to put it in an Upstart script. Upstart monitors all the system daemons and makes sure they stay running. The Upstart config below will also launch your process when the system boots.
edit the file /etc/init/a.conf and put the following config in it. You'll need to sudo to edit as root.
description "a.js"
author "YumYumYum"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
script
echo $$ > /var/run/a.pid;
exec node /var/tmp/signal/a.js
end script
post-stop script
rm -f /var/run/a.pid
end script
Now that you've created an Upstart config for your process you can start it from the command line:
$ sudo service a start
Upstart will monitor your process and will restart it any time it goes down. It also redirects logs to /var/log/upstart/a.log.

why node.js process is killed?

I have developed one app in node.js. Recently I noticed when I am doing any changes in my "public" directory of my application, one error is recorded in my log file as follows:
error: restarting script because /home/{user}/workspace/{app_folder}/img/{filename}.jpg changed.
error: Forever detected script was killed by signal: SIGKILL
error: Forever restarting script for 1 time
Express server listening on port 3000
I have already set --watchIgnore parameter in my forever script file in /etc/init/{app}.config
env IGNORE_DIRECTORY="/home/{user}/workspace/{app_folder}/img/**"
exec forever --sourceDir $APPLICATION_DIRECTORY --watchIgnore $IGNORE_DIRECTORY \
-a -w -l $LOG --minUptime 5000 --spinSleepTime 2000 \
start $APPLICATION_START
What am I missing?
Note that the log shows {user} and not your actual user directory. This path looks like it was copied from a user guide, where you were meant to replace those quasi-variables with something.
You use bash environment variables (I assume you're using bash) like this:
env IGNORE_DIRECTORY="~/workspace/${APPLICATION_DIRECTORY}/img/**"
It looks like app_folder is actually defined for you as APPLICATION_DIRECTORY. You can also use ~/ as a shortcut for the current user's home folder.

Using nodejs's Forever to output console.logs to screen

I just discovered that my nodejs app keeps crashing, so I've used forever app.js to start my app and have it automatically restarted when it crashes.
Problem: Now my app outputs alot of useful information as it runs via console.log and util.log. I used to use screen to run the node app, but now that I'm using forever to run the nodejs app, I can no longer see all the outputs.
Is there any way to see all the output from the nodejs app in realtime?
Directly with forever command :
forever logs app.js -f
It shows in realtime output of your application and forever logs (shows detected changes & restart messages).
You can watch a logfile live by using this shell-command.
tail -f /path/to/logfile
Not sure if this is what you needed.
Simplest way to go is
Run :
forever logs // will give you list of log files
forever logs 0 -f // then just provide the index of log
If you pass the --help flag to Forever you'll see this example:
forever -o out.log -e err.log my-script.js
-o and -e define the stdout and stderr log files.
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
Forever seems a little overkill for development. Give Supervisor a shot and see if you like it better:
npm install -g supervisor
supervisor app.js
I am running nodejs on AWS EC2 instance and this worked for me.
Note: logs are accessible if you are root owner. So first use sudo su then
forever -a -o out.log -e err.log yourServerApp.js
It will tail console log (in case you track event messages from log) and error log. I hope it helps someone.
linux : tail -f /path/to/logfile.log
windows : enter PowerShell -> Get-Content /path/to/logfile.log -Wait -Tail 1000

Categories

Resources