NODE APP: Systemd startup script not working? - node.js

Trying to create a start up script for my nodejs app which runs on port 3000.
Issue: The node-app.server script is not working and I think it's because ExecStart pathway is wrong. When I go to the server IP in Chrome nothing shows.
The node app was created with npm generator, and I normally use npm start to start the app. I've added path to bin/wwww, here:
[Unit]
Description=tweetMonster twtiter server - making your environment variables rad
Documentation=https://example.com
After=network.target
[Service]
Environment=NODE_PORT=3000
Type=simple
User=ubuntu
ExecStart=/home/ubuntu/twitter-server/bin/www.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
My app is running on Ubuntu 18 at /home/ubuntu/twitter-server . And if do ls:
/twitter-server$ ls
app.js node_modules package.json routes
bin package-lock.json public views
Please help!
ERROR TERMINAL:
Nov 01 05:40:25 ip-172-31-22-207 systemd[1]: node-app.service: Main process exited, code=exited, status=203/EXEC
Nov 01 05:40:25 ip-172-31-22-207 systemd[1]: node-app.service: Failed with result 'exit-code'.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Service hold-off time over, scheduling restart.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Scheduled restart job, restart counter is at 5.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: Stopped hello_env.js - making your environment variables rad.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Start request repeated too quickly.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: node-app.service: Failed with result 'exit-code'.
Nov 01 05:40:26 ip-172-31-22-207 systemd[1]: Failed to start hello_env.js - making your environment variables rad.
Nov 01 06:20:11 ip-172-31-22-207 systemd[1]: /etc/systemd/system/node-app.service:10: Executable path is not absolute: "node /home/ubuntu/twitter-server/bin/www.js"
Nov 01 06:24:35 ip-172-31-22-207 systemd[1]: /etc/systemd/system/node-app.service:10: Executable path is not absolute: "node /home/ubuntu/twitter-server/bin/www.js"
root#ip-172-31-22-207:/etc/systemd/system#

There are 2 issues with your service config.
First, wrap the value of Environment with double quotes:
Environment="NODE_PORT=3000"
Second,
You need to use node to run the ExecStart script. /home/ubuntu/twitter-server/bin/www.js is no command in itself.
Do,
ExecStart=/bin/bash -c '$$(which node) /home/ubuntu/twitter-server/bin/www.js'

I recommend this package (service-systemd) for a simple program
Sometimes you just want an "old style" daemon for simple services.
Sometimes you have to deploy in small devices (like a RaspberryPi) and you can't use Docker and all the band.

Related

Control python script like linux cli applicaitons

