30

In Linux, I've written some scripts to be executed during boot and played around with the various ways of installing them. For larger scripts I'll put in /etc/init.d and link the appropriate /etc/rc.d/rc?.d runlevels. For smaller scripts, I'll append to /etc/rc.d/rc.local. This process seems to be running smoothly.

Now I've tweaked one of my scripts and it is failing. I'm having a heck of a time diagnosing it because I can't seem to capture the error output. I've checked /var/log/messages and poked around the rest of /var/log but can't find anything of use.

Does anyone know:

  1. are these error messages automatically captured somewhere?
  2. if not, how can I capture the stdout/stderr from my init.d scripts?

Thanks in advance.

quanta
  • 50,327
  • 19
  • 152
  • 213
McKAMEY
  • 635
  • 1
  • 6
  • 13

4 Answers4

18
  1. No - they go to STDOUT (if you use echo) or STDERR (if you use echo >&2).

  2. Your scripts have to write to logs and/or syslog on their own (your distribution might contain some init.d functions that might help there - add your distribution to your question).

If you go for logs look for the tee command. If you go for syslog look at logger. You can combine them in any way you want to.

Otiel
  • 105
  • 5
Nils
  • 7,657
  • 3
  • 31
  • 71
9

Write a wrapper script that calls your script and redirects the output to a files

#!/bin/bash

    /path/to/your/script &>/path/to/logfile
user9517
  • 114,104
  • 20
  • 206
  • 289
7

You could make a function to echo the message to both the screen and to syslog, something like this:

LOGGER="/usr/bin/logger -t $myScript"    # check the location of logger for your system

myEcho () {
    echo "$1"
    $LOGGER "$1"
}

You could also put that into a separate file and include it into your scripts with

#!/bin/bash
myScript=$(basename $0)
[ -r /myFunctions/myecho ] && . /myFunctions/myecho
ott--
  • 1,081
  • 1
  • 11
  • 13
6

Messages from init scripts are generally not captured anywhere. Thus, you need to implement a way to do it yourself. A good idea is to use logger to send all output to syslog. This example will send stdout and stderr to syslog:

exec 1> >(logger -s -t $(basename $0)) 2>&1

I found it in this great article: http://urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/.

qugu
  • 61
  • 1
  • 3