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.

2
  • 1
    serverfault.com/a/321926/59925
    – quanta
    Dec 17, 2011 at 17:19
  • If you go to a more modern distribution with systemd it will handle that for you and allow to journal log messages of each service.
    – eckes
    Jun 23, 2017 at 10:13

4 Answers 4

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.

0
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
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
3
  • Thanks, this is closer to what I was looking for. I know how to redirect I/O but I didn't know how to call into the logger.
    – McKAMEY
    Dec 17, 2011 at 18:11
  • Is [ -r ... ] a test to check if it exists?
    – McKAMEY
    Dec 17, 2011 at 18:14
  • Yes, check if the file is readable.
    – ott--
    Dec 17, 2011 at 18:37
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/.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .