Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to log errors to syslog? #2

Open
jmeile opened this issue Feb 16, 2022 · 1 comment
Open

Is it possible to log errors to syslog? #2

jmeile opened this issue Feb 16, 2022 · 1 comment

Comments

@jmeile
Copy link

jmeile commented Feb 16, 2022

Hi

I have a script like this:

#!/bin/bash
source 'shell-core/base/trap.bash'

#Do something here

If I run it from the console, the errors will show properly; however, I want to run the script from the crontab and I would like to log the errors (complete backtrace) to the syslog. Is this possible? How?

Thanks

Best regards
Josef

@jmeile
Copy link
Author

jmeile commented Feb 16, 2022

Ok, never mind, I found this solution:

#!/bin/bash
#DEBUG can be:
# 0 - the errors will be logged to the syslog and printed to the console
# 1 - the errors will be printed to the console
DEBUG=0

if [ "$DEBUG" = "0" ]; then
  echo -e "\n\n\n"
  echo -e "\t\t\t\t\t*********************************************************"
  echo -e "\t\t\t\t\tWith DEBUG=0, no colors will be displayed on the console."
  echo -e "\t\t\t\t\tIf you want collors, then use DEBUG=1; however note that"
  echo -e "\t\t\t\t\tnothing will be written to the syslog"
  echo -e "\t\t\t\t\t*********************************************************"
  echo -e "\n\n\n"
  exec 1> >(logger -s -t $(basename $0)) 2>&1
fi

#This supposes that shell-core is stored in the same folder that my script
SCRIPT_FOLDER=`dirname "$0"`
source "$SCRIPT_FOLDER/shell-core/base/trap.bash"

#Do something here

I don't know how to print color when logging to the logger, but I think I can live with that.

By the way, thanks to the update of the error handling script. I found you on your post in stackoverflow

Best regards
Josef

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant