]> git.pond.sub.org Git - empserver/blob - scripts/savecore
9540da63c031e4a9cb88bd6b16da23751930d1ec
[empserver] / scripts / savecore
1 #!/bin/sh -e
2 # Script to save core files, to be run from post_crash_dump_hook
3 # Written by Markus Armbruster, 2008-2011
4 # This script is in the public domain.
5
6 # Use: edit configuration variables below to taste, then set
7 #   post_crash_dump_hook = /wherever/savecore
8 # in econfig.  Make sure to run the server with core dumps enabled
9 # (ulimit -c unlimited).
10
11 # Rules when running as post_crash_dump_hook:
12 # Data directory is working directory.
13 # stdin, stdout, stderr are redirected to /dev/null, except in debug
14 # mode
15
16 # Configuration
17
18 # How your system names core files
19 #
20 # This is a pattern rather than a name, because modern kernels can put
21 # fancy stuff in the name we can't always predict.
22 core_pattern=core.*
23 #core_pattern=emp_server.core
24 #core_pattern=core
25
26 # Where to save core dumps
27 #
28 # If you leave cores in the data directory, backup scripts may pick
29 # them up, which is probably not what you want.
30 core_dir=../core-dumps
31
32 # Minimum free disk space for saving core dumps, in KiB
33 space_low=102400
34
35 # Whom to send mail (leave empty to not send any)
36 privlog=
37
38 # Program to send mail
39 mailx=mailx
40 #mailx=mail
41
42 # End of configuration
43
44 saved=
45
46 alert_deity ()
47 {
48     local msg;
49     if [ "$saved" ]
50     then msg="Core dump $saved_core saved."
51     else msg="Could not save core dump."
52     fi
53     echo $msg | $mailx -s "emp_server dumped core in $PWD" "$privlog"
54 }
55
56 test -n "$privlog" && trap 'alert_deity' EXIT
57
58 core_name=`ls -td $core_pattern | head -n 1`
59 test -n "$core_name"
60 test -r "$core_name"
61 tstamp=`/bin/date +%Y-%m-%d-%H:%M`
62 saved_core=$core_dir/core-$tstamp
63
64 if [ `df -kP $core_dir | awk 'NR!=1 { print $4 }'` -lt "$space_low" ]
65 then rm -f $core_name; exit
66 fi
67
68 mv -f $core_name $saved_core
69 saved=y