How to restart bluetooth service when my server service restarts in systemd - linux

I am using systemd services on an embedded system. I have a web service that needs to restart bluetooth everytime it restarts. How do I write the unit file for this. Also the service I have is put into systemd/user and not systemd/system.
I tried using PartOf=bluetooth.service but that didn't work.
[Unit]
# Human readable name of the unit
Description=Python User Service
#Link it to bluetooth
After=bluetooth.service
Requires=bluetooth.service
PartOf=bluetooth.service
[Service]
# Command to execute when the service is started
ExecStart=/usr/bin/python3 /home/root/MyServ.py
# Disable Python's buffering of STDOUT and STDERR, so that output from the
# service shows up immediately in systemd's logs
Environment=PYTHONUNBUFFERED=1
# Automatically restart the service if it crashes
Restart=on-failure
# Our service will notify systemd once it is up and running
#Type=notify
Type=simple
# Use a dedicated user to run our service
User=root
[Install]
# Tell systemd to automatically start this service when the system boots
# (assuming the service is enabled)
WantedBy=default.target

On [Unit] section you can reload services
PropagatesReloadTo=, ReloadPropagatedFrom=
A space-separated list of one or more units where reload requests on this unit will be propagated to, or reload requests on the
other unit will be propagated to this unit, respectively. Issuing a
reload request on a unit
will automatically also enqueue a reload request on all units that the reload request shall be propagated to via these two settings.
It sends bluetoothd command to reload via dbus. Not kill daemon, just reread configuration.
Or on [Service] section
ExecStopPost=/usr/bin/systemctl restart bluetooth.service
Or use override.conf on bluetooth.service
systemctl edit bluetooth.service
And here put
[Unit]
BindsTo=MyServ.service

Related

Ubuntu Server how to start a service only when VPN is connected and restart it every time i got disconnected

I created a test server when i got a service that need to be started on boot after i got connected to NordVPN.
Anyway, i found that if i get also disconnected the service need to be restarted after the connection to the VPN is restored
Can you help me with this?
Thanks a lot
I created a service and I delayed the time at boot
[Unit]
Description=qBittorrent-nox service
Documentation=man:qbittorrent-nox(1)
Wants=network-online.target
After=network-online.target nss-lookup.target
[Service]
ExecStartPre=/bin/sleep 60
# if you have systemd < 240 (Ubuntu 18.10 and earlier, for example), you probably want to use Type=>
Type=exec
# change user as needed
User=root
# The -d flag should not be used in this setup
ExecStart=/usr/bin/qbittorrent-nox
Restart=on-failure
RestartSec=1s
# uncomment this for versions of qBittorrent < 4.2.0 to set the maximum number of open files to unl>
#LimitNOFILE=infinity
# uncomment this to use "Network interface" and/or "Optional IP address to bind to" options
# without this binding will fail and qBittorrent's traffic will go through the default route
# AmbientCapabilities=CAP_NET_RAW
[Install]
WantedBy=multi-user.target
The Restart directive specifies the conditions under which the service should be restarted. The default value for this directive is on-failure, which means that the service will only be restarted if it exits with a non-zero exit code. In your case, you can modify this directive to specify that the service should be restarted whenever the VPN connection is restored.
Here is an example of how you could modify the Restart directive in your unit file:
[Service]
...
Restart=on-failure-or-vpn-restored
With this configuration, your service will be restarted whenever it exits with a non-zero exit code, or whenever the VPN connection is restored.
Note that you will need to create a separate unit file to manage the VPN connection and specify this unit file as a dependency for your service's unit file. This will ensure that the service is only started after the VPN connection is established.
For example, you could add the following lines to your service's unit file to make it dependent on a VPN connection:
[Unit]
...
After=vpn.service
Wants=vpn.service
This will ensure that your service is only started after the vpn.service unit has been started successfully. You will need to create the vpn.service unit file and specify the necessary details for managing the VPN connection.

How to kill selective child processes upon stop or restart of service

