Shell Script to Delete Old Cron Jobs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to Delete Old Cron Jobs
# 1  
Old 03-28-2013
Shell Script to Delete Old Cron Jobs

I have a cron entry to deploy a website portal from a staging server to a series of production servers on a weekly basis.

On occasion, a random office worker who knows nothing about Linux let alone cron, will be tasked to update news picks on the staging server and then run a manual deployment to the production servers. So I threw together a quick PHP/JQuery script that will take the date and time chosen and insert a properly-formatted line into cron.

The trouble I'm having is finding a way to clean out the old one-run cron jobs. Rather than having the sys admin go in once in a while to clean out those old jobs, I'd like to have it deleted at the end of the run. It will take nearly an hour for the deployment to complete, so comparing the minutes passed will be enough.

What I want to do is using a bash script, read in a crontab line, find all lines with specific numbers for the date/time (no '*', ranges, multiples or '*/n'), and compare them to the current date. If in the past, delete the line.

Note there are lines that are comments or variable declarations, as well as the weekly deployment.

Any ideas?

Thanks,
Andrew

---------- Post updated at 01:04 PM ---------- Previous update was at 09:47 AM ----------

Nevermind, got it figured out. Bit of a hack but...

Code:
currYear=$(date '+%Y')
cronNow=$(date '+%Y-%m-%d %H:%M')
cronNowS=`date --date="$cronNow" +%s`
crontab -l > tempcron.txt
touch temp
while read -r line ; do
        #parse the line into an array by space
        IFS=' ' read -a cronJob <<< "$line"
        minute="${cronJob[0]}"
        hour="${cronJob[1]}"
        day="${cronJob[2]}"
        month="${cronJob[3]}"
        #if it's anything but numeric, it's not a single date...
        cronNumTest=$minute$hour$day$month
        if [[ $cronNumTest =~ ^[0-9]+$ ]]; then
                #numeric: convert to a date and compare to now.
                cronTime="$currYear-$month-$day $hour:$minute"
                cronTimeS=`date --date="$cronTime" +%s`
                if [[ $cronTimeS -gt $cronNowS ]]; then
                        echo "$line" >> temp
                fi
        else
                #leave the CONST declarations, comments and permanent cron jobs intact...
                echo "$line" >> temp
        fi
done < "tempcron.txt"
mv temp tempcron.txt
crontab < tempcron.txt
rm tempcron.txt

If anyone has a shorter, cleaner solution, I'd appreciate seeing your example code.

Cheers,
Andrew

Last edited by AndrewT; 03-28-2013 at 03:11 PM..
# 2  
Old 03-28-2013
Every cron line fires at least once in 28 years. Just look for months not 0-5 months in the future?
# 3  
Old 03-28-2013
Quote:
Originally Posted by AndrewT
The trouble I'm having is finding a way to clean out the old one-run cron jobs.
I don't quite get it: if you want to run a job exactly once, why do you (mis-)use cron instead of using at? The effort is the same, the format is even easier and you don't need any crontab-gymnastics afterwards.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to run multiple jobs and it's dependent jobs

I have multiple jobs and each job dependent on other job. Each Job generates a log and If job completed successfully log file end's with JOB ENDED SUCCESSFULLY message and if it failed then it will end with JOB ENDED with FAILURE. I need an help how to start. Attaching the JOB dependency... (3 Replies)
Discussion started by: santoshkumarkal
3 Replies

2. Shell Programming and Scripting

Calling multiple jobs from single shell script

Hi all, I have developed a shell script where I am executing datastage jobs.This script will be further placed in enterprise scheduler to trigger it at particulat time. I have around 300 jobs , I am passing jobnames to script as a paramere. So is it possible I can trigger all jobs from one... (3 Replies)
Discussion started by: prasson_ibm
3 Replies

3. Red Hat

Cron jobs

I'm running cronjobs on a redhat 5.X. Cronjobs are getting failed frequently so how to find the root cause (2 Replies)
Discussion started by: karthik9358
2 Replies

4. Shell Programming and Scripting

Shell script to schedule jobs

Dear all, I have to create a shell script which will run the job during weekday/weekend in following manner: - There are 3 jobs JB_1 JB_2 JB_3 These jobs changes its flag to "COMPLETED" in below 2 ways 1. These 3 jobs which runs after the completion of previous one i.e JB_1 runs and... (5 Replies)
Discussion started by: prajaktaraut
5 Replies

5. Solaris

Cron jobs and at jobs

There are two jobs in Solaris , Cron and at jobs.. I know how to disable or enable cron jobs. How can I enable at jobs and disable it. Kindly help. Rj (2 Replies)
Discussion started by: jegaraman
2 Replies

6. Shell Programming and Scripting

Script to get hung jobs in cron service.

Hi I want to have a list of hung jobs in crontab . e.g The below output gives a list of hung jobs including the running one. " USER1] ps -ef | grep 22345 root 22345 1 0 Nov 10 ? 0:09 /usr/sbin/cron user2 1061 22345 0 Nov 11 ? 0:00 sh -c /u01/user2s/b7111/movecsv.ksh oracle 20456 29535... (2 Replies)
Discussion started by: abdul bari
2 Replies

7. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

8. UNIX for Dummies Questions & Answers

Error while running the script through cron jobs .Please help me on this

Hi Everyone, I have written a korn shell script to shutdown my documentum docbase server and to restart it automatically on a weekly basis. My script is, When i execute this script manually, i was able to execute it successfully without any issues. I have scheduled this script in cronjob... (1 Reply)
Discussion started by: Sheethal
1 Replies

9. HP-UX

Cron jobs

i am new for cronjobs can someone please tell me what logic is behind these RED Numbers and stars below? --> crontab -l 00 1 * * * /home/scripts/TarprodContent > /tmp/MprodBkup.log 2>&1 00 1 * * * /home/scripts/TarTprodContent > /tmp/TprodBkup.log 2>&1 00 1 * * *... (5 Replies)
Discussion started by: ajadaun
5 Replies

10. UNIX for Advanced & Expert Users

cron jobs

I need to monitor my cron jobs with another unix machine since occasionally the cron will go down on the main server but there are no errors. Can anyone help with a script to write to use the cron on the back up machine to monitor the main server? I am using SCO and the cron jobs have been... (3 Replies)
Discussion started by: rmarral
3 Replies
Login or Register to Ask a Question