Visit The New, Modern Unix Linux Community


how to run cron job to truncate database table


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to run cron job to truncate database table
# 1  
how to run cron job to truncate database table

hi

i'm a bit stuck trying to find the correct syntax in order to run a cron job which just truncates all info from a single table in my database

i have a main database - db1
and i have a table in that database - userips

i wish to create a cron job that truncates or removes all the info from the userips table everyday

can anyone help me in the correct syntax in order to create a cron job that runs every day at a certain time point

thanks in advance
# 2  
What database are you using?
# 3  
hi i'm using mysql
# 4  
Here is a script to run a sql command just replace the existing query with your truncate. and add script to cron:

Code:
TABLE_NAME=sometable
USER_NAME=someuser
IP_ADDR=localhost
PASSWORD=somepassword

somevar=`echo "select * from <table_name> where <condition>" | mysql -h $IP_ADDR -u $USER_NAME -p$PASSWD $TABLE_NAME `

echo $somevar

# 5  
hi just been playing around with the code listed

got around to this

Code:
TABLE_NAME=userips
USER_NAME=user
IP_ADDR=localhost
PASSWORD=password
somevar=`echo "TRUNCATE TABLE userips" | mysql -h $IP_ADDR -u $USER_NAME -p $PASSWORD $TABLE_NAME `
echo $somevar

just a few questions though

do i not need to add in my database name somewhere in there as i have around 20 databases on this server

also the somevar= part , is that looking correct or should that be placed after the mysql command

also finally what type of file do i save this as , should it be a .sh file, then do you no the correct syntax i should enter in cron to enable this to run every day



i used something like this to backup my database every day

Quote:
0 0 * * * /usr/bin/mysqldump -u user database -p pass > /var/www/vhosts/mysite.com/httpdocs/sql-backup/dbbackup.sql
but i'm not to sure how to modify this in order to change it to truncate or delete info from just one table

the userips table fills up very quickly with lots of ips(10,000 per day), thats why i need to delete hem every day

its a pain having to login to phpmyadmin every day to just click the truncate table button on the usrips table
# 6  
You shouldnt have to select the database table, ASSUMING the username you are using is only has access to the database you want the script to truncate. Saying that I would NOT use a master login with access to all your databases.

$somevar wil contain what mysql returned when the sql was run.

your contab woud be something like:
Code:
0 0 * * * /bin/sh /path/to/your/script >> /path/to/some/log

this will output what the output was when running the script to path/to/some/log
# 7  
thanks for the help ikon you have cleared up lots of questions and ponted me in the right dirrection

i will give that a try like you have mentioned, just on thing can i simply name the script with the .sh file ending, thanks for taking the time to answer my questions
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #431
Difficulty: Medium
JavaScript arrow functions were first introduced in 6th Edition - ECMAScript 2015.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job cannot run the command

Hi, I created this cron job for asterisk to send sms daily to a number #!/bin/sh #custom mod - send sms once a day, at 07:00. CRON_PATH="/etc/asterisk/gw/crontabs_root"; if ! grep 'gsm send sms' $CRON_PATH > /dev/null 2>&1 ;then echo "* 7 * * * asterisk -rx 'gsm send sms 1 7666... (4 Replies)
Discussion started by: jazzyzha
4 Replies

2. Shell Programming and Scripting

Cron Job to Run every 2 minutes

Hello Gurus, I want to schedule a cron job which will run every 2 minutes starts at 11:25 AM and 3:25 AM daily. Can you please suggest as how to schedule the job. Thanks- Pokhraj Das (2 Replies)
Discussion started by: pokhraj_d
2 Replies

3. Shell Programming and Scripting

Cron Job to Run for 30 minutes

Hello Gurus, I have a requirement to run a job from cron only for 30 minutes duration daily twice at 8.35 am and 20.35 pm Can you please suggest how to schedule the job? Thanks- Pokhraj (5 Replies)
Discussion started by: pokhraj_d
5 Replies

4. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

5. Shell Programming and Scripting

CRON Job to copy database and replace existing database

I have a reseller account with hostgator, which means i have WHM and Cpanel. I have set up a staging environment for one of my wordpress installations (client website), which is essentially sitting at staging.domain.com (live site is at domain.com). The staging website is a complete copy of the... (1 Reply)
Discussion started by: nzrobert
1 Replies

6. Shell Programming and Scripting

How to write cron job for calling sql function database is postgres

Hi, Please help me to write cron job for calling sql function daily. I have Postgres database. (1 Reply)
Discussion started by: kulbhushan
1 Replies

7. UNIX for Advanced & Expert Users

Use cron to run job every other week

my colleague was asking about this: is there a way to run a cron job biweekly, like a script five.sh to run every *OTHER* Friday within November its part about every other Friday we cant find any documentation. thx L (8 Replies)
Discussion started by: lydiaflamp
8 Replies

8. UNIX for Advanced & Expert Users

Need help with a script run by a cron job.

Hi, new to this forum and not sure if this is the right place to post. I'm new to cron jobs and scripts, and I need some help with a script to be used with a cron job. I already have a bot set up at a certain website address and need a script that the cron job will load every day that tells it to... (1 Reply)
Discussion started by: klawless
1 Replies

9. Shell Programming and Scripting

Does not run via cron job

I have a perl script, when I ran manually it works perfect. I check the permissions which are fine. How can I find out why it is not running? how can I setup a log to check. I am running it on solaris 9. It compares multiple files, SCP and then send out an e-mail. As I said when I ran it... (2 Replies)
Discussion started by: amir07
2 Replies

10. UNIX for Advanced & Expert Users

Run cron job problem

I have the below crontab job that it will run at every 7:00am - 10:00am , it work fine , now if I want to skip to run the crontab job at a specific time , eg. I want the script not to run at next Monday 8:00am ( only skip this time , other time is normal ) , how can I make it ? is it possible ?... (3 Replies)
Discussion started by: ust
3 Replies

Featured Tech Videos