Create a configfile for the service in
/etc/systemd/system/node-hello.service
1 2 3 4 5 6 7 8 9 10 11 12 | [code language="bash"] [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[/code] |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | [code language="bash"] [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[1]: Started node-hello.service. Jul 08 13:30:30 brumbar node-hello[19160]: Server running at http://127.0.0.1:1337/ [/code] |
And for the logs, we use journalctl:
1 2 3 4 5 6 7 8 9 10 11 | [code language="bash"] [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[1]: Started node-hello.service. Jul 08 13:36:40 brumbar systemd[1]: Stopping node-hello.service... Jul 08 13:36:40 brumbar systemd[1]: node-hello.service: main process exited, code=exited, status=143/n/a Jul 08 13:36:40 brumbar systemd[1]: Stopped node-hello.service. Jul 08 13:36:40 brumbar systemd[1]: Unit node-hello.service entered failed state. Jul 08 13:36:43 brumbar systemd[1]: Starting node-hello.service... Jul 08 13:36:43 brumbar systemd[1]: Started node-hello.service. [/code] |
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)