script is executed . . . but ! Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

script is executed . . . but !

Post by Thuis »

I have a virtual switch that starts a script when it is on and starts a script when it is off.
on : script://BTmusic/badkamer1.sh
off : script://BTmusic/badkamer1s.sh

Pressing the switch on the script is executed according to the log and pressing it off also the other script.
2020-01-21 15:21:45.170 Status: Executing script: /home/onshuis/Domotica/domoticz/scripts/BTmusic/badkamer1.sh
2020-01-21 15:21:51.730 (Virtueel) Light/Switch (Badkamer1)
2020-01-21 15:21:51.717 Status: User: Admin initiated a switch command (189/Badkamer1/Off)
2020-01-21 15:21:51.934 Status: Executing script: /home/onshuis/Domotica/domoticz/scripts/BTmusic/badkamer1s.sh

Now it needs to play to a bluetooth speaker, inside the script badkamer1.sh is this:

Code: Select all

/usr/bin/mplayer -ao pulse::bluez_sink.00_XX_XX_XX_XX_1F.a2dp_sink   "/home/onshuis/Domotica/domoticz/scripts/BTmusic/music/relax1.mp3" -noconsolecontrols  -softvol -volume  85  > /dev/null 2>&1
When i press the virtual button from the website it plays nothing also mplayer is not even started.

When i ssh into the domoticz pc and start badkamer1.sh manually it plays to the bluetooth speaker. Then when i press the virtual button to off. The second script is executed and the music stops. So this is working.
off script contents:

Code: Select all

pkill -STOP mplay  > /dev/null &
The user and group that domoticz is started is the same i use to ssh into the pc. I have also other scripts that work correctly.
Then I stop the domoticz service when ssh connected. Then i manually start domoticz with domoticz start, i go to the website click the switch to on, the script is executed and music comes out of the bluetooth speaker. This is so weird to me, what am i missing or doing wrong ???

I used the preferred way, according to the wiki, to start domoticz with systemd.
First guess would be permissions but the group and user that domoticz starts is the same user if i start it manually by ssh, i tried different users and root, but no result.

I am very new to linux, there a lot of things i do not understand. I tried searching the internet for solutions, but i do not seem to find any. I tried a lot of things, but they are more or less how to pair and connect to bluetooth speaker and this works automatically.

Could someone please tell me what i am doing wrong?

