Run python -m in Ubuntu systemd Service - python

there:
I need to run a python module as systemd service.
the command is cd /home/bitnami/abc/ && python -m abc.abc.
at /etc/systemd/system, abc.service is like this:
[Unit]<br>
Description=xxxx<br>
After=syslog.target<br>
After=network.target[Service]<br>
User=ubuntu<br>
Type=simple<br>
[Service]<br>
Type=simple<br>
ExecStart=/usr/bin/python -m abc.abc<br>
WorkingDirectory=/home/abc/abc<br>
Restart=always<br>
StandardOutput=syslog<br>
StandardError=syslog<br>
SyslogIdentifier=helloworld<br>
[Install]<br>
WantedBy=multi-user.target<br>
<br>
when I check status
I got this
<br>
● abc.service - abc Push Notification<br>
Loaded: loaded (/etc/systemd/system/abc.service; disabled; vendor preset: enabled)<br>
Active: failed (Result: start-limit-hit) since Wed 2019-10-09 00:59:36 UTC; 4s ago<br>
Process: 2895 ExecStart=/usr/bin/python -m abc.abc (code=exited, status=1/FAILURE)<br>
Main PID: 2895 (code=exited, status=1/FAILURE)<br>
<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Main process exited, code=exited, <br>status=1/FAILURE<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Unit entered failed state.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Failed with result 'exit-code'.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Service hold-off time over, scheduling restart.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: Stopped abc Push Notification.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Start request repeated too quickly.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: Failed to start abc Push Notification.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Unit entered failed state.<br>
Oct 09 00:59:36 ip-172-26-4-222 systemd[1]: abc.service: Failed with result 'start-limit-hit'.<br>
I also try to change ExecStart to
ExecStart=/bin/sh -c 'cd /home/bitnami/abc/ | python -m abc.abc',<br>
it also failed.
Could anyone give me an idea?
Thanks

Related

Python Nginx Restart and Build

I’m building a web api with Flask. I got error when I use “sudo systemctl restart nginx” for ngnix restart after server installations, wsgi installations
$ sudo systemctl status nginx.servic
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-12-06 09:36:31 UTC; 23h ago
Docs: man:nginx(8)
Process: 3210037 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Dec 06 09:36:31 systemd[1]: Starting A high performance web server and a reverse proxy server…
Dec 06 09:36:31 nginx[3210037]: nginx: [emerg] unknown directive “brotli” in /etc/nginx/conf.d/brotli.conf:1
Dec 06 09:36:31 nginx[3210037]: nginx: configuration file /etc/nginx/nginx.conf test failed
Dec 06 09:36:31 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Dec 06 09:36:31 systemd[1]: nginx.service: Failed with result ‘exit-code’.
Dec 06 09:36:31 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
How can I solve this?
I checked sites-enabled and sites-available?

How can i resolve the start limit hit error

Hi I recently bought a Raspberry Pi 4
I am trying to program a discord bot for my server
Here is the error
pi#raspberrypi:~ $ sudo nano /lib/systemd/system/discordbot.service
pi#raspberrypi:~ $ sudo systemctl status discordbot.service
● discordbot.service - My Discord Bot Service
Loaded: loaded (/lib/systemd/system/discordbot.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Wed 2020-06-03 22:28:41 +08; 20min ago
Main PID: 856 (code=exited, status=0/SUCCESS)
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Service RestartSec=100ms expired, scheduling restart.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Scheduled restart job, restart counter is at 5.
Jun 03 22:28:41 raspberrypi systemd[1]: Stopped My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Start request repeated too quickly.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Failed with result 'start-limit-hit'.
Jun 03 22:28:41 raspberrypi systemd[1]: Failed to start My Discord Bot Service.
Here is the log to see my error
pi#raspberrypi:~ $ journalctl -e -u discordbot
Jun 03 22:28:41 raspberrypi systemd[1]: Started My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Succeeded.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Service RestartSec=1
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Scheduled restart jo
Jun 03 22:28:41 raspberrypi systemd[1]: Stopped My Discord Bot Service.
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Start request repeat
Jun 03 22:28:41 raspberrypi systemd[1]: discordbot.service: Failed with result '
Jun 03 22:28:41 raspberrypi systemd[1]: Failed to start My Discord Bot Service.
[2]+ Stopped journalctl -e -u discordbot
Here is the code for the systemd
[Unit]
Description=My Discord Bot Service
After=network-online.target
[Service]
Type=idle
Restart=always
User=pi
ExecStart=/usr/bin/python3 /home/pi/discordbot/discordbot.py
[Install]
WantedBy=multi-user.target
I have tried to find solutions for hours but none helped
All help is appreciated :)

Systemd service not recognizing python library

