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
Related
My socket server shuts down sometimes and I can't check why because there is no txt log where i could check last error messages, maybe there is way to write it in txt file after shutting down?
Invoke your program in the following way: node myserver.js 2> error_log.txt
When the program closes the error_log txt will have all messages, including the error message that made the program crash written in it.
To also get all console.log messages you can do node myserver.js > error_log.txt > 2>&1
(I used this command in the bash console on windows)
I'm trying to debug nodejs script (on Windows). I found that I can stop execution / set a breakpoint withing the script by using the 'debugger;' statement. However this seems to work only if node.exe called as:
node debug myscript.js
Problem is I'm not the one who's calling node.exe, so I can't pass the debug argument. Actually node.exe is being called many times before myscript.js. And myscript.js is in turn being called from some other.js. Running myscript.js directly (outside other.js) does not work.
Is there a way to wait for the debugger to attach?
If possible the solution should make use of some GUI debugger (like npm node-inspector).
Update 1:
To wait for the debugger in your application use:
npm install sleep # install locally, not global
Add code to myscript.js:
var sleep = require('sleep');
...
var done = 1;
while ( done != 1 ) {
console.log("PID=" + process.pid);
sleep(1);
}
...
Run your script (i.e. start whatever triggers its execution):
PID=3280
PID=3280
Start node-inspector:
C:\Users\Samo>node-inspector
Node Inspector v0.12.7
Visit http://127.0.0.1:8080/?port=5858 to start debugging.
Open a new cmd.exe and run:
C:\Users\Samo>tasklist /FI "IMAGENAME eq node.exe"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
node.exe 3944 Console 1 64,016 K
node.exe 3280 Console 1 119,276 K
From the same cmd.exe issue:
node -e "process._debugProcess(3280)"
In the debugged script's output, you should see:
PID=3280
Starting debugger agent.
Debugger listening on port 5858
Now go to the indicated URL (http://127.0.0.1:8080/?port=5858) using Chrome (not Firefox, but Chrome). You should be able to debug, now.
I said should because in my case it does not work. If I send _debugProcess to 3280 (my myscript.js) nothing happens. However if I send _debugProcess to 3944 I get the "Starting debugger agent." message. Why?
If you have the typical windows workspace such as Node, git and VSCode,
you can do it in these simple steps:
In VSCode, open launch.json configuration or create new by clicking on the wheel
(this is the debug view CtrlShiftD)
The node will listen on port 9229 by default, so add this configuration:
{
"type": "node",
"request": "attach",
"name": "Attach to 9229",
"port": 9229
},
Open Task Manager and locate the PID of your node process
I could identify my by the "build" folder where the index.js is.
open another cmd or git-bash and run this command,
where 21392 is the PID of your process.
node -e "process._debugProcess(21392)"
you should see this
Start debugging from VSCode Attach to 9229
Everything should be ready now.
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.
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.
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