Mysql – Running a SQL query in the background using MySQL


Is there a way to have a SQL query run on the servers even after I turn off my computer?

I heard from a coworker that there was and she wanted me to do that. I believe she said it was called a "screen"?

Best Solution

I'm going to assume you are ssh-ing to a server and you want to be able to shutdown you local workstation.

You can use nohup and background processes. Also useful to know the -e option for the mysql command.

So something like this:

nohup mysql <options> -u <user> -p <pass> -e 'Insert Query Here' &
  • nohup is for 'no hang up' i.e. don't end the process when I disconnect.

  • mysql ...The mysql command you usually run to connect, just add the -e option to run the query without opening a mysql session

  • & starts the process in the background

I'm unsure about this BUT: If you don't want to put your password in the command, run the command without the password, type fg the password prompt should come back, enter your password, ctrl-z will send the process back to the background, then you can disconnect.