AutoPilot Ausführung mehrerer Shell-Skripte innerhalb einer Skriptdatei

Tommes

Benutzer
Sehr erfahren
Maintainer
Mitglied seit
26. Okt 2009
Beiträge
9.176
Punkte für Reaktionen
1.128
Punkte
314
Nachfolgend wird beschrieben, wie sich mehrere Shell-Skript-Codeblöcke innerhalb einer, mit AutoPilot verbundenen Skriptdatei ausführen lassen. Dabei kann in jedem Codeblock z.B. die Ausführung eines Basic Backup Auftrages, einer Hyper Backup Aufgabe oder die Ausführung individueller Script-Anweisungen erfolgen. Wichtig hierbei ist, dass pro Codeblock immer nur eine Aufgabe definiert wird. Ein Codeblock ist dabei folgendermaßen aufgebaut.

Bash:
# --------------------------------------------------------------
# Shell script Codeblock
# --------------------------------------------------------------
# Important note: When executing the eval command, problems  may
# occur when using variables and quotation marks. For this
# reason, quotation marks must be escaped. Example: foo=\"bar\"
# If you want to execute a Basic Backup task, the escaping of the
# quotation marks must look like this: --job-name=\"Backup task 1\"
eval "script_${id} ()
{
    # Start here with the shell script without exit command
        ...
        ..
        .
        ..
        ...
    # End the script here

    exitcode_[${id}]=${?}
    sleep 5
}"
# Run shell script
script_${id}
((id++))
# End ----------------------------------------------------------

Innerhalb der markierten Bereiche # Start here with the shell script without exit command und # End the script here kann eine der oben bereits erwähnten Scriptanweisungen eingetragen werden. Wichtig zu beachten ist, das am Ende der Scriptanweisung kein Exit Code wie z.B. exit 0 oder exit ${?} ausgegeben werden darf. Diese Daten werden an anderer Stelle erhoben, ausgewertet und gegebenenfalls an das interne AutoPilot Protokoll übergeben. Auf diese Weise können mehrere Codeblöcke durch einfaches „copy and paste“ aneinandergereiht, sowie zwischen den grade erwähnten Markierungen mit jeweils individuellen Script-Anweisungen gefüllt werden.

Beispiel für einen Basic Backup Auftrag
Bash:
# Start here with the shell script without exit command
    /usr/syno/synoman/webman/3rdparty/BasicBackup/rsync.sh --job-name=\"Backup task 1\"
# End the script here

Beispiel für eine Hyper Backup Aufgabe
Bash:
# Start here with the shell script without exit command
    sleep 30
    /var/packages/HyperBackup/target/bin/dsmbackup --backup "17"
    pid=$(ps aux | grep -v grep | grep -E "/var/packages/HyperBackup/target/bin/(img_backup|dsmbackup|synoimgbkptool|synolocalbkp|synonetbkp|updatebackup).+-k 17" | awk '{print $2}')
    while ps -p $pid > /dev/null
    do
        sleep 60
    done
# End the script here

Beispiel für einen einfachen rsync Befehl
Bash:
# Start here with the shell script without exit command
    rsync -a --delete /volume[x]/share/quelle/ /volume[x]/share/ziel
# End the script here

Hier das komplette Script, indem sich weitere Codeblöcke durch einfaches "copy and paste" untereinander hinzufügen lassen.
Bash:
#!/bin/bash
# AutoPilot: Execution of several shell scripts within one script file
id=1
exitcode=

# --------------------------------------------------------------
# Shell script Codeblock
# --------------------------------------------------------------
# Important note: When executing the eval command, problems may
# occur when using variables and quotation marks. For this
# reason, quotation marks must be escaped. Example: foo=\"bar\"
# If you want to execute a Basic Backup task, the escaping of the
# quotation marks must look like this: --job-name=\"Backup task 1\"
eval "script_${id} ()
{
    # Start here with the shell script without exit command
        ...
        ..
        .
        ..
        ...
    # End the script here

    exitcode_[${id}]=${?}
    sleep 5
}"
# Run shell script
script_${id}
((id++))
# End ----------------------------------------------------------

