Comment/uncomment a cron

Hi,

My requirement is to comment/uncomment a cron job through a script.

  1. Redirected the output of crontab -l to a text file.
    crontab -l >cronoutput.txt

  2. grep to find the script running and sed to place the comment (#) as
    the first char

    grep -i 'weblogicmonitor.sh' cronoutput.txt | sed 's/^/#/'

  3. I could see the output on the screen,

However my requirement is to keep the "cronoutput.txt" file as it is and place a comment where the 'weblogicmonitor.sh' is located.

Can someone suggest how to comment/uncomment the cron job.

If your version of sed supports it, use -i (inline editing). Otherwise, create a temporary copy of the file, grep & sed that and pipe the result to cronoutput.txt.

Hi Pludi,

thnx for your reply, my unix version doesn't support -i, I am able to grep the script and do a sed on it but my result is a single line, I wan all my old entires in cronoutput.txt too.. any further suggestions !!

Maybe Perl is an option:

perl -i -pe 's/^/#/ if /weblogicmonitor.sh/i;' cronoutput.txt

Maybe this:

grep -i 'weblogicmonitor.sh' cronoutput.txt | sed 's/^/#/' > _c  &&  mv -f _c cronoutput.txt

Oops. That was hasty. Maybe this:

sed  '/weblogicmonitor.sh/s/^/#/' cronoutput.txt > _c  &&  mv -f _c cronoutput.txt

An important point here is that after the crontab file is modified, it should be deployed to take the effect.