not able to connect to session dbus from service in ubuntu - linux

I have to run my application as service in ubuntu 16.04. I am using systemd to make it run as service during bootup time. My application has to connect to both session dbus and system dbus.
connecting to system dbus is successful. But connecting to session dbus is failing.
I tried to run my application as service using "systemctl start Myapplication", this time also it is not connecting to session bus.
But if I run my application from terminal by "./Myapplication" , it is successfully connecting to both session and and system dbus.
can anyone help me with this?
The below code is my .service file content.
[Unit]
Description=node-health-monitor to observe system health
[Service]
Type=notify
ExecStart=/home/deepan/deepan/Myapplication
[Install]
WantedBy=graphical.target
Iam using GDBUS.

Set it up as user-service (so it can be run as systemctl --user start Myapplication).
Or keep using it as a system-wide service, but somehow switch the user in Myapplication when connecting to the session bus.
What I think causes problems:
The Myapplication is run as root user when doing systemctl start Myapplication.
So when it tries to connect to the session bus, it's trying to connect to the session of the root user.

Related

Running a systemd service without sudo privileges

I have a PC that transmits log data over a TCP/IP socket to a Raspberry Pi. I have written a python server program that runs on the Pi, so that when certain keywords are encountered, it has to play the corresponding audio track - this is a gist of the problem I'm currently working on.
Now, I want this server program to run as soon as the Raspberry Pi boots up, and so I wrote a systemd service to enable that. Assuming my server code is named as server.py, my service file looks as follows:
[Unit]
Description=Python Server
[Service]
# Command to execute when the service is started
ExecStart=/usr/bin/python3 -u /usr/bin/server.py
[Install]
WantedBy=multi-user.target
I make this server program executable, and I don't face any issues to start and enable the service (verified by rebooting the Pi as well). Now, taking a step back, I play the audio track on the python server program using the following lines (a little snippet):
import subprocess
subprocess.run(["cvlc", "~/Downloads/doors.wav"])
No errors here, all good. But when the service that runs the server program is started, when the specific keyword is encountered, the audio track does not play - it shows an error:
: [00005565470df480] vlcpulse audio output error: PulseAudio server connection failure: Connection refused
: [0000556547157310] dbus interface error: Failed to connect to the D-Bus session daemon: Unable to autolaunch a dbus-daemon without a $D
: [0000556547157310] main interface error: no suitable interface module
: [000055654700c570] main libvlc error: interface "dbus,none" initialization failed
: [0000556547101460] main interface error: no suitable interface module
: [000055654700c570] main libvlc error: interface "globalhotkeys,none" initialization failed
: [0000556547101460] dummy interface: using the dummy interface module...
: [00007f5f98c0b610] idummy demux: command `quit'
This same error shown above occurs when I try to play the audio from the command line with sudo, that is:
cvlc ~/Downloads/doors.wav
which led me to believe that if the service is enabled, then the whole python program corresponding to the service is run with sudo privileges automatically, even if I don't intend that to happen. I did a little bit of digging, but based on what I understand, to run the service, sudo privilege is necessary. I was not able to find a solution to run vlc with sudo, although I understand that ideally sudo privileges should not be given to something such as vlc. Is there a way around this?
This is happening because the PulseAudio server runs under your user, not as root. The service you are starting runs as root and tries to connect to root's PulseAudio server.
You can run specify which user/group to run the service as under the [Service] section with the User and Group directives.
You can also run the service as your user like this:
$ systemctl start --user <service>.service

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

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

How to install Jenkins Ubuntu slave as a service?

I have a Ubuntu 16.04 LTS machine where I am successfully connected to the Jenkins server via JNLP connection. Steps I took for the connection are the following:
Create a directory in the slave called /home/MyUbuntu/Jenkins
Download both agent.jar and slave-agent.jnlp files into the
directory
Run this command from the terminal:
java -jar agent.jar -jnlpUrl http://my-jenkins-server:8080/computer/MyNode/slave-agent.jnlp -secret 6f8bb3250d6dbcda77979797997b0ea6bcaaa064785d558c0e4ea07d03 -workDir "/home/MyUbuntu/Jenkins"
The connection is successful.
Problem:
Once I close the terminal the connection gets disconnected.
Question:
How do I add this as a service in Ubuntu 16.04 LTS so whenever the machine is rebooted it starts as a startup???
If you use System D, add a file like this to /etc/systemd/system/.
[Unit]
Description=Jenkins slave connection
Wants=network.target
After=network.target
[Service]
ExecStart=java -jar agent.jar -jnlpUrl http://jenkinsurl:port/endpoint.jnlp -secret 4lph4num3r1cs3cr3t -workDir "/base/path/of/your/jenkinsjar"
Restart=always
WorkingDirectory=/base/path/of/your/jenkinsjar
User=my-user
Group=my-group
RestartSec=20s
[Install]
WantedBy=multi-user.target
Alias=jenkins.service
Permissions and ownership of the file may vary based on the service or OS. Long list files in /lib/systemd/system/ to get an idea of what perms you need or want (probably root:root 644).
Notice the command is the command that Jenkins provides for you when you create an agent jar. Just use that for ExecStart.
For user and group, I use the user that owns the directory where the Jenkins workspace is located. For example, if the Jenkins workspace is in /home/ubuntu, I specify ubuntu as user and group.
After that...
Prefix these with sudo if you're not running as root:
Probably a good idea to reload System D: systemctl daemon-reload.
Start: systemctl start jenkins.service. Notice this command pertains to the last line of the file Alias.
Enable it if you want the service to start with your computer: systemctl enable jenkins.service.
Here are the Git Gists I based my file on:
https://gist.github.com/unakatsuo/d4711f52a0ab0b9bc8010018149a7e84
https://gist.github.com/dragolabs/05dfe1c0899221ce51204dbfe7feecbb
I'm sure there's a lot more that can be done for the service config but in my case, I manage a lot of different servers and just need the thing to start automatically after boot!

Node.js on Plesk not starting

I have installed the Node.js Extension and I can't get my app running. I have a Symfony 4 application and I am using socket.io for socket communication. Now I need to startup socket.js to startup my server. This file is located in the resources/js folder.
So I have setup Node.js as below:
Document Root: /application.domain.com/public
Application Mode: production
Application URL: This site is under development
Application Root: /application.domain.com
Application Startup File: resources/js/socket.js
Custom environment variables:
But when I start the application, it is not working. When I run the socket.js file from CLI (SSH connection) like this
/opt/plesk/node/8/bin/node socket.js
It is working fine. But now I need to keep my SSH connection alive and there is no check if socket.js is still running. I suppose the Node.js extension also takes care for that.
How can I use this Node.js extension to startup up my socket.js? Or how could I do this in another way?
OK, I fixed this another way with Systemd.
Found this on https://www.axllent.org/docs/view/nodejs-service-with-systemd/, so credits to Ralph Slooten.
Create the service file
/etc/systemd/system/nodeserver.service
Content
[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
systemctl enable nodeserver.service
Start the service
systemctl start nodeserver.service

Why need to reboot the system after starting service of Active MQ 5.6.0 on linux redhat

I have created one activemq service by using following steps in linux.
# ln –s /home/STI/SIServer/apache-activemq-5.6.0/bin/linux-x86-64/activemq \
/etc/init.d/activemq
# chkconfig –add activemq
Started activemq service by following command:
# chkconfig activemq on
But i need to reboot system to make activemq working.
Also, by stopping service using #chkconfig activemq off , service is not stopping.
I need to forcefully kill pid of activemq.
Please provide inputs , how i can resolve this issue.
Thanks in advance
chkconfig only controls which run levels a service will run at, it doesn't start the service. Use service activemq start to start it and service activemq stop to stop.

Resources