Basic Bash Script

Posted on 2011-04-30
Last Modified: 2013-12-26
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!

Question by:JFranks22
    LVL 31

    Accepted Solution

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

    Open in new window

    LVL 40

    Assisted Solution

    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.

    Author Comment

    Hi farzanj,

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

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


    Author Comment

    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:

    LVL 40

    Expert Comment

    there is no solution in the selected answer
    LVL 31

    Expert Comment

    Based on author's comment http:#35518002, he appreciated http:#35497718.  Comment http:#35499222 suggests a valid way for scheduling.
    LVL 40

    Expert Comment

    Agree with farzanj

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now