Node and systemd, bff!
Since Centos7 was released in July, a “nondesktop” distro(other than rhel7 ofc) is using systemd.
Like it, hate it or discuss it, systemd is here to stay.
As we have been discussing internally how to manage startup of nodejs apps, using monit/pm2/others has been alternatives, and now, imho, systemd is sailing up as the better option. And the main reason is KISS. systemd will already handle the rest of your stack, so why add more.
So how to use it? This is the simplest way.
Create a configfile for the service in
[Service] ExecStart=/bin/node /home/stein/node/hello/hello.js Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=node-hello User=stein Group=stein Environment=NODE_ENV=production [Install] WantedBy=multi-user.target
So “ExecStart” is obviously first the node binary, then your app.
“Restart” makes sure it starts again if it dies.
Then systemctl is your friend for controlling it:
[stein@brumbar ~]$ sudo systemctl start node-hello [stein@brumbar ~]$ systemctl status node-hello node-hello.service Loaded: loaded (/etc/systemd/system/node-hello.service; disabled) Active: active (running) since Tue 2014-07-08 13:30:30 CEST; 13s ago Main PID: 19160 (node) CGroup: /system.slice/node-hello.service └─19160 /bin/node /home/stein/node/hello/hello.js Jul 08 13:30:30 brumbar systemd: Started node-hello.service. Jul 08 13:30:30 brumbar node-hello: Server running at http://127.0.0.1:1337/
And for the logs, we use journalctl:
[stein@brumbar ~]$ journalctl -u node-hello -- Logs begin at Fri 2014-03-28 17:42:12 CET, end at Tue 2014-07-08 13:39:07 CEST. -- Jul 08 13:36:13 brumbar systemd: Started node-hello.service. Jul 08 13:36:40 brumbar systemd: Stopping node-hello.service... Jul 08 13:36:40 brumbar systemd: node-hello.service: main process exited, code=exited, status=143/n/a Jul 08 13:36:40 brumbar systemd: Stopped node-hello.service. Jul 08 13:36:40 brumbar systemd: Unit node-hello.service entered failed state. Jul 08 13:36:43 brumbar systemd: Starting node-hello.service... Jul 08 13:36:43 brumbar systemd: Started node-hello.service.
Now ofcourse there is a billion things you can make systemd do, like cute things like have it listen on a port and stop and start your nodeapp as it is being used, but thats another story(post)