[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

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!
0
JFranks22
Asked:
JFranks22
  • 3
  • 2
  • 2
2 Solutions
 
farzanjCommented:
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
 
omarfaridCommented:
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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now