I'm trying to run a python discord bot when my Raspberry Pi starts up. To do this, I've used a systemd service:
[Unit]
Description=Testing
[Service]
Type=idle
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/discord/bug.py
[Install]
WantedBy=multi-user.target
I have done several test using simpler python programs before, and it all works fine. When trying to run the discord bot, it throws an error at the import statement. For testing I run:
sudo systemctl start bugstart
sudo systemctl status bugstart
The output of the status is the following:
bugstart.service - Testing
Loaded: loaded (/lib/systemd/system/bugstart.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2018-08-12 02:08:47 UTC; 1s ago
Process: 1039 ExecStart=/usr/bin/python3 /home/pi/discord/bug.py (code=exited, status=1/FAILURE)
Main PID: 1039 (code=exited, status=1/FAILURE)
Aug 12 02:08:46 raspberrypi systemd[1]: Started Testing.
Aug 12 02:08:47 raspberrypi python3[1039]: Traceback (most recent call last):
Aug 12 02:08:47 raspberrypi python3[1039]: File "/home/pi/discord/bug.py", line 1, in <module>
Aug 12 02:08:47 raspberrypi python3[1039]: import discord
Aug 12 02:08:47 raspberrypi python3[1039]: ImportError: No module named 'discord'
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Main process exited, code=exited, status=1/FAILURE
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Unit entered failed state.
Aug 12 02:08:47 raspberrypi systemd[1]: bugstart.service: Failed with result 'exit-code'.
I found out that the commands are run as root, so I figured it might be that the library hand't been installed on root, but I tried importing discord in the command line shell and it worked fine.

Logging fails sometimes when program is run as system user from a systemd service. As root always works

I have a system user account called user created like this:
useradd -r user
I have a very simple Python program which logs just one line:
#!/usr/bin/env python
import logging
import os
# Set logging options
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
logging.info('LOG SOMETHING')
And I have a systemd service to run that Python program:
[Unit]
Description=Run simple.py
[Service]
ExecStart=/usr/local/bin/simple.py
User=user
If I run the Python program from the command line, with both root and user users, it works just fine:
# /usr/local/bin/simple.py
INFO:root:LOG SOMETHING
# runuser -l user -c '/usr/local/bin/simple.py'
runuser: warning: cannot change directory to /home/user: No such file or directory
INFO:root:LOG SOMETHING
Ignore that warning. That's because the system user account user doesn't have a home directory.
Now, if I run the systemd service as the user user (note the User=user at the end of the service file), the logging sometimes fails, sometimes doesn't fail:
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# systemctl start simple.service
# journalctl -u simple.service | tail -18
Dec 08 17:57:23 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:23 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:24 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:24 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:25 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:25 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:25 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:25 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:26 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:26 localhost simple.py[12774]: INFO:root:LOG SOMETHING
Dec 08 17:57:26 localhost systemd[1]: Started Run simple.py.
Dec 08 17:57:27 localhost systemd[1]: Starting Run simple.py...
Dec 08 17:57:27 localhost systemd[1]: Started Run simple.py.
Now, if I run the systemd service as the root user (just removing the User=user at the end of the service file, and of course running systemctl daemon-reload), the logging always works:
# systemctl start simple.service
# journalctl -u simple.service | tail -18
Dec 08 18:04:03 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:03 localhost simple.py[12827]: INFO:root:LOG SOMETHING
Dec 08 18:04:03 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:03 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:03 localhost simple.py[12835]: INFO:root:LOG SOMETHING
Dec 08 18:04:03 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:04 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:04 localhost simple.py[12843]: INFO:root:LOG SOMETHING
Dec 08 18:04:04 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:05 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:05 localhost simple.py[12851]: INFO:root:LOG SOMETHING
Dec 08 18:04:05 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:05 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:05 localhost simple.py[12859]: INFO:root:LOG SOMETHING
Dec 08 18:04:05 localhost systemd[1]: Started Run simple.py.
Dec 08 18:04:06 localhost systemd[1]: Starting Run simple.py...
Dec 08 18:04:06 localhost simple.py[12867]: INFO:root:LOG SOMETHING
Dec 08 18:04:06 localhost systemd[1]: Started Run simple.py.
Why is that?

SystemD python task in Ubuntu 16

I am trying to execute a python3 with systemd in a ubuntu 16.
Following are the configs
[Unit]
Description=email notification server
After=multi-user.target
[Service]
WorkingDirectory=/home/ubuntu/email-noti
Restart=on-failure
ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py
[Install]
WantedBy=multi-user.target
*please note that email_reader.py import a config file and few python3 files from /home/ubuntu/email-noti/
but it always end with following error
● email-noti.service - email notification server
Loaded: loaded (/etc/systemd/system/email-noti.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Tue 2017-03-07 18:00:38 UTC; 6s ago
Process: 11859 ExecStart=/usr/lib/python3 /home/ubuntu/email-noti/email_reader.py (code=exited, status=203/EXEC)
Main PID: 11859 (code=exited, status=203/EXEC)
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Service hold-off time over, scheduling restart.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Stopped email notification server.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: email-noti.service: Start request repeated too quickly.
Mar 07 18:00:38 ip-172-31-24-115 systemd[1]: Failed to start email notification server.
but when i manually execute the email_reader.py works totally fine.
Any help appreciated

Categories

Resources