4

I have a script that always runs on the Raspberry Pi.

I defined with sudo nano /etc/rc.local so that they turn even after a reboot or a power failure.

My question is how can visualize the execution of my scrpits after rebooting or power failure especially that there are "print" (because the execution becomes in the background).

F1Linux
  • 1,677
  • 1
  • 15
  • 32
Amélie
  • 41
  • 1

2 Answers2

4

Redirect output and errors to a file:

myscript.sh >> /home/pi/log.txt 2>&1 &
CoderMike
  • 7,102
  • 1
  • 11
  • 16
1

Redirect output of set -x to a log to capture any potential errors as script executed.

varFD is an arbitrary variable name (you could call it something else if you wish) and used here to assign the next unused File Descriptor to redirect output to the log

In your script under the SheBang add the following:

#!/bin/bash

exec {varFD}>/home/pi/yourScript.log
BASH_XTRACEFD=$varFD

set -x

This will give you granular feedback on how the script operated, including how variables DID, or worse, DID NOT expand. Specimen output from one of my scripts from the log where it was captured:

+ sleep 10
++ readlink -f /home/pi/open-ipcamera-scripts/email-camera-address.sh
+ SCRIPTLOCATION=/home/pi/open-ipcamera-scripts/email-camera-address.sh
++ sudo sed -n 's/sysLocation.[[:space:]]*//p' /etc/snmp/snmpd.conf
+ CAMERALOCATION='Office Door'
++ sudo sed -n 's/sysContact.[[:space:]]*//p' /etc/snmp/snmpd.conf
+ SYSCONTACT=joe.blogs@gmail.com
++ ip addr list
++ grep inet
++ awk FNR==2
++ grep -oE '[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
+ CAMERAIPV4=192.168.1.22
++ cut -d / -f1
++ grep -P '^(?!fe80)[[:alnum:]]{4}:.*/64'
++ awk '{print $2}'
++ ip -6 addr
+ CAMERAIPV6=
+ msmtp joe.bloggs@gmail.com
++ echo 192.168.1.22
++ hostname
+ echo -e 'Subject: IP of Camera: 192.168.1.22\r\n\r\nIP Address of Office Door Camera pi3Bplus-camera1.example.com is: 192.168.1.22 /  '\''\n'\'' Script sending this email: /home/pi/open-ipcamera-scripts/email-camera-address.sh'

HTH-

F1Linux
  • 1,677
  • 1
  • 15
  • 32