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.
*/5 5-7 * * * mysql -ufoo --password='' -te "show full processlist" > /home/foo/log/show_processlist.log.`date +\%Y\%m\%d-\%H\%M` 2>&1
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
mysql
action every 5 seconds, then you need to run it 1440 times.
So, you could try something like the following:
#!/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
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:
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.
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:
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:
mysql -ufoo --password='' -te "show full process list" >> /home/foo/log/show_processlist.log.`date +%Y%m%d-%H%M` 2>&1