Catching errors in a shell scripts

Simon Skogh simon at swedishdrunkard.com
Sun Apr 8 00:58:11 UTC 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm sure the subject line is a tad confusing, but I'm having problems
coming up with a better description.

To cut to the chase, what I'm trying to achieve is executing a command
from within a shell script and trap it in a variable. This works just
fine using something like
OUTPUT=`command`
but it doesn't seem to grab any errors.

To get a frame of reference, here is what I'm using these scripts for:
I have a server (at home) that runs several different tasks throughout
the day, the majority fully automated using scripts and cron. A
handful of these tasks I want to keep my eye on, but instead of
logging on to the machine and checking the mail (or have it forwarded)
I came up with the idea to simply store the output in a MySQL-database
and display it on a simple status page for the server. This page shows
the current status of the system (temp, disk space etc) as well as the
recent outputs from the tasks it has performed (which are now stored
in the database).

Now, I'm sure this is not an optimal solution, but on this tiny scale
it works fine and I also saw it as a learning challenge since I'd
never really tried to connect shell scripts and SQL before.

Now, to return to the issue, these commands are wrapped in a text body
identifying tasks, times etc and the actual output is placed in <pre>
tags for easy spotting. If a task fails (say for instance a run of
ntpdate fails because of my unreliable ISP) it is not grabbed and
stored in the DB, but rather sent as a mail (only the actual command
output that is, the message body, along with empty <pre> tags are
stored in the DB).

Here's the basic structure of the scripts: A task is performed in a
shell script, capturing the output (see above) and generating a
message body and putting it all together nicely. Once the script has
finished all it's supposed to do, it calls the SQL-script, passing
along whatever data it has generated. The SQL-script then simply
inserts the data and the complete run of the two scripts end.


So to sum it all up into one single sentence, how do i make
VARIABLE=`command` catch error messages when "command" fails after
being executed by a cron job?


Best regards and with hopes of at least making some sense,
Simon
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGGD4jz+/vk9aBZhMRApGYAJ9C62zEUXrYpczUTecK/l4bkf4magCfZjld
k7ZHSlZ6qPdKqHdueK3s0Yg=
=ZXL5
-----END PGP SIGNATURE-----





More information about the ubuntu-users mailing list