Problem with a Golang webapp and system service - linux

I am trying to create a Golang server using the gin framework on ubuntu. It works fine when it is executed in the terminal after building it with go build and equally works well locally.
Systemd
Description=goapp
[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/home/.../goapp/main
[Install]
WantedBy=multi-user.target
I got this error
goapp.service - rediate
Loaded: loaded (/lib/systemd/system/goapp.service; disabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2022-09-29 08:14:10 UTC; 66ms ago
Process: 21628
ExecStart=/home/.../go/goapp/main (code=exited, status=2)
Main PID: 21628 (code=exited, status=2)
CPU: 9ms

Go is compiled language. You need to build your code into an executable binary file using go build command and then give path to binary file to systemd via ExecStart property in unit file.
See Go Documentation and specifically Compile and install the application section to find out more about how to compile your application.
In your example you have ExecStart=/home/.../goapp/main.go which is telling systemd to run source code file. That file is not executable and understood by operating system so it fails to execute and systemd unit fails because of that.

Adding a working directory to the systemd fix this error.
Description=goapp
[Service]
Type=simple
Restart=always
RestartSec=
WorkingDirectory=/home/.../goapp
ExecStart=/home/.../goapp/main
[Install]
WantedBy=multi-user.target

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 ?

systemd doesn't run an application from bash script

I have a service that should run set of applications in background on my Yocto embedded Linux system. I don't like an idea to create a systemd startup script for each app so I just run them from a bash script as following:
The service:
startup.service
[Unit]
Description=applications startup script
After=network.target
[Service]
Type=simple
ExecStart=/opt/somedir/startup.sh
[Install]
WantedBy=multi-user.target
and the script
startup.sh
#!/bin/bash
echo "application startup script"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/somedir
/opt/somedir/app1 &
/opt/somedir/app2 &
/opt/somedir/app3 &
But no application started. Checking the service status give me:
systemctl status startup
● startup.service - applications startup script
Loaded: loaded (/lib/systemd/system/startup.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2021-03-25 10:33:16 UTC; 18min ago
Process: 428 ExecStart=/opt/somedir/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 428 (code=exited, status=0/SUCCESS)
Mar 25 10:33:16 systemd[1]: Started application startup script.
Mar 25 10:33:16 startup.sh[428]: application startup script
Mar 25 10:33:16 systemd[1]: startup.service: Succeeded.
So the service executed on the system startup and executes the script. If I execute the script from the command line it starts the applications as expected. So what a reason that no application run?
Systemd will need to know how to run the script. Therefore either add:
#!/bin/bash
to the top line of the startup.sh script or change the ExecStart line in the systemd service file to:
ExecStart=/bin/bash -c /opt/somedir/startup.sh
Also, to ensure that the processes spawned remain persistent after being spawned, change:
Type=forking
systemd runs script startup.sh, and after that process ends, it assumes all is done so it kills off any remaining processes and the unit ends. The simplest solution is to add a wait at the end of startup.sh so that it only returns when the backgrounded processes have all ended.

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.

systemd service doesn't start silently. How to debug?

I wrote a program (called whisky) which I now want to startup when booting the machine (a Raspberry Pi with which I'm creating an autonomous boat). So I created the file /lib/systemd/system/whisky.service:
[Unit]
Description=Whisky Boat Program
After=network.target
StartLimitIntervalSec=0
[Service]
ExecStart=/home/pi/whisky/run
KillMode=process
IgnoreSIGPIPE=true
Restart=always
RestartSec=3
User=root
Type=simple
[Install]
WantedBy=multi-user.target
I verified the file is correctly formatted for systemd using systemd-analyze verify whisky.service.
When I now run sudo systemctl start whisky I get no output (suggesting no errors).
sudo systemctl status whisky gives me the following output though:
* whisky.service - Whisky Boat Program
Loaded: loaded (/lib/systemd/system/whisky.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2020-03-20 15:03:35 CET; 792ms ago
Process: 8621 ExecStart=/home/pi/whisky/run (code=exited, status=203/EXEC)
Main PID: 8621 (code=exited, status=203/EXEC)
Mar 20 15:03:35 raspberrypi systemd[1]: whisky.service: Unit entered failed state.
Mar 20 15:03:35 raspberrypi systemd[1]: whisky.service: Failed with result 'exit-code'.
The file /home/pi/whisky/run is actually a bash script which in turn starts the program. To check whether systemd even starts that bash script I added a first line to it: mkdir /home/pi/RUNNING_FROM_SYSTEMD. The dir RUNNING_FROM_SYSTEMD is not created though, so it seems systemd doesn't even try to run the file /home/pi/whisky/run.
Does anybody know what I'm doing wrong here?
Check your run script. There should be shebang. If not, add #!/bin/bash at the top of your script and give absolute path like ExecStart=/bin/bash /home/pi/whisky/run
The error message (code=exited, status=203/EXEC) is often seen when the script itself or its interpreter cannot be executed.
Other possible reasons maybe:
wrong path to script
script not executable
no shebang (first line) or wrong path in shebang
internal files in your script might be missing access permissions.

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