how to reload a pythonic service on centos 7? - linux

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.

Related

need to waiting process in linux Operating system

Greatings to you first
I am a student at the university, and my end of study project is to master an information security protocol for autonomous systems
I have a task in this project
when I type in the command line "kill [pid]", the process starts automatically after a delay of a few seconds
how I can achieve this task and thank you in advance
use systemd service
Create a file test.service under /etc/systemd/system, such as:
[Unit]
Description=test service
[Service]
User=root
Restart=always
# number of seconds to wait before restarting
RestartSec=5
# Change it to some meaningful processes
ExecStart=/bin/sleep 30000
[Install]
WantedBy=multi-user.target
To start the service: sudo systemctl start test.service
Then if you kill the process, it will automatically restart in 5 seconds
Note: if you modify the service file, you will have to run sudo systemctl daemon-reload and sudo systemctl restart test to reload the change

systemctl enable and disable a custom service at bootup

I have an application for which I've written a myapp.service file and created a symlink for it in /etc/systemd/system/.
The myapp.service file is like this:
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=1
StartLimitInterval=0
User=myuser
ExecStart=/var/opt/myapp/myapp
[Install]
WantedBy=multi-user.target
I can use systemctl start myapp, systemctl stop myapp, systemctl status myapp to start, stop, and view the status of the service, and it works very well. I was hoping I could also use systemctl enable myapp, systemctl disable myapp, and systemctl is-enabled myapp to control whether myapp is automatically launched when the system is booted up. When I ran systemctl is-enabled myapp, it showed linked as the output. So I tried systemctl disable myapp and it deleted the symlink to /etc/systemd/system/myapp.service (the output was: Removed symlink /etc/systemd/system/myapp.service.). After that I couldn't run systemctl enable myapp, it just gave this output: Unit myapp.service could not be found.
What is the correct way to create a service such that it can be enabled and disabled with systemctl? I even tried doing it with sshd and was not able to enable after disabling it.
$ systemctl is-enabled sshd
enabled
$ systemctl disable sshd
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
Removed /etc/systemd/system/sshd.service.
$ systemctl is-enabled sshd
Failed to get unit file state for sshd.service: No such file or directory
$ systemctl enable sshd
Failed to enable unit: Unit file sshd.service does not exist.
Ultimately I just need to ensure that the application does not start at bootup, but can still be controlled with systemctl start myapp, systemctl stop myapp, systemctl status myapp. Does the linked status from systemctl is-enabled myapp mean it will not start at bootup? I tried checking the man page of systemctl, but couldn't find that state.
I can't reproduce on debian (version 244.3-1)
I created /etc/systemd/system/test.service:
me ~ $ sudo systemctl cat test.service
# /etc/systemd/system/test.service
[Unit]
Description=Test
[Service]
Type=oneshot
ExecStart=/bin/true
[Install]
WantedBy=multi-user.target
me ~ $ sudo systemctl enable test.service
Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /etc/systemd/system/test.service.
me ~ $ sudo systemctl disable test.service
Removed /etc/systemd/system/multi-user.target.wants/test.service
As expected, enable/disable creates/deletes a symbolic link to your service in /etc/systemd/system/multi-user.target.wants/. It does not touch /etc/systemd/system/*.service.
I also see that my console messages are slightly different. Which distro/version are you using?

Daemon service in systemd

I have managed to install daemon service in /etc/systemd/system, however I am not sure about 2 things:
Whether the daemon services should reside there
How can I elegantly check whether a daemon service is installed or not in systemd?
1.If the daemon services should reside there
yes, it is the .service location. The file that you should put here is:
mydeamon.service
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=**YourUser**
ExecStart=**pathToYourScript**
[Install]
WantedBy=multi-user.target
You’ll need to:
set your actual username after User=
set the proper path to your script in ExecStart= (usually /usr/bin/ You can put your script here)
creating-a-linux-service-with-systemd
2.How can I elegantly check if a daemon service is installed or not in systemd?
systemctl has an is-active subcommand for this:
systemctl is-active --quiet service
will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts:
systemctl is-active --quiet service && echo Service is running
test Service is running

How can i run a python file as service python3

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

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