I have a python script which I want to control using linux commands.
e.g. like we control mysql: service mysql restart
How to achieve this kind of functionality.
I saw some bash scripts doing this but don't have any knowledge in bash scripting.
Thanks.
UPDATE:
I have a site_monitor.py script which I included in site_monitor.service in /etc/systemd/system/.
[Unit]
Description=Site Monitor Service
After=multi-user.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 /home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py
[Install]
WantedBy=multi-user.target
After doing systemctl daemon-reload.service, I started the script using systemctl start site_monitor.service
Listing all the services using systemctl list-units --type=service, I can see the service running, but it's not doing anything.
If I run my python script in terminal using python3 site_monitor.py, then it works.
I found just now if I start the service and check the status using sudo service site_monitor status,
I checked after starting the service, it was fine , checking after sometime again gave me this error:
hemantsah#pop-os:/etc/systemd/system$ sudo service site_monitor status
● site_monitor.service - Site Monitor Service
Loaded: loaded (/etc/systemd/system/site_monitor.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2021-11-18 10:47:30 IST; 16s ago
Process: 111989 ExecStart=/usr/bin/python3 /home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py (code=exited, status=1/FAILURE)
Main PID: 111989 (code=exited, status=1/FAILURE)
Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Scheduled restart job, restart counter is at 5.
Nov 18 10:47:30 pop-os systemd[1]: Stopped Site Monitor Service.
Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Start request repeated too quickly.
Nov 18 10:47:30 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
Nov 18 10:47:30 pop-os systemd[1]: Failed to start Site Monitor Service.
Nov 18 10:47:34 pop-os systemd[1]: site_monitor.service: Start request repeated too quickly.
Nov 18 10:47:34 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
Nov 18 10:47:34 pop-os systemd[1]: Failed to start Site Monitor Service.
Running sudo journalctl -u site_monitor.service gave me following error:
Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Scheduled restart job, restart counter is at 4.
Nov 18 10:10:37 pop-os systemd[1]: Stopped Site Monitor Service.
Nov 18 10:10:37 pop-os systemd[1]: Started Site Monitor Service.
Nov 18 10:10:37 pop-os python3[111023]: Traceback (most recent call last):
Nov 18 10:10:37 pop-os python3[111023]: File "/home/hemantsah/WisdomLeaf/site_monitor/site_monitor.py", line 3, in <module>
Nov 18 10:10:37 pop-os python3[111023]: from bs4 import BeautifulSoup
Nov 18 10:10:37 pop-os python3[111023]: ModuleNotFoundError: No module named 'bs4'
Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Main process exited, code=exited, status=1/FAILURE
Nov 18 10:10:37 pop-os systemd[1]: site_monitor.service: Failed with result 'exit-code'.
I guess you already have systemd in your machine, but in case you don't, you can install it via package manager, e.g. apt:
sudo apt-get install systemd
You can then, create your own systemd service. To do so, just create a new file in /etc/systemd/systemd/, something like /etc/systemd/systemd/your_service_name.service. That file should look like this:
[Unit]
Description= My service
After=multi-user.target
[Service]
Type=simple
WorkingDirectory=/path/to/your/working/dir/
User=<user>
Restart=always
ExecStart=/usr/bin/python3 /path/to/your/script/<script_name>.py
[Install]
WantedBy=multi-user.target
Of course you can use different python 3 binaries instead of /usr/bin/python3. Also, the service configuration itself can be different, the example above is just a basic service structure.
After creating this file (with root permissions), you should reload the daemon with:
sudo systemctl daemon-reload
And if you want to keep your script enabled even if the server/machine restarts, run:
sudo systemctl enable your_service_name.service
Finally, you can start your service using the following:
sudo systemctl start your_service_name.service

npm command not found when running node app from bash script (linux EC2)

I have a node app on an linux AMI EC2 instance, and I want to run it with systemd. I've written a bash script with the npm start command, but when I run it with systemd the logs tell me that the npm command cannot be found:
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal systemd[1]: Started NodeApp.
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal systemd[1]: Starting NodeApp...
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal start.sh[9609]: /home/ec2-user/source/NodeApp/start.sh: line 8: npm: command not found
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal systemd[1]: NodeApp.service: main process exited, code=exited, status=127/n/a
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal systemd[1]: Unit NodeApp.service entered failed state.
Nov 03 21:22:45 ip-10-85-147-43.ec2.internal systemd[1]: NodeApp.service failed.
When I run npm -v it shows 7.4.0.
here's my bash script:
#! /bin/bash
cd home/ec2-user/source/NodeApp/
npm start
And the contents of NodeApp.service
[Unit]
Description= Node app
[Service]
ExecStart=/home/ec2-user/source/NodeApp/start.sh
[Install]
WantedBy=multi-user.target
Hi its easy to run node app using a process management tool called PM2, you can install PM2 directly to the Ec2 using terminal console and run the www file which is located in the soucefolder/bin
Install PM2 : npm install pm2 -g or pm2 completion install
RUN PM2 : cd /source_folder/bin and locate www file
pm2 start www --name="Application Name"

How to make my app run in background in Ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I created a service in my Ubuntu instance by adding it to systemd like:
sudo systemctl enable myservice.service
Created symlink /etc/systemd/system/multi-user.target.wants/myservice.service → /etc/systemd/system/myservice.service.
the setting inside my myservice.service are:
[Unit]
Description=myserviceService
[Service]
Restart=always
Type=simple
ExecStart=/home/myservice-app/core/core
[Install]
WantedBy=multi-user.target
This is the error when I check status:
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Main process exited, code=exited, status=1/FAILURE
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Failed with result 'exit-code'.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Scheduled restart job, restart counter is at 5.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: Stopped myserviceService.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Start request repeated too quickly.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: myservice.service: Failed with result 'exit-code'.
Feb 02 06:51:16 myservice-ubuntu-app systemd[1]: Failed to start myservice Service.
What am I doing wrong? The service runs properly in foreground with no errors, its just the service that struggles to run.
If your Go program is able to start normally (meaning not as a service, but manually from command line), then check if this is a policy issue.
For instance, a SELinux policy could prevent your Go binary to start if it is not installed in a system path (like /usr/local/bin).
Or the service definition uses relative instead of absolute paths (same here).
Or with the wrong user.

systemd not starting service from ExecStart

I have a service file that refuses to actually start the service specified in the ExecStart directive.
If I run exactly the same command from the terminal prompt, everything works as expected.
I have checked all permissions on files etc. but nothing I do will launch the program.
Here is my service file:
[Unit]
Description=Start pjsip Pjsua client in background
After=network.target
[Service]
Type=simple
RestartSec=3
ExecStart=/usr/bin/screen -dmS Pjsua /usr/local/sbin/Pjsua --config-file /usr/local/sbin/pjsua.cfg
[Install]
WantedBy=multi-user.target
and here is the output from the log file:
May 06 20:27:07 vring systemd[1]: pjsip.service: Trying to enqueue job pjsip.service/start/replace
May 06 20:27:07 vring systemd[1]: pjsip.service: Installed new job pjsip.service/start as 2553
May 06 20:27:07 vring systemd[1]: pjsip.service: Enqueued job pjsip.service/start as 2553
May 06 20:27:07 vring systemd[1]: pjsip.service: About to execute: /usr/bin/screen -dmS Pjsua /usr/local/sbin/Pjsua --confif-file /usr/local/sbin/pjsua.cfg
May 06 20:27:07 vring systemd[1]: pjsip.service: Forked /usr/bin/screen as 11355
May 06 20:27:07 vring systemd[1]: pjsip.service: Changed dead -> running
May 06 20:27:07 vring systemd[1]: pjsip.service: Job pjsip.service/start finished, result=done
May 06 20:27:07 vring systemd[1]: Started Start pjsip Pjsua client in background.
May 06 20:27:07 vring systemd[1]: pjsip.service: Child 11355 belongs to pjsip.service
May 06 20:27:07 vring systemd[1]: pjsip.service: Main process exited, code=exited, status=0/SUCCESS
May 06 20:27:07 vring systemd[1]: pjsip.service: Changed running -> stop-sigterm
May 06 20:27:07 vring systemd[1]: pjsip.service: Child 11356 belongs to pjsip.service
May 06 20:27:07 vring systemd[1]: pjsip.service: cgroup is empty
May 06 20:27:07 vring systemd[1]: pjsip.service: Changed stop-sigterm -> dead
May 06 20:27:07 vring systemd[1]: pjsip.service: Collecting.
From the above it appears as though the process has exited and I cannot understand why as it has to be commanded to do so.
If I run the command specified for ExecStart from the command line it all works perfectly. The program stays alive until I actually command it to shut down.
This problem has been driving me nuts for most of a day and I am no closer to a resolution.
Any pointers as to what I may be doing wrong would be very much appreciated.
I have solved the problem!!!
Because the system forks off the screen command I needed to change the service type to 'forking'.
It all works perfectly now.
Often it's the stupid things that catch you out!

Creating a Systemd service Error

I've used golang to create a small program that runs silently in the background, and can run this program by typing ./my_app
It is located in $HOME/my_app/my_app
I went ahead and wrote a systemd my_app.service file:
[Unit]
Description="My Test Service"
[Service]
Type=simple
ExecStart= ~/my_app/my_app
[Install]
WantedBy=multi-user.target
However, when i run sudo systemctl start my_app.service, it errors out and returns
Failed to start my_app.service: Unit my_app.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status my_app.service' for details.
systemctl status my_app.service returns
Apr 07 10:15:47 DEV-Rohan systemd[1]: Starting My Test App...
Apr 07 10:15:47 DEV-Rohan systemd[1]: Started My Test App.
Apr 07 10:36:34 DEV-Rohan systemd[1]: [/etc/systemd/system/my_app.service:6] Executable path is not abs
Apr 07 10:36:34 DEV-Rohan systemd[1]: my_app.service: Service lacks both ExecStart= and ExecStop= setti
Apr 07 11:00:39 DEV-Rohan systemd[1]: [/etc/systemd/system/my_app.service:6] Executable path is not abs
Apr 07 11:00:39 DEV-Rohan systemd[1]: my_app.service: Service lacks both ExecStart= and ExecStop= setti

Resources