Tuesday, July 5, 2016

Execute SQL script in Linux background - not closed after closing terminal

If is needed to execute some SQL script which may take too long and you want it to continue running even after closing the terminal it can be done via the following:

Create you SQL script.

Once it is loaded into server run the following example command:

nohup mysql -u user-ppassword < script.sql > your_outputfile.out&

Note: there is no whitespace between -p and the password!

Using the above, it will write the output into the file you have decided, in our case your_ouputfile.out

Once this has been executed you get the PID, then to avoid process to die after closing the terminal just use the "disown" command like this: disown -h PID

Afterwards you can safely close your terminal and your process will continue executing in background.