Permalänk
Medlem

Hjälp med BASH

Jag är absolut rudis på Linux. Jag har installerat apcupsd på min Raspberry Pi 4. Den har kontakt med UPS och jag kan få ut statusvärden från UPS. När man rycker strömmen syns det som ett meddelande i terminalen. Allt verkar fungera förutom att mitt bash-script inte körs.
Programmet kör ett script som heter /etc/apcupsd/apccontrol
apccontrol anropar onbattery eller offbattery beroende på händelse
kör jag /etc/apcupsd/apccontrol onbattery så fungerar det som tänkt oavsett om jag kör som root eller pi.

Mina egna teorier är
scriptet som körs är i själva verket ett annat än /etc/apcupsd/apccontrol
scriptet för någon ytterligare parameter som förhindrar att mitt script körs
något meck med behörigheter jag inte förstått, kollat att alla script involverade har executableflaggan men ser i scriptet att det även kollar -a som jag inte vet vad det är.

scriptet /etc/apcupsd/apccontrol

#!/bin/sh # # Copyright (C) 1999-2002 Riccardo Facchetti <riccardo@master.oasi.gpa.it> # # platforms/apccontrol. Generated from apccontrol.in by configure. # # Note, this is a generic file that can be used by most # systems. If a particular system needs to have something # special, start with this file, and put a copy in the # platform subdirectory. # # # These variables are needed for set up the autoconf other variables. # prefix=/usr exec_prefix=${prefix} APCPID=/var/run/apcupsd.pid APCUPSD=/sbin/apcupsd SHUTDOWN=/sbin/shutdown SCRIPTSHELL=/bin/sh SCRIPTDIR=/etc/apcupsd WALL=wall export SYSADMIN=root export APCUPSD_MAIL="mail" if [ -f $SCRIPTDIR/config ]; then . $SCRIPTDIR/config ; fi # # Concatenate all output from this script to the events file # Note, the following kills the script in a power fail situation # where the disks are mounted read-only. # exec >>/var/log/apcupsd.events 2>&1 # # This piece is to substitute the default behaviour with your own script, # perl, or C program. # You can customize every single command creating an executable file (may be a # script or a compiled program) and calling it the same as the $1 parameter # passed by apcupsd to this script. # # After executing your script, apccontrol continues with the default action. # If you do not want apccontrol to continue, exit your script with exit # code 99. E.g. "exit 99". # # WARNING: the apccontrol file will be overwritten every time you update your # apcupsd, doing `make install'. Your own customized scripts will _not_ be # overwritten. If you wish to make changes to this file (discouraged), you # should change apccontrol.sh.in and then rerun the configure process. # if [ -f ${SCRIPTDIR}/${1} -a -x ${SCRIPTDIR}/${1} ] then ${SCRIPTDIR}/${1} ${2} ${3} ${4} # exit code 99 means he does not want us to do default action if [ $? = 99 ] ; then exit 0 fi fi case "$1" in killpower) echo "Apccontrol doing: ${APCUPSD} --killpower on UPS ${2}" | (${WALL} 2>/dev/null || cat) sleep 10 ${APCUPSD} --killpower echo "Apccontrol has done: ${APCUPSD} --killpower on UPS ${2}" | (${WALL} 2>/dev/null || cat) ;; commfailure) echo "Warning communications lost with UPS ${2}" | ${WALL} ;; commok) echo "Communications restored with UPS ${2}" | ${WALL} ;; # # powerout, onbattery, offbattery, mainsback events occur # in that order. # powerout) ;; onbattery) echo "Power failure on UPS ${2}. Running on batteries." | ${WALL} onbattery ;; offbattery) echo "Power has returned on UPS ${2}..." | ${WALL} offbattery ;; mainsback) if [ -f /etc/apcupsd/powerfail ] ; then printf "Continuing with shutdown." | ${WALL} fi ;; failing) echo "Battery power exhausted on UPS ${2}. Doing shutdown." | ${WALL} ;; timeout) echo "Battery time limit exceeded on UPS ${2}. Doing shutdown." | ${WALL} ;; loadlimit) echo "Remaining battery charge below limit on UPS ${2}. Doing shutdown." | ${WALL} ;; runlimit) echo "Remaining battery runtime below limit on UPS ${2}. Doing shutdown." | ${WALL} ;; doreboot) echo "UPS ${2} initiating Reboot Sequence" | ${WALL} ${SHUTDOWN} -r now "apcupsd UPS ${2} initiated reboot" ;; doshutdown) echo "UPS ${2} initiated Shutdown Sequence" | ${WALL} ${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown" ;; annoyme) echo "Power problems with UPS ${2}. Please logoff." | ${WALL} ;; emergency) echo "Emergency Shutdown. Possible battery failure on UPS ${2}." | ${WALL} ;; changeme) echo "Emergency! Batteries have failed on UPS ${2}. Change them NOW" | ${WALL} ;; remotedown) echo "Remote Shutdown. Beginning Shutdown Sequence." | ${WALL} ;; startselftest) ;; endselftest) ;; battdetach) ;; battattach) ;; *) echo "Usage: ${0##*/} command" echo " warning: this script is intended to be launched by" echo " apcupsd and should never be launched by users." exit 1 ;; esac

Permalänk
Medlem
Skrivet av Mordekai:

