How to run node.js script from systemd service - node.js

I have a node.js script in my directory on my raspberry pi /home/pi/my/app/here/app.js
When I navigate using cd my/app/here and run node app.js my script executes without any problems. However, when I try to execute this script from a service in systemd I get an code=exited, status=203/EXEC error.
Here is my service file:
[Unit]
Description=App
After=network.target
[Service]
Type=simple
User=pi
ExecStart=/user/bin/node /home/pi/my/app/here/app.js
WorkingDirectory=/home/pi/my/app/here
Restart=on-failure
[Install]
WantedBy=multi-user.target
This is really my first time using systemd and most of what I have found has been through research online and other SO posts. So I am sure I am missing something pretty silly, but any help on getting my service to run would be much appreciated.
What am I missing here?

Put a hash bang in the file
#!/usr/bin/env node
And just ensure the x bit is set.

Related

bottle error "critical error while processing request:" when launched from systemd

I have a server built on bottle that works great when launched from userland. The server appears on port 8088 and appears to be communicating to the outside world, but when I contact the app all I get is the very informative "Critical error while processing request:schema" which is the url of the app.
My systemd file is below:
[Unit]
Description=Survey Service
After=multi-user.target
Conflicts=getty#tty1.service
[Service]
User=ubuntu
Type=simple
Working-directory=/home/ubuntu/survey
ExecStart=/usr/bin/python3 /home/ubuntu/survey/server.py
[Install]
WantedBy=multi-user.target
I've found several articles related to the informative error message, but none related with systemd. As I said, the app runs perfectly when launched as user ubuntu in the project directory with the very simple command "python3 server.py" but seems to be missing... something when systemd tries to launch it.
Systemd reports the process is running and, as I said, I'm able to connect to the app... it just fails in an orderly fashion with this message, and I'm lost as to why. I suspect a permissions problem, but doesn't "user" and "Working-directory" take care of that? All files used by the app are in that directory or directories below it.
Apparently doing it the old fashion way works: set systemd to run a bash script as such:
cat /home/ubuntu/survey/server.sh
#!/bin/bash
cd /home/ubuntu/survey/
python3 server.py
Works just great. So my question now becomes one about systemd: what is the point of "Working-directory" if it does not actually set to that working directory?

What is the best way to run a Node.js script as service in Ubuntu?

I have a Node.js script that keeps my MongoDB database and the CRM database synced in real-time.
I want to run this script as a background task on my Ubuntu server. I found this solution, but it doesn't work for me. Is there another approach to reach this?
If you just want to start your application, you could use Forever or PM2 for running and auto-restarting on crash. However, this is not a background task.
For a background task that starts on server reboot, the post you linked is the right way to go. If it didn't work, maybe this article will help you. This is from official Express site: Process managers for Express apps
Basically, you create
[Unit]
Description="My Express App"
[Service]
ExecStart=/usr/bin/node server.js
WorkingDirectory=/project/absolute/path
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
Environment=NODE_ENV=production PORT=8080
[Install]
WantedBy=multi-user.target
Into a /etc/systemd/system/my-app.service file and then use systemctl to start it:
systemctl enable my-app.service
systemctl start my-app.service
Now this assumes your Linux distribution works with systemctl. If your Linux distribution works with upstart or something else, then you need to google up the instruction for that process manager.

Ubuntu Nodejs systemd service failure