This is my setup where domoticz runs on:
Spoiler: show
Architecture: i686
CPU op-mode(s): 32-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 2
Core(s) per socket: 1
Socket(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 28
Model name: Intel(R) Atom(TM) CPU N270 @ 1.60GHz
Stepping: 2
CPU MHz: 1017.483
CPU max MHz: 1600,0000
CPU min MHz: 800,0000
BogoMIPS: 3192.01
L1d cache: 24K
L1i cache: 32K
L2 cache: 512K

Distributor ID: Ubuntu
Description: Ubuntu 18.10
Release: 18.10
Codename: cosmic
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script is executed . . . but !

Post by FireWizard »

Hi,

This looks to be the same problem as I encountered and which is described in this thread:
https://www.domoticz.com/forum/viewtopi ... 41#p231841

Regards
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

Hmm ok, so
Scripts activated by triggers from Domoticz should not run as root!
For this moment I close this Topic.

Why does it work when i launch domoticz from ssh session?

I have another button, that activates/calls ffmpeg from a script file to make a recording from a webcam. The script has the same permissions as the badkamer1 script and the ffmpeg has the same permissions as the mplayer. And it does work from a button.

So i am really flabberguested
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
FireWizard
Posts: 1967
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script is executed . . . but !

Post by FireWizard »

Hi,

I did not investigate this problem further, as I found (in this case) a more suitable solution.
Scripts activated by triggers from Domoticz should not run as root!
For this moment I close this Topic.
This is my opinion and, as said, I found another solution,so I closed this thread.
Why does it work when i launch domoticz from ssh session?
With ssh you log in as the pi user and then it works.
Try to run it as root with sudo and see what happens.

Create a log file:

Code: Select all

#!/bin/sh
NOW=$(date +"%Y-%m-%d")
LOGFILE=/home/pi/test.$NOW.log
echo  $(date +"%Y-%m-%d %H:%M:%S") nest_hub | tee -a $LOGFILE
AND THEN YOUR SCRIPT COMMAND
insert your script command on the line "AND THEN YOUR SCRIPT COMMAND".

[Edit] See also this thread: https://www.domoticz.com/forum/viewtopic.php?t=23963

Regards
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

No i tried that it does not work. I did the test log thing, it creates the testlog file and there is nothing in only this: 020-01-23 11:29:00 nest_hub.
Domoticz is not running on a pi.

Now i did systemctl status domoticz when logged in with ssh. Then i started the switch and could see that it could not find the bluetooth. This is what it says:
Spoiler: show
Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
jan 23 11:20:07 [19329]: AUDIO: 48000 Hz, 2 ch, s16le, 320.0 kbit/20.83% (ratio: 40000->192000)
jan 23 11:20:07 [19329]: Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
jan 23 11:20:07 [19329]: ==========================================================================
jan 23 11:20:07 [19329]: AO: [pulse] Init failed: No such entity
jan 23 11:20:07 [19329]: Failed to initialize audio driver 'pulse::bluez_sink.00_XX_56_XX_7D_1F.a2dp_sink'
jan 23 11:20:07 [19329]: Could not open/initialize audio device -> no sound.
jan 23 11:20:07 [19329]: Audio: no sound
jan 23 11:20:07 [19329]: Video: no video
jan 23 11:20:07 [19329]: Exiting... (End of file)
So i think i am a step closer, but i do not understand, i start domoticz from systemd with the same user and group i use to ssh. I try to set user and group to root in the service/unit file for systemd but this has no result.
Can anyone help or explain what i am doing wrong?
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

Here is my service file:
Spoiler: show
[Unit]
Description=domoticz_service
[Service]
User=onshuis
Group=onshuis
ExecStart=/home/onshuis/Domotica/domoticz/domoticz -www 8080 -sslwww 443
WorkingDirectory=/home/onshuis/Domotica/domoticz
#
# Give the right to open priviliged ports. This allows you to run on a port <1024 without root permissions (user/group setting above)
#
# The following line is for pre-16.04 systems.
# ExecStartPre=setcap 'cap_net_bind_service=+ep' /home/domoticz/domoticz/domoticz
#
# The below works on Ubuntu 16.04 LTS
# CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#
# The following works on Ubuntu 18.04
AmbientCapabilities=CAP_NET_BIND_SERVICE
#
Restart=on-failure
RestartSec=1m
#StandardOutput=null
[Install]
WantedBy=multi-user.target

And i am fairly new to linux, but i am under the impression that now domoticz runs with user onshuis and group onshuis ? The same as i log in when ssh.
And here is the first part of my domoticz.sh:
Spoiler: show
! /bin/sh
### BEGIN INIT INFO
# Provides: domoticz
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Home Automation System
# Description: This daemon will start the Domoticz Home Automation System
### END INIT INFO

# Do NOT "set -e"

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DESC="Domoticz Home Automation System"
NAME=domoticz
USERNAME=onshuis
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
The bluetooth is there:
Spoiler: show
pactl list sources short
3 bluez_sink.00_58_56_2F_7D_1F.a2dp_sink.monitor module-bluez5-device.c s16le 2ch 48000Hz SUSPENDED
4 alsa_output.pci-0000_00_1b.0.analog-surround-41.monitor module-alsa-card.c s16le 5ch 48000Hz SUSPENDED
pactl list sinks short
3 bluez_sink.00_58_56_2F_7D_1F.a2dp_sink module-bluez5-device.c s16le 2ch 48000Hz SUSPENDED
4 alsa_output.pci-0000_00_1b.0.analog-surround-41 module-alsa-card.c s16le 5ch 48000Hz SUSPENDED

pacmd list-sources | grep -e 'index:' -e device.string -e 'name:' -e 'available'
2 source(s) available.
* index: 3
name: <bluez_sink.00_58_56_2F_7D_1F.a2dp_sink.monitor>
device.string = "00:58:56:2F:7D:1F"
index: 4
name: <alsa_output.pci-0000_00_1b.0.analog-surround-41.monitor>
device.string = "0"
/spoiler]
And it plays the song to the bluetoothspeaker when executing the script from the command line with ./badkamer1.sh as user onshuis in group onshuis, logged in with ssh as onshuis. Also the bluetooth speaker connects automatically after reboot of computer.
(onshuis is the user and group name i use for domotica stuff)

