Run MySQL command when booting the system

1

Other commands I was able to execute when booting the OS normally in these two ways:

1 - Creating a .sh file in the /etc/init.d/ folder and putting the command in.

2 - Placing at the end of file /etc/rc.local before exit 0 .

If I run the command on the line normally, it executes:

mysql -u usuario -psenha -Bse "USE intranet;set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"

Edit:

I tried another alternative that also did not work: I created a .sh script and tried to put it in cron ( @reboot /home/user/script.sh )

What am I doing wrong?

    
asked by anonymous 22.09.2017 / 19:50

3 answers

0

The "solution" I found was to create a cron to run this command every minute. The other alternatives did not work (possibly because it was running before MySQL was running).

    
22.09.2017 / 21:16
0

Check your rc.local (if the script needs to be run as root, you have to use sudo manually to run it).

Ensure that /etc/rc.local has execute permission and that the script also has execute permission:

$ ls -l /etc/rc.local
-rwxr-xr-x 1 root root 13 Jun 29 16:29 /etc/rc.local

Make sure rc.local also has #:

$ cat /etc/rc.local | head -n1
#!/bin/bash

(In case you use bash).

    
22.09.2017 / 20:09
0

In /etc/init.d, mount it as follows

#!/bin/bash ou #!/bin/sh como achar melhor
### BEGIN INIT INFO
# Provides:          (nome que vc deseja dar ao script)
# Required-Start:    $exemplo1 $exemplo3 $exemplo3 (aqui vc coloca quais serviços devem estar em execução antes do seu script ser rodado ou pode colocar $all para todos)
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: breve descrição
# Description:       Descrição completa
### END INIT INFO

#comando a ser executado

mysql -u usuario -psenha -Bse "USE intranet;set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"

Caution when using double quotation marks, if it has any special characters, it will be interpreted, before any special characters put \ (backslash) to escape.

Make the executable file chmod + x /etc/init.d/seu_script Put on startup update-rc.d your_script defaults update-rc.d seu_script enable if you are interested in a look at link

Using rc.local as follows

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.

#Comando a ser executado
mysql -u usuario -psenha -Bse "USE intranet;set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"
exit 0

Make sure it has execute permission, checks if rc.local is at system startup, if it is not using the following command

update-rc.d rc.local defaults
update-rc.d rc.local enable

Using cron

Create a file in /etc/cron.d/meu_cron and edit it as follows

*/1 * * * * root mysql -u usuario -psenha -Bse "USE intranet;set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"

Make sure the cron is running at system boot, if it is not put with the command

update-rc.d <serviço> <ação>

see more at link

    
06.10.2017 / 16:16