Well, I have a service configured with systemctl. This is the config file:
[Unit]
Description=The description of the service (:
After=network.target
[Service]
ExecStartPre=/bin/echo 'Starting Service' >> /var/log/node/myapp.log
ExecStart=/root/.nvm/versions/node/v5.0.0/bin/node ./bin/www
ExecStopPost=/bin/echo 'Stopping Service' >> /var/log/node/myapp.log
Restart=always
#StandardOutput=/var/log/node/myapp.log
#StandardError=/var/log/node/myapp.log
SyslogIdentifier=myapp
Environment=NODE_ENV=production
WorkingDirectory=/home/user/node-apps/theapp
[Install]
WantedBy=multi-user.target
What I need?:
1) The ExecStartPre
and ExecStopPost
could write the message 'starting service'
or 'stopping service'
to the file /var/log/node/myapp.log
. With the above configuration, doesn't work, it only outputs 'Starting Service' >> /var/log/node/myapp.log
and 'Stopping Service' >> /var/log/node/myapp.log
to journalctl
. (I checked with journalctl -u myapp
)
2) I need that instead of the all logs of the app, outputs to the journalctl
, could output to a file. Ex: /var/log/node/myapp.log
. I mean, if in my app, I have a console.log()
, this could be there.
With upstart, I can do it in this way:
script
exec start-stop-daemon --start --make-pidfile --pidfile /var/run/upstart-yourapp.pid --chdir /var/www/yourapp/--chuid user:usergroup --exec /usr/bin/node index.js >> /var/log/yourapp.upstart.log 2>&1
end script
pre-start script
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/yourapp.upstart.log
end script
post-stop script
rm /var/run/upstart-ghost.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/yourapp.upstart.log
end script
But, it is possible to do it with systemctl?