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

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"

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

/usr/bin/env 'node' Permission Denied

I installed Node V10.19.0 and tileserver-gl
I created a bash file in my home directory, by the name tileserver.sh
Which contain this code:
xvfb-run -a -s "-screen 0 1024x768x24" tilerserver-gl NewZeland.mbtiles
When I executed it works fine, and listening on port 8080
and I created service of this file in /etc/systemd/system/tileserver.service and enabled
sudo systemctl status tileserver.service
It shows this error:
Loaded: loaded (/etc/systemd/system/tileserver.service: enabled; vendor preset: enabled)
Active: Failed (Result: exit-code) since .....
process: 3729 ExecStart=/home/tilesServer/tileserver.sh ( code=exited, status=126)
Main PID: 3729 ( code=exited, status=126)
mar 26 18:21:57 tileserver systemd started tileserver bash script runing
mar 26 18:21:57 tilerserver tileserver.sh[3729] : /usr/bin/env "node' Permission denied
mar 26 18:21:57 tileserver systemd[1]: tileserver.service: main process exited , code=exited, status=126/n/a
mar 26 18:21:57 tileserver systemd[1]: tileserver.service: Failed with result 'exit-code'
How to resolve this issue and error
I recommended you use NVM and install It as normal user, after your installed NVM you should install NodeJS with nvm install stable to get node stable version and then you will can test.
First I add the nvm path to bachrc
start to debug it. With root
node debug tileserver-gl
It work fine , so I modified the script and working fine

NODE APP: Systemd startup script not working?

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.

Unable to run node app using systemd

This going to be my first node deployment. I have this app which runs fine on my Debian 9 server using this command:
cd /srv/myapp && NODE_ENV=production yarn start
And print out this message:
yarn run v1.6.0
$ babel-node index.js
In order to demonize the app using systemd, I created `/lib/systemd/system/myapp.service:
[Unit]
Description=Myapp
[Service]
ExecStart=/home/john/start.sh
Type=simple
User=john
Restart=on-failure
[Install]
WantedBy=multi-user.target
and in start.sh I have:
cd /srv/myapp && NODE_ENV=production yarn start
Hoewever, when I run systemctl start myapp the node app does not start to listen on port 3000, as expected (netstat -tulpn | grep :3000
returns no results)
# systemctl status myapp
● myapp.service - Myapp
Loaded: loaded (/lib/systemd/system/myapp.service; disabled; vendor preset: enabled
Active: failed (Result: exit-code) since Sun 2018-05-13 06:14:04 EDT; 5s ago
Process: 8852 ExecStart=/home/bob/start.sh (code=exited, status=203/EXEC)
Main PID: 8852 (code=exited, status=203/EXEC)
May 13 06:14:04 9606 systemd[1]: myapp.service: Unit entered failed state.
May 13 06:14:04 9606 systemd[1]: myapp.service: Failed with result 'exit-code'.
May 13 06:14:04 9606 systemd[1]: myapp.service: Service hold-off time over, scheduling
May 13 06:14:04 9606 systemd[1]: Stopped myapp.
May 13 06:14:04 9606 systemd[1]: myapp.service: Start request repeated too quickly.
May 13 06:14:04 9606 systemd[1]: Failed to start myapp.
May 13 06:14:04 9606 systemd[1]: myapp.service: Unit entered failed state.
May 13 06:14:04 9606 systemd[1]: myapp.service: Failed with result 'exit-code'.
I have other variations on myapp.service but could not manage to run node.
What could be wrong here? How can I fix it?
Process: 8852 ExecStart=/home/bob/start.sh (code=exited, status=203/EXEC)
According to systemd.exec(5), this means systemd could not execute the specified file:
203 EXIT_EXEC The actual process execution failed (specifically, the execve(2) system call). Most likely this is caused by a missing or non-accessible executable file.
You should check if /home/bob/start.sh is executable and has correct shebang specified (that is, the first line of your script must be #!/bin/bash).

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