How can i run a python file as service python3 - python-3.x

I have a python script and that I have to run as a service/background process, I have tried python-daemon but it gives me errors is there any good way to run it as a service so that the script can be run in the background to accept messages and process files?

I have made a sytemctl service file in /lib/systemed/system
The file content is
[Unit]
Description=RPC SERVER
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/
Type=simple
ExecStart=/usr/bin/ python3 /home/ubuntu/rpc.py
[Install]
WantedBy=multi-user.target
/home/ubuntu/rpc.py is my python script path
and i saved the file as rpcservertest.service
so i can start the service using
sudo systemctl start rpcservertest.service
to stop
sudo systemctl stop rpcservertest.service
and to get status
sudo systemctl status rpcservertest.service

Related

systemctl service hang on start command, no logs in journalctl

I am trying to understand why my service isn't starting on some of my devices but is starting on others. When I try to do systemctl start pishock.service it just hangs, it doesn't give me any output, it doesn't start the service, it just hangs there. It's an head scratcher because it works on others very close environment. (Same raspbian, same versions of systemctl and same version of python)
[Unit]
Description=PiShock Client
After=multi-user.target
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/usr/bin/python3 /home/pi/PiShockClient.pyc
[Install]
WantedBy=multi-user.target
If I try to start PiShockClient.pyc normally by entering sudo python3 PiShockClient.pyc it starts sucessfully.
The code above works on some of my other Pi Zero Ws
I have done apt-get upgrade, apt-get dist-upgrade.
The weird part is that there's no logs in journalctl, even under journalctl -xe. It just hangs like it never tries to start the service.
I am at a complete loss.

Auto-starting Twonky Server on Ubuntu 18.04 using systemd

I was trying to set up a Twonky Server on Ubuntu. The server works fine, but I could not get systemd to autostart the server (using a service file I created at /etc/systemd/system/twonkyserver.service). Sometimes I got the cryptic error message that some PID-file (/var/run/mediaserver.pid) is not accessible, the exit code of the service is 13, which apparently is a EACCES Permission denied error . The service runs as root.
I finally managed to fix the problem by setting PIDFile in the twonkyserver.service file to /var/run/mediaserver.pid. For reference, find the service file below:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
As described above, the below service file auto-starts the Twonky Server on boot. Simply create it using vim /etc/systemd/system/twonkyserver.service. This assumses that you have installed the Twonky Server to usr/local/twonky. The shell-file twonky.sh already provides a nice interface to the service file (twonky.sh start|stop|reload|restart, also see twonky.sh -h).
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/twonky/twonky.sh start
ExecStop=/usr/local/twonky/twonky.sh stop
ExecReload=/usr/local/twonky/twonky.sh reload
ExecRestart=/usr/local/twonky/twonky.sh restart
PIDFile=/var/run/mediaserver.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
I would slightly amend the start and stop commands from twonky.sh and put them directly into the twonky.service file for systemd:
[Unit]
Description=Twonky Server Service
After=network.target
[Service]
Type=simple
#Systemd will ensure RuntimeDirectory for the PID file is created under /var/run
RuntimeDirectory=twonky
PIDFile=/var/run/twonky/mediaserver.pid
# use the -mspid argument for twonkystarter to put the pid file in the right place
ExecStart=/usr/local/twonky/twonkystarter -mspid /var/run/twonky/mediaserver.pid -inifile /usr/local/twonky/twonkyserver.ini -logfile /usr/local/twonky/twonky.log -appdata /usr/local/twonky
ExecStop=kill -s TERM $MAINPID
ExecStopPost=-killall -s TERM twonkystarter
ExecStopPost=-killall -s TERM twonky
# Twonky 8.5.1 doesn't reload, it stops instead (on arm at least)
# ExecReload=kill -s HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
You need to be sure the paths in the ExecStart command match where you unpacked twonky, and also where you want the .pid file, configuration, logfile and runtime appdataunless you are happy with their default locations.
After putting that all into/etc/systemd/system/twonky.server, run
sudo systemctl daemon-reload
sudo systemctl start twonky
sudo systemctl enable twonky

how to reload a pythonic service on centos 7?

I have a python app that I made it as a service on centos 7.
I created a file in /usr/lib/systemd/system with my project name. And wrote these on it:
[Unit]
Description=My Script Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python3.6 /usr/src/python-project/sampleService-services/serverprotocol.py
[Install]
WantedBy=multi-user.target
After that:
$ sudo systemctl daemon-reload
$ sudo systemctl enable sampleService.service
$ sudo reboot
I can start, restart and stop this service with commands:
$ systemctl start sampleService.service
$ systemctl restart sampleService
$ systemctl stop sampleService
But when i try to reload it with these commands:
$ systemctl reload sampleService
or
$ service sampleService reload
I get this error:
Failed to reload sampleService.service: Job type reload is not applicable for unit basiscore.service.
See system logs and 'systemctl status sampleService.service' for details.
Is there any command for reload this pythonic service ?!
how can I reload my service without restarting it ?!
Under the ExecStart= line, try to add
Restart=on-failure
RestartSec=10s
For systemctl reload ... to work, you need to provide an ExecReload= line in your unit (service) file. A common example is:
ExecReload=/bin/kill -HUP $MAINPID
That requires your program to catch and act on a SIGHUP signal. If your application has a different mechanism to trigger a reload of its configuration while running, then provide some other suitable command which generates that trigger.

Convert upstart script to systemd for django channel on ubuntu 16.04

I am following http://masnun.rocks/2016/11/02/deploying-django-channels-using-daphne this tutorial to deploy Django channel on Nginx.In this tutorial, they used upstart script to run daphne serve.I need to convert upstart script to systemd because i am working on ubuntu 16.04.
Below is upstart script
start on runlevel [2345]
stop on runlevel [016]
respawn
script
cd /home/ubuntu/Project/projectname
export DJANGO_SETTINGS_MODULE="projectname.settings"
exec daphne -b 0.0.0.0 -p 8001 projectname.asgi:channel_layer
end script
Below is systemd script which i converted
[Unit]
Description=daphne server script
[Service]
Environment=DJANGO_SETTINGS_MODULE="projectname.settings"
WorkingDirectory=/home/ubuntu/Project/projectname
ExecStart=daphne -b 0.0.0.0 -p 8001 projectname.asgi:channel_layer
Restart=always
[Install]
WantedBy=multi-user.target
When i run systemd service status it gave me below error
Failed to get properties: No such interface ''
From man systemd.service in the section on ExecStart=:
For each of the specified commands, the first argument must be an absolute path to an executable.
You have other problems, but first you need to provide a full path to daphne. You can check your file with:
systemd-analyze verify /path/to/your/file.service

systemctl service not starting after system restart

I have this service which I want to be able to start as a service on system restart. I am using Ubuntu 15.10. The service configuration file looks like this:
[Unit]
Description=Service client
After=syslog.target
[Service]
ExecStart=/bin/bash -c "/usr/local/bin/service_clientd start"
ExecStop=/bin/bash -c "/usr/local/bin/service_clientd stop"
Type=simple
[Install]
WantedBy=multi-user.target
The service starts perfectly with systemctl command, but does not start automatically after system restart.
Do this:
systemctl enable servicename.service
https://wiki.archlinux.org/index.php/systemd#Using_units

Resources