Linux service returns (code=exited, status=2) - linux

I have created a Golang web app and built as an executable. The executable is running well when executed as ./main (main is the name of the executable) from its subdirectory.
I'm trying to create it as a service. I have a file goservice.service
[Unit]
Description=goservice
[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/nshankar/go/eperssona/main
[Install]
WantedBy=multi-user.target
When I see the status of the service, I get following response -
Loaded: loaded (/etc/systemd/system/goservice.service; enabled; preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2022-10-28 10:47:54 UTC; 498ms ago
Process: 13286 ExecStart=/home/nshankar/go/eperssona/main (code=exited, status=2)
Main PID: 13286 (code=exited, status=2)
CPU: 4ms
The service is listening on port 9002.
Why am I getting this error?

Related

status=203/EXEC Error when creating code server

I've created code server service on Ubuntu 18.04 nginx.
But gives error when run command :
systemctl status code-server
Error :
Warning: The unit file, source configuration file or drop-ins of code-server.service changed on disk. Run 'systemctl daemon-reload' to reload units.
● code-server.service - code-server
Loaded: loaded (/lib/systemd/system/code-server.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2022-09-02 17:45:29 CEST; 4min 53s ago
Process: 4749 ExecStart=/usr/bin/code-server --bind-addr 127.0.0.1:8080 --user-data-dir /var/lib/code-server --auth password (code=exited, status=203/EXEC)
Main PID: 4749 (code=exited, status=203/EXEC)
My /lib/systemd/system/code-server.service file as below
[Unit]
Description=code-server
After=nginx.service
[Service]
Type=simple
Environment=PASSWORD=xxxxxx
ExecStart=/usr/bin/code-server --bind-addr 127.0.0.1:8080 --user-data-dir /var/lib/code-server --auth password
Restart=always
[Install]
WantedBy=multi-user.target
I checked all topics, they say it's a missing path problem but I checked all paths and exists
root#xx:~# sudo mkdir /usr/lib/code-server
mkdir: cannot create directory ‘/usr/lib/code-server’: File exists
root#xx:~# sudo mkdir /var/lib/code-server
mkdir: cannot create directory ‘/var/lib/code-server’: File exists
root#xx:~#
I'm not a pro ubuntu user. What should I check ?

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.

Start Airflow Webserver Automaticalll as a Service

I currently would like to write a system configuration file that starts the airflow webserver. Here is my config file definition:
#airflow-webserver.service
[Unit]
Description=Airflow webserver daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service
Wants=postgresql.service mysql.service redis.service rabbitmq-server.service
[Service]
#EnvironmentFile=/etc/default/airflow
Environment="PATH=/root/airflow_venv/bin"
User=airflow
Group=airflow
Type=simple
ExecStart=/root/airflow_venv/bin/airflow webserver -p 8080 --pid /root/airflow/airflow-webserver.pid
Restart=on-failure
RestartSec=5s
PrivateTmp=true
[Install]
WantedBy=multi-user.target
However running "sudo service airflow-webserver status" it return:
● airflow-webserver.service - Airflow webserver daemon
Loaded: loaded (/etc/systemd/system/airflow-webserver.service; enabled; vendor preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Mon 2021-01-25 06:58:14 UTC; 3s ago
Process: 5913 ExecStart=/root/airflow_venv/bin/airflow webserver -p 8080 --pid /root/airflow/airflow-webserver.pid (code=exited, status=217/USER)
Main PID: 5913 (code=exited, status=217/USER)
CGroup: /system.slice/airflow-webserver.service
I already looked at How to enable a virtualenv in a systemd service unit? as it sounds like a similar issue. However, I could not find my solution there. The OS I am operating on is Amazon Linux 2 AMI.
Can somebody help?
Lazloo
For anyone who gets the same Error:
Main PID: 5913 (code=exited, status=217/USER)
this indicates that the user is incorrect. After adapting the user and group, this service worked for me.

How do i install spark as a daemon

I start spark as master and slave on two machines with this guide:
https://www.tutorialkart.com/apache-spark/how-to-setup-an-apache-spark-cluster/
Then I make systemd .service for each of them but when I start them as a service they fail to start. Here is my systemctl status:
● sparkslave.service - Spark Slave
Loaded: loaded (/etc/systemd/system/sparkslave.service; enabled; ven
dor preset: enabled)
Active: inactive (dead) since Mon 2019-12-09 07:30:22 EST; 55s ago
Process: 31680 ExecStart=/usr/lib/spark/sbin/start-slave.sh spark://1
72.16.3.90:7077 (code=exited, status=0/SUCCESS)
Main PID: 31680 (code=exited, status=0/SUCCESS)
Dec 09 07:30:19 SparkSlave1 systemd[1]: Started Spark Slave.
Dec 09 07:30:19 SparkSlave1 start-slave.sh[31680]: starting org.apache.
spark.deploy.worker.Worker, logging to /usr/lib/spark/logs/spark-spark-
user-org.apache.spark.deploy.worker.Worker-1-SparkSlave1.out
And this is my sparkslave.service:
[Unit]
Description=Spark Slave
After=network.target
[Service]
User=spark-user
WorkingDirectory=/usr/lib/spark/sbin
ExecStart=/usr/lib/spark/sbin/start-slave.sh spark://172.16.3.90:7077
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
What is the problem?
Service type must change from simple to forking:
[Service]
Type=forking

Node.js, trying to add script as a service to centos gives error status=203/EXEC

I have a node.js server that I'd like to make a service so it wouldn't shut down after closing SSH-session.
I followed instructions in here, but it didn't go quite as planned because instead of the service starting up it gave me the following error:
Loaded: loaded (/etc/systemd/system/jasentiedot.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since ti 2018-01-23 10:38:08 EET; 59s ago
Process: 10525 ExecStart=/home/nodeuser/jasentiedot/server.js (code=exited, status=203/EXEC)
Main PID: 10525 (code=exited, status=203/EXEC)
Here is the service file at /etc/systemd/system/jasentiedot.service
[Unit]
Description=Servicename
[Service]
ExecStart=/home/nodeuser/jasentiedot/server.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/nodeuser/jasentiedot
[Install]
WantedBy=multi-user.target
The script runs fine when running it manually, so there shouldn't be anything wrong with that.
Is there any better methods to make node.js script persistent in case this doesn't work out? I tried forever, but that didn't work.
You have to specify node xyz.js in Execstart. See this post: Node.js script failed to start with systemctl

Resources