# --------------------------------------------------------------
# Evaluation! Please do not change anything from here on
# --------------------------------------------------------------

# Path of the AutoPilot log file
log="/var/packages/AutoPilot/target/ui/usersettings/logfiles/autopilot.log"

# Exit code evaluation
for script in ${!exitcode_[@]}; do
    if [ ${exitcode_[${script}]} -gt 0 ]; then
        echo "Warning: Error executing script ${script}! Exit-Code ${exitcode_[${script}]}" >> "${log}"
        echo "" >> "${log}"
        exitcode=1
    fi
done

# Exit script
[[ ${exitcode} -eq 1 ]] && exit 1 || exit 0

Zur Arbeitsweise des Scripts:
Nach dem Aufruf des Scripts durch AutoPilot wird der erste Codeblock ausgeführt. Im Anschluss daran wird anhand des übergebenen Exit Code geprüft, ob dieser Codeblock fehlerfrei ausgeführt wurde oder nicht. Das Ergebnis wird am Ende des Scripts ausgewertet bei einem Fehler an das interne AutoPilot Protokoll übergeben. So lässt sich eine genaue Aussage darüber treffen, welcher Codeblock mit einem Fehler abgeschlossen wurde und welcher nicht. Anschließend wird nach dem gleichen Prinzip der nächste Codeblock ausgeführt und ausgewertet, bis alle Codeblöcke abgearbeitet wurden. Sind alle Codeblöcke fehlerfrei durchgelaufen, erfolgt keine weitere Meldung ins internen AutoPilot Protokoll.

Hier noch ein weiteres Beispiel, wie das Ganze mit mehreren Codeblöcken aussehen könnte...
Bash:
#!/bin/bash
# AutoPilot: Execution of several shell scripts within one script file
id=1
exitcode=

# --------------------------------------------------------------
# Shell script 1 - Example of a Basic Backup task
# --------------------------------------------------------------
# Important note: When executing the eval command, problems may
# occur when using variables and quotation marks. For this
# reason, quotation marks must be escaped. Example: foo=\"bar\"
# If you want to execute a Basic Backup task, the escaping of the
# quotation marks must look like this: --job-name=\"Backup task 1\"
eval "script_${id} ()
{
    # Start here with the shell script without exit command
        /usr/syno/synoman/webman/3rdparty/BasicBackup/rsync.sh --job-name=\"Backup task 1\"
    # End the script here

    exitcode_[${id}]=${?}
    sleep 5
}"
# Run shell script
script_${id}
((id++))
# End ----------------------------------------------------------

# --------------------------------------------------------------
# Shell script 2 - Example of a Hyper Backup task
# --------------------------------------------------------------
# Important note: When executing the eval command, problems may
# occur when using variables and quotation marks. For this
# reason, quotation marks must be escaped. Example: foo=\"bar\"
# If you want to execute a Basic Backup task, the escaping of the
# quotation marks must look like this: --job-name=\"Backup task 1\"
eval "script_${id} ()
{
    # Start here with the shell script without exit command
        sleep 30
        /var/packages/HyperBackup/target/bin/dsmbackup --backup "17"
        pid=$(ps aux | grep -v grep | grep -E "/var/packages/HyperBackup/target/bin/(img_backup|dsmbackup|synoimgbkptool|synolocalbkp|synonetbkp|updatebackup).+-k 17" | awk '{print $2}')
        while ps -p $pid > /dev/null
        do
            sleep 60
        done
    # End the script here

    exitcode_[${id}]=${?}
    sleep 5
}"
# Run shell script
script_${id}
((id++))
# End ----------------------------------------------------------

