Creating a Systemd service Error - linux

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

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

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.

systemd server isn't started

I am running this systemd command but when I screen -ls I don't see the screen.
The status is active and seems well.
But it isn't actually running when I check.
This is the .service file
[Unit]
Description=webhookdaemon
[Service]
ExecStart=/bin/bash path/to/script
RemainAfterExit=yes
Type=forking
Restart=on-failure
RestartSec=30
[Install]
WantedBy=multi-user.target
Here is the script (path/to/script)
screen -S docker-hub-daemon -d -m npm run start --prefix /root/nodeserver/
Here is the status output
webookdaemon.service - webhookdaemon
Loaded: loaded (/etc/systemd/system/webookdaemon.service; enabled; vendor preset: enabled)
Active: active (exited) since Tue 2018-03-13 19:55:15 UTC; 57min ago
Main PID: 2144 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/webookdaemon.service
Mar 13 19:58:29 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 19:59:03 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:00:22 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:01:21 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:02:26 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:04:41 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:47:41 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:49:53 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
Mar 13 20:52:53 aggregate-terminal-logs-tor1-01 systemd[1]: Started webhookdaemon.
root#aggregate-terminal-logs-tor1-01:~#
You should not be using screen to manage services. Just use systemd directly.
Make sure that Type= is set match the behavior of the service you are lauching. I could not find references to docker-hub-daemon, so I'm not sure the appropriate value for it. See man systemd.service for the documentation for Type=.
Instead of using screen -ls to check the status of the service, use systemctl status webookdaemon.
You may also wish to update the spelling of this service to be webhoookdaemon to match the spelling in the description.

Why process always exited when running systemd

why my service always exited when running systemd?
if change dir manualy to var/www/enterprise/src/ and execute /var/www/env/bin/celery -A catalog.tasks beat , proccess work perfectly
root#debian-django-nginx:/var/www/enterprise/src# cat /etc/systemd/system/celerybeat.service
[Unit]
Description=celery beat
[Service]
WorkingDirectory=/var/www/enterprise/src/
Type=simple
ExecStart=/var/www/env/bin/celery -A catalog.tasks beat &
KillMode=process
TimeoutSec=300
[Install]
WantedBy=multi-user.target
root#debian-django-nginx:/var/www/enterprise/src# systemctl daemon-reload
root#debian-django-nginx:/var/www/enterprise/src# systemctl start celerybeat
root#debian-django-nginx:/var/www/enterprise/src# systemctl status celerybeat
● celerybeat.service - celery beat
Loaded: loaded (/etc/systemd/system/celerybeat.service; enabled)
Active: active (exited) since Tue 2017-02-28 15:01:52 +10; 1 weeks 0 days ago
Mar 07 14:11:55 debian-django-nginx systemd[1]: celerybeat.service lacks ExecStart setting. Refusing.
Mar 07 14:28:08 debian-django-nginx systemd[1]: Started celery beat.
Mar 07 14:39:34 debian-django-nginx systemd[1]: Started celery beat.
Mar 07 15:05:56 debian-django-nginx systemd[1]: Started celery beat.
Mar 07 15:08:46 debian-django-nginx systemd[1]: Started celery beat.
Mar 07 15:19:35 debian-django-nginx systemd[1]: Started celery beat.
Mar 07 15:33:38 debian-django-nginx systemd[1]: Started celery beat.

xsp4 service SystemD Service exits with "Start request repeated too quickly."

I am trying to run xsp4 as a systemd service in order to host WebAPI 2 application on Ubuntu 16.04. This is my service file:
[Unit]
Description=myapp xsp4 WebAPI 2
Wants=network.target
[Service]
Type=simple
User=www-data
Group=www-data
EnvironmentFile=/etc/myapp/env
ExecStart=/usr/bin/xsp4 --appconfigdir /etc/xsp4/ --root /var/www/myapp.com/server --address 127.0.0.1
Restart=on-failure
[Install]
WantedBy=multi-user.target
The command in ExecStart works fine when ran in shell (as root). When I start via systemd I get:
root#ubuntu-512mb-ams3-01:/etc/systemd/system# sctl status myapp.service
● myapp.service - myapp xsp4 WebAPI 2
Loaded: loaded (/etc/systemd/system/myapp.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Oct 07 14:32:00 ubuntu-512mb-ams3-01 systemd[1]: myapp.service: Start request repeated too quickly.
Oct 07 14:32:00 ubuntu-512mb-ams3-01 systemd[1]: Failed to start myapp xsp4 WebAPI 2.
Oct 07 14:59:12 ubuntu-512mb-ams3-01 systemd[1]: Stopped myapp xsp4 WebAPI 2.
Oct 07 14:59:16 ubuntu-512mb-ams3-01 systemd[1]: Started myapp xsp4 WebAPI 2.
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: xsp4
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: Listening on address: 127.0.0.1
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: Root directory: /var/www/myapp.com/server
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: Listening on port: 9000 (non-secure)
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: Hit Return to stop the server.
Oct 07 14:59:19 ubuntu-512mb-ams3-01 xsp4[26490]: [0x7f24851e9700:] EXCEPTION handling: System.Threading.ThreadAbortException:
That System.Threading.ThreadAbortException does not occur when ran in shell. The actual error is "Start request repeated too quickly." because I configured mono to output all exceptions, even if caught - so this exception is not cause of exit.
Any advice ?
I will add the answer, because I myself was looking for.
[Service]
TimeoutStartSec=100
ExecStart=/usr/bin/xsp4 --appconfigdir /etc/xsp4/ --root /var/www/myapp.com/server --address 127.0.0.1 --nonstop

Resources