Running npm start from ExecStart in systemctl service file - node.js

I have the following service file:
[Unit]
Description=MyApp
After=syslog.target network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
WorkingDirectory=/opt/nodejs-sites/MyApp
ExecStart=/usr/bin/npm start
Environment=NODE_ENV=development
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
[Install]
WantedBy=multi-user.target
Here is the error from /var/log/syslog
Oct 14 13:00:55 devu18 systemd[1]: Started myapp.
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Changing to the requested working directory failed: No such file or directory
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Failed at step CHDIR spawning /usr/bin/npm: No such file or directory
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Main process exited, code=exited, status=200/CHDIR
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Failed with result 'exit-code'.
I for the life of me can't figure out why it's complaining of cannot find the file. npm start from the same working directory works just fine, no problems. Am I missing some permissions or +x or something somewhere?

This error might happen due to the fact that the executable was run without environment.
Here is a quick fix recipe
You can fix that by creating a Bash Script to do everything you need.
script.sh
#! /bin/bash
source ${HOME}/.bashrc
cd /absolute/path/to/my/project
export NODE_ENV=development
npm start
Now change it's mode to be executable
chmod +x script.sh
Now we can create our service (located for instance in /etc/systemd/system/my-project.service)
my-project.service
[Unit]
Description=My Project
After=syslog.target network.target
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/path/to/my/script.sh
User=root
[Install]
WantedBy=multi-user.target
And now let's run it and enable it
systemctl start my-project
systemctl enable my-project
Troubleshooting (Ubuntu 18.04 LTS)
This happened to me lately that for some reason systemctl couldn't effectively source .bashrc leaving me with an error npm error does not exists This is a workaround that helped me out.
Change the script.sh file's content into the following
#! /bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
cd /absolute/path/to/my/project
export NODE_ENV=development
npm start
Now your npm service should hopefully work.
This solution was tested on:
Ubuntu 20.04 LTS, with Node version 14.16.0 installed via NVM (Node Version Manager)
Ubuntu 18.04 LTS, with Node version 12.22.6 installed via NVM (Node Version Manager)
CentOS 7, with Node version 12.22.6 installed via NVM (Node Version Manager)

/bin/npm --prefix $PATH start
This worked for me

Related

ExecStart path to installed packages with python-poetry

What's the ExecStart path to the installed packages for a systemd unit file like that for python-poetry instead of PIP/venv on Ubuntu 20.04?
[Unit]
Description=Gunicorn Daemon for FastAPI Demo Application
After=network.target
[Service]
User=demo
Group=www-data
WorkingDirectory=/home/demo/fastapi_demo
ExecStart=/home/demo/fastapi_demo/venv/bin/gunicorn -c gunicorn_conf.py app:app
[Install]
WantedBy=multi-user.target
This line I need to replace I think:
ExecStart=/home/demo/fastapi_demo/venv/bin/gunicorn -c gunicorn_conf.py app:app

How can i get gunicorn flask app to run as a service?

I have this small flask application that is running on gunicorn and I am trying to run it as a systemd service. I have created a unit file in my local directory in ~/.config/my-user/user/gunicorn.service
[Unit]
Description=Gunicorn app
After=network.target
[Service]
User=my-user
Group=www-data
WorkingDirectory=/home/my-user/repo
ExecStart=/home/my-user/repo/venv/bin/gunicorn -b 0.0.0.0:8000 app:app
[Install]
WantedBy=multi-user.target
I get this error when trying to run sudo systemctl --user start gunicorn. I am running on amazon linux 1:
Failed to get D-Bus connection: Connection refused
what am i missing here?

Service file not running on Ubuntu but the project/file runs on Putty / terminal fine (ASP.NET Core website)

When I run an ASP.NET Core website from Putty in Ubuntu 20.04:
/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll
OR
dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll
It runs website fine on browser: localhost:5000 or https://localhost:5001
But when I try to start it as a service "website.service" using:
sudo nano /etc/systemd/system/website.service
And write file:
[Unit]
Description=Example .NET Web API App running on Ubuntu
[Service]
WorkingDirectory=/root/dev/myWebApp/myWebApp
ExecStart=/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
And then:
sudo systemctl enable website.service
sudo systemctl daemon-reload
sudo systemctl start website.service
The service starts but localhost:5000 or https://localhost:5001 does not open
and
sudo systemctl status website.service
Gives:
website.service - Example .NET Web API App running on Ubuntu
Loaded: loaded (/etc/systemd/system/website.service; enabled; vendor prese>
Active: activating (auto-restart) (Result: exit-code) since Sat 2021-08-21>
Process: 2421 ExecStart=/usr/bin/dotnet /root/dev/myWebApp/myWebApp/myWebAp>
Main PID: 2421 (code=exited, status=200/CHDIR)
Note:
Steps to recreate ASP.NET core environment and project:
Install dotnet core 5.0
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install apt-transport-https
sudo apt update
sudo apt install dotnet-sdk-5.0
Create, build and publish asp.net core project
mkdir dev
cd dev
dotnet --info
dotnet new webApp -o myWebApp //Create a basic web application that should listen on 5000 port
cd myWebApp
dotnet build //Builds the project
dotnet publish -c release -o myWebApp //Publishes the project as a myWebApp.dll on /root/dev/myWebApp/myWebApp/myWebApp.dll path, it should run in "website.service" file
I followed this tutorial: https://www.youtube.com/watch?v=M3G4bNRZTDo and partially from time: 5:44: https://www.youtube.com/watch?v=Sw6USmvt60s
I found the answer, the service file does not run dll from /root/dev folder, it runs file fine from /var/www folder.

Ubuntu upstart init script not working

I'm following an nginx tutorial from here and I created a myproject.conf file in /etc/init/ as stated in the tutorial. The code is as follows:
description "uWSGI server instance configured to serve myproject"
start on runlevel [2345]
stop on runlevel [!2345]
setuid ec2-user
setgid www-data
env PATH=/home/ec2-user/login_test/venv/bin
chdir /home/ec2-user/login_test/oauth_login
exec uwsgi --ini wsgi.ini
When I tried to activate this by giving sudo start myproject it returns:
start: Unknown job: myproject
How can I get this to work?

Gitlab CI - gitlab-runner run as root

I new on continous integration on iOS,
I try to run build with gitlab-runner and use shell as executor but I got issue that pod cannot run as root I am sure that I am not installing cocoapods with sudo and I try run whoami at before_script and that's right my runner run as root
any one got same issue ?and how to fix it ?
Register the runner without sudo, and that should set the gitlab-runner to run as your current user.
So steps should be:
sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
gitlab-runner register ...
gitlab-runner install
Remember to stop your sudo gitlab-runner service otherwise you could have multiple runners on the same machine fighting for the same jobs.
Here is documentation for how to use sudo and gitlab-runner user.
I am not sure, but I think it creates multiple runners.
On CentOS 8 I modified the gitlab-runner.service and changed the --user option to root.
Here is the default configuration:
/usr/bin/gitlab-runner run --working-directory /home/gitlab-runner --config /etc/gitlab-runner/config.toml --service gitlab-runner --user gitlab-runner
or
root#server# cat /etc/systemd/system/gitlab-runner.service
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/bin/gitlab-runner
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "gitlab-runner"
Restart=always
RestartSec=120
[Install]
WantedBy=multi-user.target
and I changed to this:
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/bin/gitlab-runner
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--user" "root"
User=root
Group=root
Restart=always
RestartSec=120
[Install]
WantedBy=multi-user.target
So this part --user gitlab-runner to --user root
NOTE
Absolutely I did not have security concerns, and did it for test, plase make sure you are considering security part.

Resources