systemd cannot run service after running commands - node.js

I tried to run systemd using the commands systemctl enable photogrid.service & systemctl start photogrid.service in ubuntu 16
The nodejs app itself can run as expected. The service is to ensure that application will auto-start when application crash or server reboot.
The service apparently did not start. So I key in systemctl status photogrid.service to see what happened, the below is what I got from the terminal.
● photogrid.service - Photogrid
Loaded: loaded (/lib/systemd/system/photogrid.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Wed 2016-11-09 04:35:36 UTC; 7s ago
Process: 27523 ExecStart=/usr/local/bin/node /home/ubuntu/photogrid/app.js (code=exited, status=203/EXEC)
Main PID: 27523 (code=exited, status=203/EXEC)
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Main process exited, code=exited, status=203/EXEC
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Unit entered failed state.
Nov 09 04:35:36 ip-172-31-34-151 systemd[1]: photogrid.service: Failed with result 'exit-code'.
This the script that I wrote for the service under the path /lib/systemd/system/photogrid.service
[Unit]
Description=Photogrid
[Service]
Type=simple
Restart=always
RestartSec=10
Environment=NODE_ENV=production
ExecStart=/usr/local/bin/node /home/ubuntu/photogrid/app.js
[Install]
WantedBy=multi-user.target

Basically under ExecStart make sure you point to the correct nodejs executable. For my case it was in a different folder and not /usr/local/bin/node, to check where is your node executable. (Assuming you confirm you have downloaded and install it correctly in linux) use command which node to give you path direction.

Related

Linux / Raspberry Pi OS - Systemd not working with python3 script using OS module that accesses enviroment variable

I'm trying to use systemd to run a python3 script, it was working fine, however I changed my python script to use the built in OS module as I wanted to retrieve an enviroment variable from the system to use in the python script as a variable.
The python script is as follows:
#/usr/bin/python
import sys, requests, json, time, os
import paho.mqtt.client as mqtt
from requests.auth import HTTPBasicAuth
from datetime import datetime
MQTT_DEVICE_ID = os.environ['DEVICE_ID']+"/DEFENCE"
The DEVICE_ID enviroment variable comes from one I set in /etc/enviroment:
export DEVICE_ID="TEST1"
user#computer:~ $ echo $DEVICE_ID
TEST1
After adding this change to my python script the systemd service that runs this script no longer starts, it will work for a brief period then it will keep failing and thren working again and failing:
computer#computer:~ $ sudo systemctl status mqtt.service
● mqtt_defense.service - Arms the mqtt.py script that will alert if the device is moved
Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:57:37 BST; 1s ago
Process: 2442 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
Main PID: 2442 (code=exited, status=1/FAILURE)
CPU: 631ms
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Main process exited, code=exited, status=1/FAILURE
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Failed with result 'exit-code'.
user#computer:~ $ sudo systemctl status mqtt_defense.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:57:37 BST; 3s ago
Process: 2442 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
Main PID: 2442 (code=exited, status=1/FAILURE)
CPU: 631ms
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Main process exited, code=exited, status=1/FAILURE
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Failed with result 'exit-code'.
user#computer:~ $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt_defense.py script that will alert if the device is moved
Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2022-08-11 08:57:59 BST; 304ms ago
Main PID: 2475 (python3)
Tasks: 1 (limit: 3720)
CPU: 295ms
CGroup: /system.slice/mqtt.service
└─2475 python3 /scripts/mqtt.py
Aug 11 08:57:59 computer systemd[1]: Started Arms the mqtt.py script that will alert if the device is moved.
user#computer:~ $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:58:40 BST; 2s ago
Process: 2551 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
Main PID: 2551 (code=exited, status=1/FAILURE)
CPU: 633ms
After reading a few other questions on this issue such as this, I've tried altering my service to add the WorkingDirectory, User and Group as follows:
[Unit]
Description=Runs the mqtt.py script
After=multi-user.target
[Service]
WorkingDirectory=/scripts/
User=user
Group=user
Type=simple
ExecStart=python3 /scripts/mqtt.py
Restart=always
RestartSec=5
TimeoutSec=60
RuntimeMaxSec=infinity
[Install]
WantedBy=multi-user.target
I've also tried changing the User and Group to root, still with no success. If I don't use the OS module in my python script the systemd service runs perfectly.
I have a feeling this is a specific problem with the python OS module or that I'm trying to access etc/enviroment in my python script, but I'm not sure what would be the issue.
Any help would be appreciated, thanks.
This has nothing to do with Python. Your problem is that systemd doesn't expose the env vars defined in /etc/environment by default. See, for example, https://unix.stackexchange.com/questions/473001/env-vars-in-etc-environment-not-globally-visible. As explained in the answer to that question /etc/environment is only loaded by PAM (pluggable authentication module) managed sessions such as interactive logins. AFAIK, systemd doesn't use PAM.

pm2 not starting services automatically on reboot

pm2 is not resurrecting the services on reboot. I am using an EC2 instance with RHEL8.
This is how I have setup pm2:
1. pm2 start service1
2. pm2 startup
3. run the cmd returned by this
4. pm2 save
5. sudo reboot
Following are the logs on running this command systemctl -l status pm2-ec2-user:
● pm2-ec2-user.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-ec2-user.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Tue 2021-06-15 21:24:35 UTC; 662ms ago
Docs: https://pm2.keymetrics.io/
Process: 1528 ExecStart=/home/ec2-user/.nvm/versions/node/v12.13.0/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=203/EXEC)
Jun 15 21:24:35 ip-172-31-0-225.us-east-2.compute.internal systemd[1]: pm2-ec2-user.service: Control process exited, code=exited status=203
Jun 15 21:24:35 ip-172-31-0-225.us-east-2.compute.internal systemd[1]: pm2-ec2-user.service: Failed with result 'exit-code'.
Jun 15 21:24:35 ip-172-31-0-225.us-east-2.compute.internal systemd[1]: Failed to start PM2 process manager.
When I run the same command (/home/ec2-user/.nvm/versions/node/v12.13.0/lib/node_modules/pm2/bin/pm2 resurrect) manually after reboot, it works. But somehow this is not working automatically.