Jag är absolut rudis på Linux. Jag har installerat apcupsd på min Raspberry Pi 4. Den har kontakt med UPS och jag kan få ut statusvärden från UPS. När man rycker strömmen syns det som ett meddelande i terminalen. Allt verkar fungera förutom att mitt bash-script inte körs.
Programmet kör ett script som heter /etc/apcupsd/apccontrol
apccontrol anropar onbattery eller offbattery beroende på händelse
kör jag /etc/apcupsd/apccontrol onbattery så fungerar det som tänkt oavsett om jag kör som root eller pi.

Mina egna teorier är
scriptet som körs är i själva verket ett annat än /etc/apcupsd/apccontrol
scriptet för någon ytterligare parameter som förhindrar att mitt script körs
något meck med behörigheter jag inte förstått, kollat att alla script involverade har executableflaggan men ser i scriptet att det även kollar -a som jag inte vet vad det är.

scriptet /etc/apcupsd/apccontrol

#!/bin/sh # # Copyright (C) 1999-2002 Riccardo Facchetti <riccardo@master.oasi.gpa.it> # # platforms/apccontrol. Generated from apccontrol.in by configure. # # Note, this is a generic file that can be used by most # systems. If a particular system needs to have something # special, start with this file, and put a copy in the # platform subdirectory. # # # These variables are needed for set up the autoconf other variables. # prefix=/usr exec_prefix=${prefix} APCPID=/var/run/apcupsd.pid APCUPSD=/sbin/apcupsd SHUTDOWN=/sbin/shutdown SCRIPTSHELL=/bin/sh SCRIPTDIR=/etc/apcupsd WALL=wall export SYSADMIN=root export APCUPSD_MAIL="mail" if [ -f $SCRIPTDIR/config ]; then . $SCRIPTDIR/config ; fi # # Concatenate all output from this script to the events file # Note, the following kills the script in a power fail situation # where the disks are mounted read-only. # exec >>/var/log/apcupsd.events 2>&1 # # This piece is to substitute the default behaviour with your own script, # perl, or C program. # You can customize every single command creating an executable file (may be a # script or a compiled program) and calling it the same as the $1 parameter # passed by apcupsd to this script. # # After executing your script, apccontrol continues with the default action. # If you do not want apccontrol to continue, exit your script with exit # code 99. E.g. "exit 99". # # WARNING: the apccontrol file will be overwritten every time you update your # apcupsd, doing `make install'. Your own customized scripts will _not_ be # overwritten. If you wish to make changes to this file (discouraged), you # should change apccontrol.sh.in and then rerun the configure process. # if [ -f ${SCRIPTDIR}/${1} -a -x ${SCRIPTDIR}/${1} ] then ${SCRIPTDIR}/${1} ${2} ${3} ${4} # exit code 99 means he does not want us to do default action if [ $? = 99 ] ; then exit 0 fi fi case "$1" in killpower) echo "Apccontrol doing: ${APCUPSD} --killpower on UPS ${2}" | (${WALL} 2>/dev/null || cat) sleep 10 ${APCUPSD} --killpower echo "Apccontrol has done: ${APCUPSD} --killpower on UPS ${2}" | (${WALL} 2>/dev/null || cat) ;; commfailure) echo "Warning communications lost with UPS ${2}" | ${WALL} ;; commok) echo "Communications restored with UPS ${2}" | ${WALL} ;; # # powerout, onbattery, offbattery, mainsback events occur # in that order. # powerout) ;; onbattery) echo "Power failure on UPS ${2}. Running on batteries." | ${WALL} onbattery ;; offbattery) echo "Power has returned on UPS ${2}..." | ${WALL} offbattery ;; mainsback) if [ -f /etc/apcupsd/powerfail ] ; then printf "Continuing with shutdown." | ${WALL} fi ;; failing) echo "Battery power exhausted on UPS ${2}. Doing shutdown." | ${WALL} ;; timeout) echo "Battery time limit exceeded on UPS ${2}. Doing shutdown." | ${WALL} ;; loadlimit) echo "Remaining battery charge below limit on UPS ${2}. Doing shutdown." | ${WALL} ;; runlimit) echo "Remaining battery runtime below limit on UPS ${2}. Doing shutdown." | ${WALL} ;; doreboot) echo "UPS ${2} initiating Reboot Sequence" | ${WALL} ${SHUTDOWN} -r now "apcupsd UPS ${2} initiated reboot" ;; doshutdown) echo "UPS ${2} initiated Shutdown Sequence" | ${WALL} ${SHUTDOWN} -h now "apcupsd UPS ${2} initiated shutdown" ;; annoyme) echo "Power problems with UPS ${2}. Please logoff." | ${WALL} ;; emergency) echo "Emergency Shutdown. Possible battery failure on UPS ${2}." | ${WALL} ;; changeme) echo "Emergency! Batteries have failed on UPS ${2}. Change them NOW" | ${WALL} ;; remotedown) echo "Remote Shutdown. Beginning Shutdown Sequence." | ${WALL} ;; startselftest) ;; endselftest) ;; battdetach) ;; battattach) ;; *) echo "Usage: ${0##*/} command" echo " warning: this script is intended to be launched by" echo " apcupsd and should never be launched by users." exit 1 ;; esac

När man skickar "onbattery" som argument så ser man att det ska skrivas ut en text och sedan ska den köra kommandot "onbattery".
Jag förmodar att kommandot "onbattery" inte hittas, förmodligen har din användare en annan PATH-miljövariabel än den som finns när scriptet körs automatiskt, vilket skulle förklara att olika saker kan hittas i de olika situationerna.
En enkel lösning vore att skriva hela sökvägen till "onbattery", så du inte är beroende av PATH.

Annars kan du lägga till "set -ex" i början av scriptet för att få se bättre vad som händer när det körs + stoppa vid fel.