Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Each time Ubuntu is starting I need to run a script, which is running some SQLs. So I need run it after MySQL already started.

Where should I put such script?

share|improve this question
    
SysV init, upstart or systemd? –  Jan Sep 1 '14 at 9:51
    
I'm using Ubuntu. I have /etc/init.d/mysql –  Pavel Bernshtam Sep 1 '14 at 10:11

2 Answers 2

up vote 2 down vote accepted

file sould be place in /etc/init.d, and a link make in /etc/rc2.d (assuming you are in run level 2 (see who -r).

The link sould be in the form S99sql, all files in rc2.d are run in alphabetical order.

to sum up,

create tour file in /etc/init.d/mysqlcmd.sh

then change directory to /etc/rc2.d ans ln -s ../init.d/mysqlcmd.sh S99mysql

share|improve this answer
    
I do not see mysql in /etc/rc2.d Is it okay? How can I see when it is starting? –  Pavel Bernshtam Sep 1 '14 at 10:56

systemd

If the version of Ubuntu you're using is using systemd (14.04 onwards) then you could modify the .service file and add another script to be ran when MySQL is ready.

# vi /usr/lib/systemd/system/mysqld.service

Add another entry to:

ExecStartPost=/usr/libexec/mysqld-wait-ready $MAINPID

by appending a semicolon and the name of your script:

ExecStartPost=/usr/libexec/mysqld-wait-ready $MAINPID ; <path to my script>

Note that these scripts are not run using a shell, so you'll need to wrap yours in bash or similar if you need redirection etc.

The mysqld-wait-ready script does exactly what it says and waits until mysqld is ready to accept connections. Your script should therefore be able to run SQL on the server.

Upstart

For pre-14.04 versions of Ubuntu, you could create an upstart conf file

description     "My Post-MySQL Start Script"
author "Pavel Bernshtam"

start on started mysql

task
script
    # do my stuff here
end script

Save the file to /etc/init/<my script name>.conf

More information on upstart scripts is available from here

share|improve this answer
    
I'm using Ubuntu 12 :( –  Pavel Bernshtam Sep 1 '14 at 10:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.