Hot answers tagged error-handling
11
I am not sure what shell sh.exe provides (since there are multiple shells that use that name for their Windows executables), but if it is bash or similar, you can use the $PIPESTATUS array. For your example, you would do:
g++ -c source.cpp -o source.o 2>&1 | perl /bin/gSTLFilt.pl
echo "${PIPESTATUS[0]}"
9
One common way is:
die() {
echo "$*" 1>&2
exit 1
}
then you use it like this:
mkdir -p some/path || die "mkdir failed with status $?"
Or if you want it to include the exit status, you could change it to:
die() {
echo "FATAL ERROR: $* (status $?)" 1>&2
exit 1
}
and then using it is a bit easier:
mkdir -p some/path || ...
8
This is a broad ranging question and can probably only be answered with the same sorts of brushstrokes. Ultimately, it comes down to what you, the user, wish to protect.
Fundamentally, you should not post anything that would allow someone else to find it easier to compromise your system, or any of the other connected tools or accounts you use. For this ...
8
I think you want the trap function, specifically:
error_func()
{
echo 'An error occurred!'
exit 1
}
trap error_func ERR
Errors later will jump to the function. This is supported by at least bash, zsh, and ksh.
8
Bash has an option pipefail:
The return status of a pipeline is the exit status of the last command,
unless the pipefail option is enabled. If pipefail is enabled, the
pipeline's return status is the value of the last (rightmost) command
to exit with a non-zero status, or zero if all commands exit success-
fully.
So:
set -o pipefail && ...
8
The error codes aren't from make: make is reporting the return status of the command that failed. You need to look at the documentation of each command to know what each status value means. Most commands don't bother with distinctions other than 0 = success, anything else = failure.
In each of your examples, ./dpp cannot be executed. When this happens, the ...
6
You could rewrite your code like this:
#!/bin/bash
function try {
"$@"
code=$?
if [ $code -ne 0 ]
then
echo "$1 did not work: exit status $code"
exit 1
fi
}
try mkdir -p some/path
try cd some/path
try run_some_command
If you don't actually need to log the error code, but just whether the command succeeded or not, you ...
6
Well, there are a couple of cases:
This disk is part of a RAID array. Good. Just have md 'repair' the array like this: echo 'repair' > /sys/block/md0/md/sync_action. Problem solved without data loss. (I'm guessing this isn't the case for you, but you really ought to consider changing that.)
You don't care about the data on the disk (or there isn't any). ...
6
It means that there are 22 sectors that could not be read. The next time you write to those sectors, if they can not be correctly written to, they will be remapped to a spare sector. You can use the badblocks utility to locate the bad sectors, and dd to write to them:
sudo badblocks -b 512 /dev/sda
For each sector listed, first verify that it can not be ...
6
In traditional shells, the status of the first command in a pipeline is not reported at all to the script. Only the status of the last command is available, in $?.
In bash ≥3.0, when you want to do is stop if an error occurs anywhere in the pipeline, use the pipefail option.
g++ -c source.cpp -o source.o 2>&1 | perl /bin/gSTLFilt.pl
More ...
5
I'm assuming your cron email settings are all correct, and you otherwise get emails.
Your sending all stdout to /dev/null, so anything that prints error messages must output them to stderr. You might want to make sure everything in the script is outputting correctly.
At times I have had to work with third party code, which was sending everything to ...
5
There's a simpler way of what you're doing. If you use set -x, the script will automatically echo each line before it's executed.
Also, ss soon as you execute another command, $? is replaced with the exit code of that command.
You'll have to back it up to a variable if you're going to be doing anything with it other than a quick test-and-forget. The [ is ...
4
There are standard error values, defined in errno.h. You can look at this file on your system to see the numerical values. On most systems, they're in /usr/include/errno.h or a file that it includes. On Linux, most are in /usr/include/asm-generic/errno-base.h or /usr/include/asm-generic/errno.h, with a few more in /usr/include/bits/errno.h.
If you have a ...
4
From the shell, you can run perror:
$ perror 123
OS error code 123: No medium found
That comes with MySQL.
If you don't have MySQL, you can use Perl or Python, e.g.:
$ perl -MPOSIX -e 'print strerror(123)'
No medium found
$ python -c 'import os; print os.strerror(123)'
No medium found
In a C program you can use the function with the same name:
...
4
If you really want to exit on an error and are using Bash, then you should also consider set -e. From help set:
-e Exit immediately if a command exits with a non-zero status.
This of course doesn't give you the flexibility of a did_it_work() function, but it is an easy way to make sure your bash script stops on an error without adding lots of calls ...
4
You can redirect the error output to a file and then retrieve that output:
trap "rm -f /tmp/cfn-error.txt" 0 1 2 3 15
/opt/aws/bin/cfn-init -s ... 2>/tmp/cfn-error.txt ||
error_exit $(</tmp/cfn-error.txt)
You should always clean up your mess, so don't forget to delete any temp files you create.
3
When your PC has more than 4 GB of memory, but has also some devices that support only 32-bit addresses, any I/O from or to these devices must be mapped to somewhere in the low 4 GB range.
Typically, a range of 64 MB is allocated for this.
"Out of SW-IOMMU space" means that either
you are doing so much I/O that you need more than 64 MB of buffers at the ...
3
There are two aspects: the ways system calls signal that an error occurred, and the way what error occurred is reported.
Most system calls signal that an error occurred by returning -1, but this is not completely universal (for example, some system calls are always successful, e.g. getpid).
If you know an error occurred, the error code is always in errno¹. ...
3
The current pending sector count is the number of sectors the disk is currently tracking that it has not been able to read. With luck, they'll be written, and then the disk will remap them. But until it reads them successfully or the computer tries to write to them, there's nothing it can do. This is a possible sign of more problems to come.
You can read ...
2
So you want to do fault injection on file accesses. Two techniques come to mind: using LD_PRELOAD to overload the read function in the library to return an error instead of doing its job in certain conditions; or accessing files on a FUSE that mirrors an existing filesystem, but artificially fails in certain conditions.
Looking for fault injection software, ...
2
First of all:
The logs for apache are set in the httpd.conf file.
And the logs for PHP (if any) are set in the php.ini file.
For the case of PHP, you have to look at the php.ini file, and look for log_errors and error_log variables, that must have these values:
log_errors = On
error_log = /tmp/php_error.log
the last value (/tmp/php_error.log) is just ...
2
For rsync, it has a --no-motd you can use to suppress the motd for your script and only see the output of what rsync is transferring. It looks like it was added in rsync 3.x since I don't see that option in 2.x.
The man page lists a caveat about not using it when trying to get a listing of modules you can rsync:
--no-motd
This option affects the ...
2
Rsync doesn't have an option for this. I see two solutions. One is to parse rsync error messages; this isn't very robust. The other is to generate a list of unreadable files to filter.
cd /source/directory
exclude_file=$(mktemp)
find . ! -readable -o -type d ! -executable |
sed -e 's:^\./:/:' -e 's:[?*\\[]:\\1:g' >>"$exclude_file"
rsync -rlptD ...
2
The basic rule should be to expose only necessary information.
So here the basic security rules apply:
Only as much access/information as needed
Anything else should be forbidden/not readable
As you can see from many questions asked here - comments will ask for further information, if needed. But it is up to you to obfiscate personal information about ...
2
Adding to the other two (great) answers, it's also good to realize that the process of factor separation, so important for good testing/troubleshooting, is somehow related to the act of removing the sensitive info.
In other words, whenever possible, always try to replicate your problem in a separate environment. Apart from the advantage of ruling out ...
2
Just complement the other answers that show you what types of things to anonymize in logs I thought I'd provide a list of tools that can be used to help facilitate anonymizing the logs.
TCPDUMP/pcap
The list is primarily tools for dealing with tcpdump/pcap logs. NOTE: The full list of tools and libraries is here.
AnonTool
Netflow (v5 and v9) traces in ...
1
Store the output in a variable or a temporary file, and print it only if the return status is nonzero.
59 * * * * errors=$( { cd FQNameOfRepo && git pull && make doc-all && cp doc/latex/refman.pdf doc/html/; } 2>&1 >/dev/null) || { ret=$?; echo "$errors"; exit $ret; }
(You may want to put this rather long one-liner in a ...
1
Relying on the mailing capabilities of crond too much may yield various problems. Depending on your your crond they are perhaps just not flexible enough.
For example, often, as you described, one cannot configure that only an exit status != 0 should trigger the mailing of stdout/stderr. Another issue is that, for example, the Solaris crond has a ...
1
There is no reason to use tee or script. Unless you are looking to do the analysis after the fact and offline.
script will capture the text output of your shell session and put it into a file typescript by default but not necessarily. Appending or overriding is up to you.
tee allows you to dump output of the running command to a file and to allow it to ...
1
Apparently my setup doesn't work with the featured 1.0.14 from code.google.com
I used the 1.0.12 version as such:
go to eID quick install page
download their most recent .deb (contains Firefox eID add-on 1.0.12)
close all Firefox instances
connect USB reader to computer
insert eID card into reader
start browser
ignore message that eID add-on could not ...
Only top voted, non community-wiki answers of a minimum length are eligible