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:
- are these error messages automatically captured somewhere?
- if not, how can I capture the stdout/stderr from my init.d scripts?
Thanks in advance.
No - they go to STDOUT (if you use
echo
) or STDERR (if you useecho >&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 atlogger
. You can combine them in any way you want to.Write a wrapper script that calls your script and redirects the output to a files
You could make a function to echo the message to both the screen and to syslog, something like this:
You could also put that into a separate file and include it into your scripts with
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:I found it in this great article: http://urbanautomaton.com/blog/2014/09/09/redirecting-bash-script-output-to-syslog/.