A simple testing app called myapp:
const fs = require("fs");
const json_encode = require('json_encode');
//emulate a real error
var wrong;
var test = json_encode(wrong);
//TypeError: Cannot read property 'replace' of undefined at json_encode...
If I try to save myapp_error.log for myapp from command line I use:
node myapp2.js > myapp_error.log 2>&1
//myapp_error.log is created and I can read the error logged
I need to do the same but if I starting myapp as service in /etc/systemd/system like
systemctl start myapp
The service (tested and running ok):
[Unit]
Description=myapp
[Service]
ExecStart=/path/to/myapp/myapp.js
Restart=my_username
User=nobody
Group=www-data
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/path/to/myapp
[Install]
WantedBy=multi-user.target
I have tried:
ExecStart=/path/to/myapp/myapp.js > myapp_error.log 2>&1 (this do nothing)
also tried:
systemctl start myapp > myapp_error.log 2>&1 (this write a empty file myapp_error.log in /etc/systemd/system)
CodePudding user response:
The [Service] block in your unit file has options for stdout/stderr logs:
StandardOutput=append:/path/to/stdout.log
StandardError=append:/path/to/stderr.log
Edit (from comments discussion):
Running node itself was also missing:
ExecStart=/usr/bin/node /path/to/myapp/myapp.js
Systemd doesn't know what to do with a JS file, so we need to call the correct interpreter first.
