Basic Bash Script

Hi Experts,

I please need some direction for creating a basic bash script to run a sql query against a mysql database for nightly maintenance.

Sample info is displayed below.

DB name: test_db
Table name: test_table
SQL command: delete from test_table where id = 0

The site is hosted on a linux server. We have Plesk that we will use for executing the scheduled task.

Your help is very appreciated!

Thanks!
JFranks22Asked:
Who is Participating?
 
farzanjConnect With a Mentor Commented:
Try this
#!/bin/bash

table="test_db.test_table"

qry="delete from test_table where id = 0"
/usr/bin/mysql -u root << eof
$qry
eof

Open in new window

0
 
omarfaridConnect With a Mentor Commented:
to schedule the job you need to use crontab:

EDITOR=vi ; export EDITOR
crontab -e (this will open the crontab schedules for editing, you will use vi editor to edit this temp file)
you can add new schedule like one below

0 0 * * * /path/to/myscript

you may then save and exit

The above schedule is a sample which will run your script myscript at 12:00 AM every day (please see link below for crontab format)

the script myscript should be executable and should have in it all required env. variables set, etc.



http://linux.die.net/man/5/crontab
0
 
JFranks22Author Commented:
Hi farzanj,

I will work with your script - thanks! I'll provide feedback.

omarfarid, I should be good on the scheduling side using Plesk.

Thanks!
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
JFranks22Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for JFranks22's comment http:/Q_26986984.html#35518002

for the following reason:

thanks
0
 
omarfaridCommented:
there is no solution in the selected answer
0
 
farzanjCommented:
Based on author's comment http:#35518002, he appreciated http:#35497718.  Comment http:#35499222 suggests a valid way for scheduling.
0
 
omarfaridCommented:
Agree with farzanj
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.