Bash script doesn't launch with systemd

I'm trying to launch a simple bash script called script-startup.sh when my raspberry Pi boots, using systemd. This script only contains some basic actions on files (copying, moving, executing binaries) and calls make as well to compile bits of codes.
I created a new file with path /etc/systemd/system/baymabx.service :
[Unit]
Description=Baymabx
After=pm2-pi
[Service]
ExecStart=/home/pi/Desktop/baymabx/script-startup.sh
Launching /home/pi/Desktop/baymabx/script-startup.sh in the terminal works as it is intended to be. Also, pm2-pi.service is pm2 starting my backend server, that I need before using my script.
However, after doing systemctl start baymabx and systemctl status baymabx, I get :
● baymabx.service - Baymabx
Loaded: loaded (/etc/systemd/system/baymabx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2020-12-11 01:34:06 CET; 3min 2s ago
Process: 359 ExecStart=/home/pi/Desktop/baymabx/script-startup.sh (code=exited, status=203/EXEC)
Main PID: 359 (code=exited, status=203/EXEC)
Dec 11 01:34:06 raspberrypi systemd[1]: Started Baymabx.
Dec 11 01:34:06 raspberrypi systemd[359]: baymabx.service: Failed to execute command: Exec format error
Dec 11 01:34:06 raspberrypi systemd[359]: baymabx.service: Failed at step EXEC spawning /home/pi/Desktop/baymabx/script-startup.sh: Exec format error
Dec 11 01:34:06 raspberrypi systemd[1]: baymabx.service: Main process exited, code=exited, status=203/EXEC
Dec 11 01:34:06 raspberrypi systemd[1]: baymabx.service: Failed with result 'exit-code'.
Anyone know how I can get it to work ?

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).

Systemd Service for jar file gets "operation timed out" error after few minues or stay in "activating mode"

the service unit is:
[Unit]
Description=test
After=syslog.target
After=network.target
[Service]
Type=forking
ExecStart=/bin/java -jar /home/ec2-user/test.jar
TimeoutSec=300
[Install]
WantedBy=multi-user.target
it starts fine for 1-4 minues. But later it fails:
tail /var/log/messages:
Feb 27 18:43:44 ip-172-31-40-48 systemd: Reloading.
Feb 27 18:44:06 ip-172-31-40-48 systemd: Starting test...
Feb 27 18:44:06 ip-172-31-40-48 java: 5.1.73
Feb 27 18:44:06 ip-172-31-40-48 java: Starting the internal [HTTP/1.1] server on port 8182
Feb 27 18:49:06 ip-172-31-40-48 systemd: test.service operation timed out.Terminating.
Feb 27 18:49:06 ip-172-31-40-48 systemd: test.service: control process exited, code=exited status=143
Feb 27 18:49:06 ip-172-31-40-48 systemd: Failed to start test.
Feb 27 18:49:06 ip-172-31-40-48 systemd: Unit test.service entered failed state.
systemctl status test.service (while restarting- stays in activating mode):
test.service - Setsnew
Loaded: loaded (/etc/systemd/system/test.service; enabled)
Active: activating (start) since Sun 2015-03-01 14:29:36 EST; 2min 30s ago
Control: 32462 (java)
CGroup: /system.slice/test.service
systemctl status test.service (after fail):
test.service - test
Loaded: loaded (/etc/systemd/system/test.service; enabled)
Active: failed (Result: exit-code) since Fri 2015-02-27 18:49:06 EST; 18min ago
Process: 27954 ExecStart=/bin/java -jar /home/ec2-user/test.jar (code=exited, status=143)
when running the jar in command line it works just fine.
tried changing the jar location because I thought it's a permissions problem
selinux is off
How can i fix this issue so I could start the jar on boot? there any alternatives? (RHEL7 do not include service command)
You made the service type forking, but this service does not fork. It just runs directly. Thus systemd waited five minutes for the program to daemonize itself, and it never did. The correct type for such a service is simple.
You also disabled SELinux, which is another problem you should resolve.

Resources