Merge branch 'master' of mathias-kettner.de:omd
[omd.git] / packages / mysql-omd / skel / etc / init.d / mysql
blob805b142d7a7ed4bcece6056fed7f157af0a0bed9
1 #!/bin/bash
3 cd ###ROOT###
5 # Check if MySQL is enabled in OMD. If not terminate.
6 . lib/omd/init_profile
7 . etc/omd/site.conf
8 if [ "$CONFIG_MYSQL" != on ] ; then
9 exit 5
12 MYSQLD_PID=tmp/run/mysqld/mysqld.pid
13 MYSQL_CNF=.my.cnf
15 ## Checks if there is a server running and if so if it is accessible.
17 # check_alive insists on a pingable server
18 # check_dead also fails if there is a lost mysqld in the process list
20 # Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
21 mysqld_status () {
22 ping_output=`mysqladmin --defaults-file=$MYSQL_CNF ping 2>&1`; ping_alive=$(( ! $? ))
24 ps_alive=0
25 if [ -f "$MYSQLD_PID" ] && ps `cat $MYSQLD_PID` >/dev/null 2>&1; then ps_alive=1; fi
27 if [ "$1" = "check_alive" -a $ping_alive = 1 ] ||
28 [ "$1" = "check_dead" -a $ping_alive = 0 -a $ps_alive = 0 ]; then
29 return 0
30 else
31 if [ "$2" = "warn" ]; then
32 echo -e "$ps_alive processes alive and 'mysqladmin --defaults-file=$MYSQL_CNF ping' resulted in\n$ping_output\n"
34 return 1
38 mysql_wait_start() {
39 if mysqld_status check_alive nowarn; then
40 echo -n "already running. "
41 return 1
42 else
43 mysqld_safe --defaults-file=$MYSQL_CNF >/dev/null 2>&1 &
44 i=0
45 while mysqld_status check_dead nowarn; do
46 if [ $i = '10' ]; then
47 return 1
48 else
49 echo -n "."
50 i=$(($i+1))
51 sleep 1
53 done
54 return 0
58 mysql_wait_stop() {
59 if mysqld_status check_dead nowarn; then
60 echo -n 'Not running. '
61 return 0
64 mysqladmin --defaults-file=$MYSQL_CNF shutdown
66 # wait until really stopped
67 i=0
68 while ! mysqld_status check_dead nowarn; do
69 if [ $i = '10' ]; then
70 return 1
71 else
72 echo -n "."
73 i=$(($i+1))
74 sleep 1
76 done
78 return 0
81 check_install_db() {
82 if [ ! -e ~/var/mysql/mysql ] ; then
83 echo "first start of MySQL, creating initial database..."
84 mysql_install_db --defaults-file=$MYSQL_CNF > $OMD_ROOT/var/log/omd.log 2>&1
85 if [ $? != 0 ]; then
86 echo "failed: please have a look at $OMD_ROOT/var/log/omd.log"
87 echo "and http://omdistro.org/wiki/omd/FAQ"
88 exit 1
93 __init_hook $0 $1 pre
94 case $1 in
95 start)
96 echo -n "Starting MySQL... "
97 check_install_db;
98 if mysql_wait_start; then
99 echo 'OK.'
100 __init_hook $0 $1 post 0
101 exit 0
102 else
103 echo 'ERROR.'
104 __init_hook $0 $1 post 1
105 exit 1
108 stop)
109 echo -n "Stopping MySQL... "
110 if mysql_wait_stop; then
111 echo 'OK.'
112 __init_hook $0 $1 post 0
113 exit 0
114 else
115 echo 'ERROR.'
116 __init_hook $0 $1 post 1
117 exit 1
120 restart)
121 $0 stop
122 $0 start
124 reload)
125 mysqladmin reload
127 status)
128 if mysqld_status check_alive nowarn; then
129 echo "$(mysqladmin version)"
130 else
131 echo "MySQL is stopped."
132 exit 3
136 echo "Usage: $0 {start|stop|restart|reload|status}"
138 esac