Is this all correct ? Or must i do something different ?
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

@FireWizard thanks for the link to the post. So if i understand, domoticz daemon always runs as root. I read this line:
Services are (99.999%) always started as root on Linux / *nix systems. The startup script is simply telling the OS to start the process domoticz as pi.

Could this be the problem that root has no access to the bluetooth ? Am i in the right direcrtion ?

I also did change the patch given in the other post, and change that line 62 in the domoticz.sh, but same.
Spoiler: show
● domoticz.service - domoticz_service
Loaded: loaded (/etc/systemd/system/domoticz.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-01-23 13:18:19 CET; 49s ago
Main PID: 32048 (domoticz)
Tasks: 35 (limit: 4517)
Memory: 42.0M
CGroup: /system.slice/domoticz.service
└─32048 /home/onshuis/Domotica/domoticz/domoticz -www 8080 -sslwww 443

jan 23 13:19:07 onshuis-pc domoticz[32048]: Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
jan 23 13:19:07 onshuis-pc domoticz[32048]: AUDIO: 48000 Hz, 2 ch, s16le, 320.0 kbit/20.83% (ratio: 40000->192000)
jan 23 13:19:07 onshuis-pc domoticz[32048]: Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
jan 23 13:19:07 onshuis-pc domoticz[32048]: ==========================================================================
jan 23 13:19:07 onshuis-pc domoticz[32048]: AO: [pulse] Init failed: No such entity
jan 23 13:19:07 onshuis-pc domoticz[32048]: Failed to initialize audio driver 'pulse::bluez_sink.00_58_56_2F_7D_1F.a2dp_sink'
jan 23 13:19:07 onshuis-pc domoticz[32048]: Could not open/initialize audio device -> no sound.
jan 23 13:19:07 onshuis-pc domoticz[32048]: Audio: no sound
jan 23 13:19:07 onshuis-pc domoticz[32048]: Video: no video
jan 23 13:19:07 onshuis-pc domoticz[32048]: Exiting... (End of file)
Also i use the preferred method from the wiki, and it says everywhere to better not run as root, i do not understand anymore?
Spoiler: show
Option 2: Systemd Alternative (Preferred)

Open the systemd configuration file, here you can change the port number you wish to use. To use a port below 1024 you can run as root (not reccomended) or you can un-comment one of the marked lines in the file - you should only un-comment one of the lines depending on your Ubuntu version :

vi /etc/systemd/system/domoticz.service

[Unit]
Description=domoticz_service
[Service]
User=domoticz
Group=domoticz
ExecStart=/home/domoticz/domoticz/domoticz -www 8080 -sslwww 443
WorkingDirectory=/home/domoticz
#
# Give the right to open priviliged ports. This allows you to run on a port <1024 without root permissions (user/group setting above)
#
# The following line is for pre-16.04 systems.
# ExecStartPre=setcap 'cap_net_bind_service=+ep' /home/domoticz/domoticz/domoticz
#
# The below works on Ubuntu 16.04 LTS
# CapabilityBoundingSet=CAP_NET_BIND_SERVICE
#
# The following works on Ubuntu 18.04
# AmbientCapabilities=CAP_NET_BIND_SERVICE
#
Restart=on-failure
RestartSec=1m
#StandardOutput=null
[Install]
WantedBy=multi-user.target
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

:oops: :oops: :oops: Or i just add the user to the audio group .... AAAAaaaahhhh !!!!

I been add this for days. Just uninstalled pulse and bluetooth everything. Started fresh, still the same, then i came to the idea to add the onshuis user to the audio group, and voila !
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
User avatar
Thuis
Posts: 281
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: script is executed . . . but !

Post by Thuis »

I was wrong, i changed from mplayer to play and it is solved ! Just to let anyone know.
If you can not handle a computer, you buy a Mac. If you can not handle domotica, you use Homeassistant!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest