CRON Check
05-24-2010 @ 5:25AM EDT
This is a very simple bash script that will check for cron jobs that are set to run every minute and every other minute. This is helpful because having a script that runs every minute or 2 can be a strain on your resources if they are not properly configured and most of the time such scripts can get by with running every 5 minutes or more. 99% of the time you will find website based games that require a cron job run every minute, unfortunately this causes a huge issue with very active gaming sites when the cron jobs cannot process in time and you have multiple instances of the same job running at a time instead of one every minute. For this reason web based games should be watched closely as they were not designed to be run on a shared environment with limited resources.
How to use it:
If you don't already have a mydir folder then create it (mkdir /mydir). Then copy and paste the code into a new croncheck.sh file in the /mydir folder (vi /mydir/croncheck.sh). Don't forget to set your permissions to make the file executable (chmod 0771 /mydir/croncheck.sh). After that, copy and paste the code into a new cronchecker.sh file in the /mydir folder (vi /mydir/cronchecker.sh). Don't forget to set your permissions to make the file executable (chmod 0771 /mydir/cronchecker.sh). Now you can copy and paste the following line into your crontab (type crontab -e after you SSH into your server):
2 0 * * * /mydir/croncheck.sh
The above line will run the script every morning at 12:02AM.
You can adjust the time based on your other scheduled jobs because if you have a lot of accounts using cron jobs this can run for a while, otherwsie this should only take a minute or 2.
A breakdown of the croncheck.sh script:
#!/bin/bash <- Tells the server to run this script with the BASH interpreter.
sh /tools/cronchecker.sh > /var/log/cronchecklog.txt <- Runs the cronchecker.sh script seperately.
echo "Time: `date` and Uptime/Load: `uptime`" >> /var/log/cronchecklog.txt <- Prints the end time when finished to the cronchecklog.txt log (includes the uptime to keep track of your CPU load).
mail -s "CRON Check Results"
The cronchecker.sh script:
#!/bin/bash <- Tells the server to run this script with the BASH interpreter.
echo "Jobs running every minute:" <- Prints text to log.
grep -w "* * * * *" /var/spool/cron/* <- Finds all files with the text "* * * * *" in all of the cron directories.
echo "Jobs running every other minute:" <- Prints text to log.
grep -w "*/2 * * * *" /var/spool/cron/* <- Finds all files with the text "*/2 * * * *" in all of the cron directories.
Code:
#!/bin/bash
sh /tools/cronchecker.sh > /var/log/cronchecklog.txt
echo "Time: `date` and Uptime/Load: `uptime`" >> /var/log/cronchecklog.txt
mail -s "CRON Check Results" < /var/log/cronchecklog.txt
sh /tools/cronchecker.sh > /var/log/cronchecklog.txt
echo "Time: `date` and Uptime/Load: `uptime`" >> /var/log/cronchecklog.txt
mail -s "CRON Check Results"
Code:
#!/bin/bash
echo "Jobs running every minute:"
grep -w "* * * * *" /var/spool/cron/*
echo "Jobs running every other minute:"
grep -w "*/2 * * * *" /var/spool/cron/*
echo "Jobs running every minute:"
grep -w "* * * * *" /var/spool/cron/*
echo "Jobs running every other minute:"
grep -w "*/2 * * * *" /var/spool/cron/*
linux, servers, scripts, cron, tutorials
Comments
This article hasn't been commented yet.


Write a comment
* = required field