comment out a cron job as part of a script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

comment out a cron job as part of a script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-04-2009
22blaze 22blaze is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 20 December 2012, 1:34 PM EST
Location: NorCal
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
comment out a cron job as part of a script

Greetings,
I am creating a ksh script to automate the installation of a utility on many servers.
As part of this install, I want to check for a job in root's crontab.
If the job exists, I need to comment it out.
I know I will need to copy off the crontab then read it back in, but I am struggling on commenting the job out while maintaining the integrity of the crontab.
The job will have the same path, so this is a constant to check for.
The crontab entry might not reside on the same line for each server.
Any tips would be appreciated.
Thanks
Sponsored Links
    #2  
Old 03-04-2009
rikxik's Avatar
rikxik rikxik is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 October 2011, 2:20 AM EDT
Posts: 259
Thanks: 0
Thanked 2 Times in 2 Posts
You can copy off the crontab, grep for the job name, if found, put a hash at the beginning of the line (you can use sed) and write it back.

Show us the code you have tried so far.
Sponsored Links
    #3  
Old 03-04-2009
22blaze 22blaze is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 20 December 2012, 1:34 PM EST
Location: NorCal
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
I am aware of all those steps. It is the syntax of the sed command that I am struggling with. If I just grep for the command and issue the
Code:
sed 's/^/#/'
#0 10 * * 5 /my/cron/job

I only get that single line in my new file. I am not strong with sed and need some help with the [address]. I have tried these and get the same error,
Code:
$ crontab -l | sed [/my/cron/job]s/^/#/
Unrecognized command: [/my/cron/job]s/^/#/
$ crontab -l | sed '[/my/cron/job]s/^/#/'
Unrecognized command: [/my/cron/job]s/^/#/

    #4  
Old 03-04-2009
rikxik's Avatar
rikxik rikxik is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 October 2011, 2:20 AM EDT
Posts: 259
Thanks: 0
Thanked 2 Times in 2 Posts
Thats why you should start with posting code to save everyone's time. Here goes:


Code:
$ cat cron.sample
0 10 * * 5 /my/cron/job
0 10 * * 5 /other/cron/job

$ cat cron.sample |sed '/\/my\/cron\/job/s!^!#!'
#0 10 * * 5 /my/cron/job
0 10 * * 5 /other/cron/job

Sponsored Links
    #5  
Old 03-05-2009
22blaze 22blaze is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 20 December 2012, 1:34 PM EST
Location: NorCal
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Understood on the code posting. I will keep this in mind for future posts.
This works great. Thanks for the quick response, much appreciated.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pls comment on my script. sparcguy Shell Programming and Scripting 3 11-21-2008 03:18 AM
help needed for multiline comment deleting script aster007 Shell Programming and Scripting 1 10-10-2008 04:40 AM
comment jobs in cron for multiple accounts shibajighosh AIX 3 04-08-2008 10:52 AM
Block Comment in Shell script skyineyes Shell Programming and Scripting 3 07-02-2007 07:15 AM
Snapshot of part of the cron rooh UNIX for Dummies Questions & Answers 5 07-07-2001 05:30 AM



All times are GMT -4. The time now is 05:49 AM.