# --------------------------------------------------------------
# Shell script 3 - Example of a simple rsync task
# --------------------------------------------------------------
# Important note: When executing the eval command, problems may
# occur when using variables and quotation marks. For this
# reason, quotation marks must be escaped. Example: foo=\"bar\"
# If you want to execute a Basic Backup task, the escaping of the
# quotation marks must look like this: --job-name=\"Backup task 1\"
eval "script_${id} ()
{
    # Start here with the shell script without exit command
        rsync -a --delete /volume[x]/share/quelle/ /volume[x]/share/ziel
    # End the script here

    exitcode_[${id}]=${?}
    sleep 5
}"
# Run shell script
script_${id}
((id++))
# End ----------------------------------------------------------

# --------------------------------------------------------------
# Evaluation! Please do not change anything from here on
# --------------------------------------------------------------

# Path of the AutoPilot log file
log="/var/packages/AutoPilot/target/ui/usersettings/logfiles/autopilot.log"

# Exit code evaluation
for script in ${!exitcode_[@]}; do
    if [ ${exitcode_[${script}]} -gt 0 ]; then
        echo "Warning: Error executing script ${script}! Exit-Code ${exitcode_[${script}]}" >> "${log}"
        echo "" >> "${log}"
        exitcode=1
    fi
done

# Exit script
[[ ${exitcode} -eq 1 ]] && exit 1 || exit 0

Dem Script wurde ein wichtiger Hinweis hinzugefügt:
Bei der Ausführung des eval-Befehls innerhalb der Funktion script_${id} () kann es zu Problemen bei der Verwendung von Variablen und Anführungszeichen kommen. Aus diesem Grund müssen die Anführungszeichen mit einem Backslash (einen umgedrehten Schrägstrich) maskiert werden. Beispiel: foo=\"bar\"
Bei der Ausführung eines Basic Backup Auftrages muss die Maskierung der Anführungszeichen wie folgt aussehen: --job-name=\"Backup task 1\"

Viel Spaß

Tommes
 
Zuletzt bearbeitet:

Yippie

Benutzer
Mitglied seit
01. Feb 2011
Beiträge
580
Punkte für Reaktionen
32
Punkte
54
Danke, liest sich ja interessant und ich denke ich habe eine Anwendung dafür, aber was macht genau oder woher stammt AutoPilot? Ist das ein 3rd-Party Package?

Edit: Wer Lesen kann ist klar im Vorteil, hab's in deiner Sig gesehen ;)AutoPilot
 
  • Like
Reaktionen: Tommes

Tommes

Benutzer
Sehr erfahren
Maintainer
Mitglied seit
26. Okt 2009
Beiträge
9.176
Punkte für Reaktionen
1.128
Punkte
314
Hi!

Ich bin heute auf ein Problem gestoßen, wofür ich aber bereits eine Lösung erarbeitet habe. Den Eingangspost habe ich daher entsprechend angepasst und am Ende des Textes einen Spoiler platziert, welcher Auskunft zu dem Problem gibt und wie man dieses löst. Falls also noch Fragen diesbezüglich auftauchen, immer raus damit.

Tommes
 
  • Like
Reaktionen: Benie


 

Kaffeautomat

Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.

Als Dankeschön schalten wir deinen Account werbefrei.

:coffee:

Hier gehts zum Kaffeeautomat 

 
 
  AdBlocker gefunden!

Du bist nicht hier, um Support für Adblocker zu erhalten. Dein Adblocker funktioniert bereits ;-)

Klar machen Adblocker einen guten Job, aber sie blockieren auch nützliche Funktionen.

Das Forum wird mit hohem technischen, zeitlichen und finanziellen Aufwand kostenfrei zur Verfügung gestellt. Wir zeigen keine offensive Werbung und bemühen uns um eine dezente Integration.

Bitte unterstütze dieses Forum, in dem du deinen Adblocker für diese Seite deaktivierst.

Du kannst uns auch über unseren Kaffeautomat einen Kaffe ausgeben oder ein PUR Abo abschließen und das Forum so werbefrei nutzen.

Vielen Dank für Deine Unterstützung!