Moin gwaag,
ok, nun noch mal zum Mitschreiben.
- Reboot
- Taste drücken, Meldung geht raus.
- Du mit Putty auf den RPi.
- Taste drücken, Meldung geht nicht raus.
- systemctl sagt aber, das das Programm noch rennt.
Soweit richtig?
73 de Bernd
Moin gwaag,
ok, nun noch mal zum Mitschreiben.
- Reboot
- Taste drücken, Meldung geht raus.
- Du mit Putty auf den RPi.
- Taste drücken, Meldung geht nicht raus.
- systemctl sagt aber, das das Programm noch rennt.
Soweit richtig?
73 de Bernd
(gelöst)Script läueft nur nach reboot? Schau mal ob du hier fündig wirst!
Ja genau, so hast Du es richtig verstanden.
Habe soeben auch noch mit powershell probiert, genau selbes verhalten.
gruss
gwaag
PS: roter Punkt ist nun weg, Danke
Moin gwaag,
in ein_ausvonalarm.py schreibst du ja ein Log: /home/pi/th/Alarm.log.
Kannst du in dem Python-Script nach dieser Zeile
logging.basicConfig(filename='/home/pi/th/Alarm.log', format='%(asctime)s %(message)s', datefmt = "Woche %V %a %d.%m.%Y %H:%M:%S ", level=logging.INFO)
eine logging.info einbauen?
Und ev. in den event erst loggen und dann die anderen Scripts aufrufen?
Irgendwie müssen wir doch hinter den Fehler kommen.
73 de Bernd
Hallo Bernd666
Danke fuer den Tipp mit dem logging.info.
Habe es so eingebaut:
logging.basicConfig(filename='/home/pi/th/Alarm.log', format='%(asctime)s %(message)s', datefmt = "Woche %V %a %d.%m.%Y %H:%M:%S ", level=logging.INFO)
logging.info('Anfang')
sleep(2)
def low(button):
subprocess.run("/home/pi/th/aus.py") # Licht aus
logging.info('Alarm AUS') # Logeintrag
Log sagt:
Woche 42 So 18.10.2020 08:31:50 Reboot
Woche 42 So 18.10.2020 08:32:24 Anfang
Woche 42 So 18.10.2020 08:33:22 Alarm EIN
Woche 42 So 18.10.2020 08:33:33 Alarm AUS
Erst mit dieser Aenderung habe ich gesehen dass waehrend dem einloggen mit Putty das Licht kurz an und aus ging. ( Das was "aus.py" macht)
Nach dem ich gesucht habe von wo das sonst noch her kommen kann, habe ich noch ein Script gefunden das etwas aehnliches macht.
Nach dem ich das deaktiviert habe, funktioniert jetzt auch das ein_ausvonalarm.py wenn ich eingeloggt bin.
D.h ich muss jetzt untersuchen wiso.
Danke und gruss
gwaag
Script das ich deaktiviert habe und untersuchen muss:
#!/usr/bin/env python3
# Startet "Zone1-4Morgen", wenn im Zeitfenster, beim einschalten von Alarm
# und stoppt mit "aus" beim ausschalten, oder Zeitfenster Ende
from datetime import datetime
import subprocess
from subprocess import Popen
import gpiozero
from gpiozero import Button
import time
from time import sleep
# definitions
testprocess = None
button = Button(24, pull_up=True, hold_time=1)
# start script
while True:
sleep(0.1)
now = datetime.now() # Datum heute
Now = now.hour*60+now.minute
timewindow_ok = ((6*60+45) <= Now and Now < (8*60+00))
year = datetime.now().year # Jahr heute
Year = datetime.now().year+1 # Naechstes Jahr
if timewindow_ok and button.is_pressed and not testprocess:
sleep(0.5)
testprocess = Popen(["/home/pi/th/Zone1-4Morgen"])
sleep(1)
if not timewindow_ok and testprocess:
sleep(0.5)
subprocess.Popen(["/home/pi/th/aus.py"])
testprocess.kill()
testprocess = None
sleep(1)
if not button.is_pressed and testprocess:
sleep(0.5)
if testprocess:
testprocess.kill()
testprocess = None
subprocess.Popen(["/home/pi/th/aus.py"])
sleep(1)
Alles anzeigen
Hallo,
ich habe jetzt nicht alle durchgelesen, habe nur eine Anmerkung zu deinem "gefundenen" Python-Skript.
Auf dass, das man da einiges anders schreibt will ich nicht eingehen, da ich selbst das meiste nachschauen müsste. (Schlagworte: PEP8, aufrufen von Python-Programme in Python, importieren)
Aufgefallen ist mit, dass du in dem Ordner 'th' einmal 'aus.py' aufrufst und einmal 'Zone1-4Morgen'. Ich weis nicht was dass ist, aber fehlt hier eventuell eine Dateiendung?
Falls das nichts zur Lösunssuche beiträgt, einfach überlesen.
Grüße
Dennis
aus.py is python script
Zone1-4Morgen is basch scrip, also ohne Endung.
Koenntest Du mit ev. sagen ob es in dieser Logik noch Klammern braucht, kenn mich da zuweng aus.
Habe jetz4 scripte in eines geschmissen, daher brauce ich jetzt diese Logik.
if not button.is_pressed and testprocess or not timewindow1_ok and testprocess or not timewindow2_ok and testprocess:
Gruss
und Danke
gwaag
Moin gwaag,
wie ich schon geschrieben habe. Python kann ich nicht.
da sollte jemand mit Kenntnis helfen.
Sri.
73 de Bernd
if not button.is_pressed and testprocess or not timewindow1_ok and testprocess or not timewindow2_ok and testprocess:
Klammern sind keine notwendig. Wenn ich mir bei sowas nicht sicher bin, dann ersetze ich die Bedingungen in einem Beispielskript und kann leicht testen, ob die Syntax stimmt.
Zur Überprüfung kannst du auch ein 'print()' oder besser ein 'ic()' einabuen. (IceCream -- Never use print() to debug again)
Grüße
Dennis
gwaag Ob da Klammern hin *müssen* kommt letztlich darauf an ob die Logik denn so stimmt. Man kann aber auch argumentieren, dass bei einer so langen Bedingung nicht auch ansonsten implizite Klammern gesetzt werden sollten, damit man das leichter lesen/verstehen kann. Die impliziten Klammern wären ja folgende:
if (
(not button.is_pressed and testprocess)
or (not timewindow1_ok and testprocess)
or (not timewindow2_ok and testprocess)
):
...
Was hier jetzt sofort ins Auge springt ist das alle drei Konjunktionen `testprocess` erfordern. Das könnte man da also herausziehen:
if testprocess and (
not button.is_pressed or not timewindow1_ok or not timewindow2_ok
):
...
Und dann kann man natürlich noch die ganzen ``not`` aus dem zweiten Teilausdruck heraus ziehen:
Was stehst in der /home/pi/.bashrc des Benutzers PI?
@_blackjack_,
das nehm ich, mach Sinn.
@Rasp_Berlin
das steht drin:
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \$\[\033[00m\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
cd /home/pi/th
Alles anzeigen
Gruss
gwaag
Hallo Rasp_Berlin,
hattest Du Zeit es Dir anzuschauen?
Gruss
gwaag
Moin gwaag,
wenn ich auch darf.
cd /home/pi/th
das sollte nicht stören, aber kannst ja mal löschen.
73 de Bernd
Sorry, war eine Woche weg.
In der Zwischenzeit bin ich nur etwas weiter gekommen. Habe 4 script in eines gepackt, brachte aber nichts.
Nach reboot alles ok. Log Info sagt geladen, Script funktioniert einwandfrei.
Wenn ich mich mit Putty einlogge und sonst nichts mache, geht es nicht.
Starte ich das Script in der Komandozeile, kommt Log Info, geladen, wenn ich jetzt Taster druecke /loslasse schickt es mail_alarmEin / Aus
Log Info sagt Alarm Ein/Aus
Stoppe ich das Script, geht es nicht mehr. Mit ps aux steht aber immer noch das ein_ausvonalarm.py läuft.
Bin echt ratlos.
Gruss
gwaag
root 1152 0.0 0.0 0 0 ? I 12:46 0:01 [kworker/u2:0-events_unbound]
root 1192 0.0 0.0 0 0 ? I< 13:00 0:00 [kworker/0:2H-kblockd]
root 1242 1.0 2.4 44580 10844 ? Ssl 13:00 0:19 /usr/bin/python3 /home/pi/th/ein_ausvonalarm.py
root 1267 0.0 1.4 12220 6284 ? Ss 13:09 0:00 sshd: pi [priv]
pi 1329 0.2 0.9 12220 4132 ? R 13:09 0:02 sshd: pi@pts/0
[Unit]
Description=ein_ausvonalarm Service
After=multi-user.target
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/th/ein_ausvonalarm.py
Restart=always
[Install]
WantedBy=multi-user.target
Alles anzeigen
#!/usr/bin/env python3
import time
from time import sleep
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sender(recipients):
body = 'Alarm wurde Ein geschaltet, dies zur Info.'
msg = MIMEMultipart()
msg['Subject'] = 'Alarm ABUS Ein'
msg['From'] = '*****@*******.ch'
msg['To'] = (', ').join(recipients.split(','))
msg['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('mail.********.ch', 587)
server.starttls()
server.login('*****@*****.ch', '********')
server.send_message(msg)
server.quit()
if __name__ == '__main__':
sender('*******@********.ch')
print("Email sent Alarm Ein")
sleep(0.5)
Alles anzeigen
#!/usr/bin/env python3
import logging
import locale
import subprocess
from subprocess import Popen
import gpiozero
from gpiozero import Button
import signal
from signal import pause
import datetime
from datetime import datetime
import time
from time import sleep
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
button_Gui = Button(22,hold_time=1)
testprocess = None
logging.basicConfig(filename='/home/pi/th/Alarm.log', format='%(asctime)s %(message)s', datefmt = "Woche %V %a %d.%m.%Y %H:%M:%S", level=logging.INFO)
logging.info('Script geladen')
sleep(3)
def low(button):
subprocess.run("/home/pi/th/aus.py") # Licht aus
logging.info('Alarm AUS') # Logeintrag
if button_Gui.is_pressed: # mail nur schicken wenn Ferien
subprocess.run("/home/pi/th/Mail_alarmAus.py")
def high(button):
subprocess.run("/home/pi/th/aus.py") # Licht aus
logging.info('Alarm EIN') # Logeintrag
if button_Gui.is_pressed:
subprocess.run("/home/pi/th/Mail_alarmEin.py") # mail nur schicken wenn Ferien
button = Button(24, pull_up=True, hold_time=1) # Pin 18 GPIO 24 input (Kontakt von Alarmanlage)
button.when_released =low
button.when_pressed = high
while True:
sleep(0.1)
now = datetime.now()
Now = now.hour*60+now.minute
# example time window 06.40h-8.00h ((6*60+40) <= Now and Now < (8*60+00))
timewindow1_ok = ((6*60+45) <= Now and Now < (8*60+00))
timewindow2_ok = ((16*60+20) <= Now and Now < (22*60+30))
sleep(2)
if timewindow1_ok and button.is_pressed and not testprocess:
sleep(0.5)
testprocess = Popen(["/home/pi/th/Zone1-4Morgen"])
sleep(1)
if timewindow2_ok and button.is_pressed and not testprocess:
sleep(0.5)
testprocess = Popen(["/home/pi/th/Zone1-4Abend"])
sleep(1)
if not timewindow1_ok and testprocess and not timewindow2_ok or not timewindow2_ok and testprocess and not timewindow1_ok:
sleep(0.5)
subprocess.Popen(["/home/pi/th/aus.py"])
sleep(1)
testprocess.kill()
testrocess = None
sleep(1)
if not button.is_pressed and testprocess:
sleep(0.5)
if testprocess:
testprocess.kill()
testprocess = None
subprocess.Popen(["/home/pi/th/aus.py"])
sleep(1)
pause()
Alles anzeigen
Gibt es eine ~/.profile? und war steht in dieser Datei am Ende?
Was steht in der Datei /etc/profile am Ende?
Gibt es Dateien im Verzeichnis /etc/profile.d?
Ist eine Datei davon neuer?
Bei gibt es beim einen PI:
root@pihole:~# ls -la /etc/profile.d/
insgesamt 24
drwxr-xr-x 2 root root 4096 Aug 21 17:03 .
drwxr-xr-x 86 root root 4096 Sep 17 20:15 ..
-rw-r--r-- 1 root root 95 Apr 29 2019 at-dbus-fix.sh
-rw-r--r-- 1 root root 664 Mär 1 2019 bash_completion.sh
-rw-r--r-- 1 root root 324 Mai 9 2019 sshpwd.sh
-rw-r--r-- 1 root root 450 Jul 22 2019 wifi-check.sh
root@pihole:~#
Hallo Rasp-Berlin,
hier die Daten:
Danke und Gruss
pi@raspberrypi:~/th $ ls -la /etc/profile.d/
total 28
drwxr-xr-x 2 root root 4096 Okt 28 10:12 .
drwxr-xr-x 121 root root 4096 Okt 28 12:14 ..
-rw-r--r-- 1 root root 95 Apr 29 2019 at-dbus-fix.sh
-rw-r--r-- 1 root root 664 Mär 1 2019 bash_completion.sh
-rw-r--r-- 1 root root 324 Feb 20 2020 sshpwd.sh
-rw-r--r-- 1 root root 1945 Dez 28 2018 vte-2.91.sh
-rw-r--r-- 1 root root 450 Jul 22 2019 wifi-check.sh
pi@raspberrypi:~/th $
home/pi/.profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
Alles anzeigen
/etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games"
fi
export PATH
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
Alles anzeigen
Moin gwaag,
Stoppe ich das Script, geht es nicht mehr. Mit ps aux steht aber immer noch das ein_ausvonalarm.py läuft.
Es muss ja auch einmal das Programm laufen, du hast ja die Serviceunit gestartet. Oder ist sie nicht aktiviert worden?
Wenn du einen Reboot machst, fliegst du ja sowieso mit Putty raus.
Kannst du dann mehrmals die Meldung auslösen?
Wenn ja, und du dann mit Putty den Raspberry kontaktierst, dann geht nichts mehr?!?
Das passiert auch wenn du in einer Windowskonsole mit ssh auf den Raspberry gehst?
73 de Bernd
Die service unit läuft.
Nach einem reboot funktoniert es tagelang, einmal eingeloggt, geht es nicht mehr.
Mit powershell selbes verhalten, siehe post 44.
Gruss
gwaag
Was treibt dieses Script?
Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!