bash wait for service to start

I'm having trouble with the first part of the bash script, I need to wait for server mysql to start before executing mysqadmin commands... I cannot reliable get it to work!

#!/bin/bash  if [[ ! -f /var/lib/mysql/status.secured ]]; then   echo "MariaDB -> secure"   #   /usr/sbin/service mysql start   while "$(mysql -u root -e "select 1")" -ne 1; do     sleep 1   done   #   mysqladmin -u root password "$MARIA_ROOT_PASSWORD"   mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$MARIA_ROOT_PASSWORD') WHERE User='root'"   mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"   mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"   mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"   mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "FLUSH PRIVILEGES"   touch /var/lib/mysql/status.secured   #   /usr/sbin/service mysql stop   while [[ "$(/usr/sbin/service mysql status)" != " * MariaDB is stopped." ]]; do     sleep 1   done   echo "MariaDB -> secured" fi 

It is ubuntu based docker container, phusion/baseimage

Replay

I think this will work:

#!/bin/bash

if [[ ! -f /var/lib/mysql/status.secured ]]; then
  echo "MariaDB -> secure"
  #
  /usr/sbin/service mysql start
  #give up if not started in 2min
  if mysqladmin --wait=120 -u root password "$MARIA_ROOT_PASSWORD"; then
    mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$MARIA_ROOT_PASSWORD') WHERE User='root'"
    mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
    mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"
    mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
    mysql -u root -p"$MARIA_ROOT_PASSWORD" -e "FLUSH PRIVILEGES"
    touch /var/lib/mysql/status.secured
    /usr/sbin/service mysql stop
    while [[ "$(/usr/sbin/service mysql status)" != " * MariaDB is stopped." ]]; do
      sleep 1
    done
    echo "MariaDB -> secured"
  else
    echo Gave up waiting for mysqld to respond
    /usr/sbin/service mysql stop
  fi

fi

Category: bash Time: 2016-07-29 Views: 4
Tags: bash service

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.181 (s). 12 q(s)