Sponsored Content
Top Forums Shell Programming and Scripting Shell Script to Delete Old Cron Jobs Post 302786889 by AndrewT on Thursday 28th of March 2013 02:04:03 PM
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..
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
queuedefs(4)							   File Formats 						      queuedefs(4)

NAME
queuedefs - queue description file for at, batch, and cron SYNOPSIS
/etc/cron.d/queuedefs DESCRIPTION
The queuedefs file describes the characteristics of the queues managed by cron(1M). Each non-comment line in this file describes one queue. The format of the lines are as follows: q.[njobj][nicen][nwaitw] The fields in this line are: q The name of the queue. a is the default queue for jobs started by at(1); b is the default queue for jobs started by batch (see at(1)); c is the default queue for jobs run from a crontab(1) file. njob The maximum number of jobs that can be run simultaneously in that queue; if more than njob jobs are ready to run, only the first njob jobs will be run, and the others will be run as jobs that are currently running terminate. The default value is 100. nice The nice(1) value to give to all jobs in that queue that are not run with a user ID of super-user. The default value is 2. nwait The number of seconds to wait before rescheduling a job that was deferred because more than njob jobs were running in that job's queue, or because the system-wide limit of jobs executing has been reached. The default value is 60. Lines beginning with # are comments, and are ignored. EXAMPLES
Example 1: A sample file. # # a.4j1n b.2j2n90w This file specifies that the a queue, for at jobs, can have up to 4 jobs running simultaneously; those jobs will be run with a nice value of 1. As no nwait value was given, if a job cannot be run because too many other jobs are running cron will wait 60 seconds before trying again to run it. The b queue, for batch(1) jobs, can have up to 2 jobs running simultaneously; those jobs will be run with a nice(1) value of 2. If a job cannot be run because too many other jobs are running, cron(1M) will wait 90 seconds before trying again to run it. All other queues can have up to 100 jobs running simultaneously; they will be run with a nice value of 2, and if a job cannot be run because too many other jobs are running cron will wait 60 seconds before trying again to run it. FILES
/etc/cron.d/queuedefs queue description file for at, batch, and cron. SEE ALSO
at(1), crontab(1), nice(1), cron(1M) SunOS 5.10 1 Mar 1994 queuedefs(4)
All times are GMT -4. The time now is 10:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy