Linux: Debugging Bash

Everything that you type plus the result is logged to a text file that you can then use to go back and see what happen. This can be very useful when something isn't working correctly and you need to debug it.

script mylogfile
echo "This is recored"
echo "So is this"
exit

Everything you type plus the result is recorded to mylogfile.

 Logging using rsyslog

A job that runs regularly at a scheduled time such as backup script can be logged to it's own log file using rsyslog. To redirect the output to it's a separate log file create a file called /etc/rsyslog.d/30-databasebackup.conf that looks something like this. If you don't do this then it we be added to /var/log/syslog

if $programname == 'databasebackup' or $syslogtag == 'databasebackup' then /var/log/databasebackup.log
& stop

Change the tag and output files to suit your script.

sudo systemctl restart syslog.service

 Restart rsyslog

databasebackup.sh | logger -t 'databasebackup'

Run you script