Run Mysql command when starting the system

Asked

Viewed 505 times

1

Other commands I managed to execute when Boot in the OS normally in these two ways:

1 - Creating a file .sh in the briefcase /etc/init.d/ and put the command inside.

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

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

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 script . sh and tried to put in cron (@reboot /home/user/script.sh)

What am I doing wrong?

3 answers

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 the /etc/rc.local has execution permission and that the script also has execution permission:

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

Ensure that rc.local also has the shebang:

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

(In case you use bash).

  • -rwxr-Xr-x 1 root root 305 Sep 22 17:28 /etc/rc.local e #! /bin/sh -e

  • Placing the command inside a sh (with execution permission) and placing this sh in rc.local works?

  • I created the sh file with execution permission (chmod +x) and ran it, it worked correctly (with Warning, just like when running from the command line)

  • If I create that same file in the /etc/init. d/ folder with these permissions, it doesn’t seem to run.. but if I run it manually, it works

  • Did you try to put on rc.local? bash /caminho/script.sh

  • Yes, I put in the rc.local also as said in the question and continued not executing..

Show 1 more comment

0

No /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';"

Be careful when using double quotes, if you have any special character, it will be interpreted, before any special character put (backslash) to escape.

Make the file executable chmod +x /etc/init. d/seu_script Put on startup update-rc. d seu_script defaults update-rc. d seu_script enable if you are interested in taking a look at http://www.debian.org/doc/debian-policy/#Contents

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 he has permission to execute, checks whether rc.local is on system startup if it is not using the following command

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

Using the 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 that cron is running on system boot if you are not running it with the command

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

see more in https://linux.die.net/man/5/crontab

0


The "solution" I found was to create a cron to run that command every minute. The other alternatives didn’t work (possibly because it was running before Mysql was running).

  • Actually Voce may be trying to run before the server is active. System startup follows an order and this order is controlled by System V, Consult HERE to understand how it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.