Cron job for MySQL process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job for MySQL process
# 1  
Old 05-06-2016
Cron job for MySQL process

Hi,

How can I write a cron job that runs the MySQL "show processlist" command and stores in log file every 5 seconds between 5 am to 7 am?

I know the lowest possible timing I can have in cron is a minute not second. If I need a script, I am looking for a solution in Bash.

I think this cron job works for every 5 minutes between 5 am to 7 am.
Code:
*/5 5-7 * * * mysql -ufoo --password='' -te "show full processlist" > /home/foo/log/show_processlist.log.`date +\%Y\%m\%d-\%H\%M` 2>&1


Last edited by vbe; 05-06-2016 at 01:26 PM.. Reason: code tags
# 2  
Old 05-06-2016
Hello there!
Maybe you could use a shell script in order to accomplish that task.
This is how I thought about it:

From 5 a.m to 7.am there are 2 hours, then 7200 seconds.
You want to run the
Code:
mysql

action every 5 seconds, then you need to run it 1440 times.

So, you could try something like the following:

Code:
#!/bin/bash

for i in {1..1440}; do
  mysql -ufoo --password='' -te "show full processlist" > /home/foo/log/show_processlist.log.`date +\%Y\%m\%d-\%H\%M` 2>&1
  sleep 5;
done

Say you save this script as filename
Code:
mysql_log.sh

Then, all you have to do now is to configure your crontab to run this script at exactly 5 a.m (one time only). Something like this:

Code:
0 5 * * * mysql_log.sh 2>&1

That action will last till 7 am.

Hope it helps and I hope someone points a better solution too.
# 3  
Old 05-06-2016
How long does it take to run your mysql command? How much variation is there in the time it takes to run your mysql command?

If you want to run your command 12 times/minute, don't you want the seconds in output filename? As in:
Code:
mysql -ufoo --password='' -te "show full process list" > /home/foo/log/show_processlist.log.`date +%Y%m%d-%H%M%S` 2>&1

If not, the output from the first 11 times you run it will be overwritten by the last time it runs in that minute?

Or, didi you intend to store the output of running your command 12 times in each file with the output from later runs appended to the file instead of replacing the previous contents? As in:
Code:
mysql -ufoo --password='' -te "show full process list" >> /home/foo/log/show_processlist.log.`date +%Y%m%d-%H%M` 2>&1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Commented cron job -- cron monitoring

Hi I have a requirement to write a shell script,that will check the all commented job in cron job.Please help !! (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Solaris

Cron job running even after cron is removed

Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And... (4 Replies)
Discussion started by: chidori
4 Replies

4. Shell Programming and Scripting

Cron job and shell script to kill a process if memory gets to high

Hello, I'd like to set a cron job that runs a shell script every 30 minutes or so to restart a java based service if the memory gets above 80%. Any advice on how to do this? Thanks in advance! - Ryan (19 Replies)
Discussion started by: prometheon123
19 Replies

5. UNIX for Dummies Questions & Answers

cron job is unable to process

Hi All, I have got a shell script that excutes some job and mails me the output as an attachment. While running the script manually, its perfect. when i am scheduling the job through crontab, i am getting the mail. but the attachment but this is a blank file. after the scheduler run, i can... (2 Replies)
Discussion started by: gotam
2 Replies

6. Solaris

Cron Job -- auto start process when it dies

I would like to setup a Cron job to check weather X process is running or not. if it is not running then start that X process with a log message.... can any one help writing a script? thanks (3 Replies)
Discussion started by: chandravadrevu
3 Replies

7. Solaris

cron job starts new cron proccess

I run cron in solaris 10 zone. One cron job which syncing files to nfs mounted on container, creates after finishing another cron proccess(/usr/sbin/cron), and after 100 existing cron proccesses next cron job will not start. It's too weird for me, I'm not able to solve this problem. Theoretically... (3 Replies)
Discussion started by: ron76
3 Replies

8. UNIX for Dummies Questions & Answers

CRON usage for CRON job

can anybody explain the usage of CRON for adding a cron job. please provide an example also for better understanding !!! Thanks (1 Reply)
Discussion started by: skyineyes
1 Replies

9. Solaris

killing a unix job after the job process gets completed

Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. Thanks... (7 Replies)
Discussion started by: dtazv
7 Replies

10. Shell Programming and Scripting

killing unix job after the job process completes

Hi, Thanks in advance. i need to kill a unix background running job after that job process completes. i can kill a job by giving the following unix command kill -9 processid how to kill the job after the current process run gets completed ? Appreciate your valuable help. ... (1 Reply)
Discussion started by: dtazv
1 Replies
Login or Register to Ask a Question