I have the current latest node v8.6 installed on my Ubuntu 16.04 VPS.
The node app.js is in var/www/back-end.
Now I am trying to run it as a systemd service, but that fails:
Process: 8583 ExecStart=/usr/bin/nodejs /var/www/back-end/app.js (code=exited, status=1/FAILURE)
in other questions I read about the path to the node executable maybe being wrong, but I checked with 'which nodejs' and that gave back '/usr/bin/nodejs'. I also checked the permissions for var/www/back-end and they are set correctly to admin.
In another question I read that apt-get calls it nodejs because of a conflict and that a symlink should be created, however I was not convinced. I should note that I need the latest version of node because of it's features, so downgrading is not an option.
Any idea what could be wrong? Or is there some logfile in which I can find a better error so I know what is causing this?
This is my .service file:
[Unit]
Description=Node_API
After=mongodb.service
[Service]
ExecStart=/usr/bin/nodejs /var/www/back-end/app.js
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodeapi
User=admin
Group=admin
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production PORT=3000
WorkingDirectory=/var/www/back-end
[Install]
WantedBy=muti-user.target
You didn't mention testing that the command runs correctly when run from the CLI as the admin user:
cd /var/www/back-end && PATH=/usr/bin:/usr/local/bin NODE_ENV=production PORT=3000 /usr/bin/nodejs /var/www/back-end/app.js
You also didn't mention checking the permissions on the parent directions of /var/ and /www, just the script itself. These will also need to allow the admin user to have the execute permission, as explained more here.
Finally, your output indicates that the script did start but then exited, so you should have the ability to add diagnostics to your script to better understand why it's not starting. According to your configuration, you've sent the output of the script to syslog, you should look for the logs there. You can add a console.log() as soon as the script starts, and continue to add more to confirm at what step it's dying.
Also, you have a typo here: WantedBy=muti-user.target. That should be "multi-user.target".
You can also use systemd-analyze verify /path/to/your/file.service to check your file for syntax correctness.

My server run by systemd service but can't generate log file

I used systemd (ubuntu 16.0.4 LTS) to start MyServer at service (if reboot or crash ocurred, restart automatically by systemd).
It's work fine but it's never generate logs any more!
My server generate logs at file "MyServerLogs" and if this file not exist, create new one (in same running directory).
Also MyServer watch a directory (that create by itself in same running directory) for file creation. This does not work either!
When MyServer run by systemd service, It can not create file, write to file and watch directory for file creation any more. But why?.
I develop MyServer with c++ and beginner of systemd and unit files.
I put MyServer at:
/usr/bin/MyServer
and my unit file
[Unit]
Description=Virtual Distributed Ethernet
After=syslog.target
After=network-online.target
After=network.target
[Service]
ExecStart=/usr/bin/MyServer
Restart=always
StandardOutput=syslog
[Install]
WantedBy=multi-user.target
Thanks for any help.

systemd --user for normal graphical applications

I'm trying to set up systemd to start some programs when I log in. I'm doing this by putting files in e.g. ~/.config/systemd/user/some.service. This has worked for my emacs server service, which looks like this:
[Unit]
Description=Emacs: the extensible, self-documenting text editor
[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=always
[Install]
WantedBy=default.target
This starts a background process and allows me to connect to the emacs service with emacsclient
What's not working is my terminator service:
[Unit]
Description=The only terminal emulator that seems to be somewhat decent
After=multi-user.target
[Service]
ExecStart=/usr/bin/terminator
Restart=always
Environment=DISPLAY=:0
[Install]
WantedBy=default.target
With this code in ~/.config/systemd/user/terminator.service I can start terminator on the command-line with systemctl --user start terminator, but I can't get terminator to start automatically on (graphical) login. I've tried messing around with the WantedBy and After lines, switching between to graphical.target, default.target and multi-user.target, but it's really just guess work and hasn't helped.
Any ideas?
your terminator service file looks okay to me.
check below things
1. just check "default.target" file, is this below line present in that file
After=multi-user.target
2. if you write "WantedBy=default.target" then you need to create a soft link of terminator.service file inside "default.target.wants/" directory.
if this above two are correct, it should get start.
If still doesn't work, please remove "After=multi-user.target" this line, and change Wantedby as "WantedBy=graphical.target".
let me know the status of service after login
systemctl status terminator.service
Systemd is not the right tool for this task because it doesn't know when you have finished logging in in a desktop environment and it's possible to launch terminator. Instead, you could put a symlink to terminator's desktop file in your ~/.config/autostart :
ln -s /usr/share/applications/terminator.desktop ~/.config/autostart/
Most desktop interfaces (Gnome, KDE, Unity) will launch it when they start.

Resources