]> git.pond.sub.org Git - empserver/blob - scripts/savecore
ship: Unbreak MOB_ACCESS real-time mobility update for ships
[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-2012
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.  Modern user space
22 # can squirrel away core dumps in fancy places; you may need to switch
23 # that off for this insufficiently fancy script to work.
24 core_pattern=core.*
25 #core_pattern=emp_server.core
26 #core_pattern=core
27
28 # Where to save core dumps
29 #
30 # If you leave cores in the data directory, backup scripts may pick
31 # them up, which is probably not what you want.
32 core_dir=../core-dumps
33
34 # Minimum free disk space for saving core dumps, in KiB
35 space_low=102400
36
37 # Whom to send mail (leave empty to not send any)
38 privlog=
39
40 # Program to send mail
41 mailx=mailx
42
43 # End of configuration
44
45 saved=
46 core_name=
47
48 alert_deity ()
49 {
50     local msg
51     if [ "$saved" ]
52     then msg="Core dump $saved_core saved."
53     elif [ "$core_name" ]
54     then msg="Could not save core dump $core_name."
55     else msg="Could not find core dump to save."
56     fi
57     echo "$msg" | $mailx -s "emp_server dumped core in $PWD" "$privlog"
58 }
59
60 test -n "$privlog" && trap 'alert_deity' EXIT
61
62 core_name=`ls -td $core_pattern 2>/dev/null | head -n 1`
63 test -n "$core_name"
64 test -r "$core_name"
65 tstamp=`/bin/date +%Y-%m-%d-%H:%M`
66 saved_core=$core_dir/core-$tstamp
67
68 mkdir -p $core_dir
69 if [ `df -kP $core_dir | awk 'NR!=1 { print $4 }'` -lt "$space_low" ]
70 then rm -f "$core_name"; exit
71 fi
72
73 mv -f "$core_name" $saved_core
74 saved=y