In the unit configuration file for systemd service, as mentioned below,
Inside the test_auto.tcsh script, there are 10 processes are being launched.
Upon service restart or stop, I want to kill only 2 selective processes out of 10. Not sure how can I achieve this from the systemd configuration settings.
KillMode=process make sure to kill only main process, on top of that, I want to kill 2 additional child process upon service termination, not all.
I tried to run a command (to kill the 2 selective processes) using ExecPostStop, but seems like this option doesn't work in Restart mode of service.
Any idea to get this functionality from the service settings ?
[Unit]
Description=test_auto
[Service]
Type=simple
ExecStart=/mydirectory/test_auto.tcsh
Restart=always
KillMode=process
[Install]
WantedBy=default.target

Do you know how to make the system automatically restart daemon service?

I have made a daemon service in linux server. It is running well. The service file is
stargate.service (in /etc/systemd/system).
[Unit]
Description=stargate
[Service]
Type=simple
PIDFile=/app/stargate/stargate.pid
ExecStart=/app/stargate/stargate.sh start
ExecReload=/app/stargate/stargate.sh restart
ExecStop=/app/stargate/stargate.sh stop
[Install]
Alias=stargate
WantedBy=default.target
If by some reasons, the daemon service is die and stop. Do you know how to make the system automatically restart the daemon service ?
How to make the daemon service starts if server get rebooted?
To respawn your service when it fails, add the following to the [Service] block:
[Service]
Restart=on-failure
RestartSec=3
If you wish to always restart when your service is killed use Restart=always
The RestartSec value is the delay between restart attempts.
See more info here: https://www.freedesktop.org/software/systemd/man/systemd.service.html

systemd After=nginx.service is not working

I am trying to setup a custom systemd service on my linux system and was experimenting with it
Following is my custom service, where it will trigger a bash file
[Unit]
Description=Example systemd service.
After=nginx.service
[Service]
Type=simple
ExecStart=/bin/bash /usr/bin/test_service.sh
[Install]
WantedBy=multi-user.target
Since I have mentioned After=nginx.service i was expecting nginx serivce to start automatically
So after starting the above service, i check the status of nginx, which has not started
However if i replace After with Wants it works
Can someone differenciate between After and Wants and when to use what?
Specifying After=foo tells systemd how to order the units if they are both started at the same time. It will not cause the foo unit to autostart.
Use After=foo in combination with Wants=foo or Requires=foo to start foo if it's not already started and also to keep desired order of the units.
So your [Unit] should include:
[Unit]
Description=Example systemd service.
After=nginx.service
Wants=nginx.service
Difference between Wants and Requires:
Wants= : This directive is similar to Requires= , but less strict. Systemd will attempt to start any units listed here when this unit is activated. If these units are not found or fail to start, the current unit will continue to function. This is the recommended way to configure most dependency relationships.

How to run node js even on server restart

I built a Nodejs project and now it runs smoothly.
I use forever service for running file in background but if server get restarted
the daemon won't be started automatically and should be started manually.
I want to run the daemon even the server get rebooted
You could add the forever command in .bash_profile so that every time the server restart, your command will simply be also executed.
nano ~/.bash_profile
forever start app.js # add this command to the file, or whatever command you are using.
source ~/.bash_profile # very important, else changes will not take effect
Next time, on your server restart, your command will also run, hence creating a daemon of your node script.
Note: This is maybe not the best solution, but the one I have got.
Update
As #dlmeetei, suggested, you can also start your nodejs app like a service so that we can use the features given by a linux service.
First create a file in /etc/systemd/system, like:
touch /etc/systemd/system/[your-app-name].service
nano /etc/systemd/system/[your-app-name].service
Then, add and edit the following script according to your relevance.
[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first
[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
# WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=<alternate user>
#Group=<alternate group>
Environment=NODE_ENV=production PORT=1337
[Install]
WantedBy=multi-user.target
Enable the service, it will marks the service for starting up on boot.
systemctl enable [your-app-name].service
Manage the service
systemctl start [your-app-name].service
systemctl stop [your-app-name].service
systemctl status [your-app-name].service # ensure your app is running
systemctl restart [your-app-name].service
Reference: https://www.axllent.org/docs/view/nodejs-service-with-systemd/
Thanks #dlmeetei for sharing the link.

Resources