0

i was asked to write shell script to perform the following task:

update cronentry from

\*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out 

to

\*/30 2,4,6 * * * /bin/sh /opt/myscript/myscript2.sh > /tmp/myscript2.check.out 

before edit take a backup of the cronentry

i have tried the following script and need to check if it will work or does require any modification - please help!!

Shell Script to update cronentry(backup before edit)

#!/bin/sh
crontab -l > my_crontab.backup

crontab -l | grep -v "*/30 2,4,6 * * * /bin/sh /opt/myscript/myscript2.sh > /tmp/myscript2.check.out" >*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out

echo "*/30 2,4,6 * * * /bin/sh /opt/myscript/myscript2.sh > /tmp/myscript2.check.out" >>*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out

crontab */15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out

1 Answer 1

0

You wish to delete a particular crontab entry and replace it with another.

You may dump the existing crontab schedule to a text file using

crontab -l >crontab-old.txt

Assuming that you know that the existing crontab entry is exactly

*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out 

(with no additional spaces anywhere) you may filter that out from the crontab-old.txt file using

grep -v -x -F '*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out' crontab-old.txt >crontab-new.txt

To add the new schedule to the crontab-new.txt file, you could do

cat <<'END_ENTRY' >>crontab-new.txt
*/30 2,4,6 * * * /bin/sh /opt/myscript/myscript2.sh > /tmp/myscript2.check.out 
END_ENTRY

And finally, you load the new schedule with

crontab crontab-new.txt

You now have crontab-old.txt with the old schedule and crontab-new.txt with the new schedule, and you have loaded the new schedule.

You should obviously look at the files in-between each step above to make sure the correct edits are made to them, if you are running this manually, or while you are developing a script to do it.

In a script, this may look like

#!/bin/sh

crontab -l >crontab-old.txt

grep -v -x -F '*/15 * * * * /bin/sh /opt/myscript/myscript.sh > /tmp/myscript.check.out' crontab-old.txt >crontab-new.txt

cat <<'END_ENTRY' >>crontab-new.txt
*/30 2,4,6 * * * /bin/sh /opt/myscript/myscript2.sh > /tmp/myscript2.check.out 
END_ENTRY

crontab crontab-new.txt

The grep command uses -F to match the given pattern as a string rather than as a regular expression. The pattern contains * characters which are special in regular expressions, so using -F allows us to match the string without special handling of these. I'm also using -x which forces successful matches to match across a full line, from start to finish, rather than parts of lines.

Also related:

1
  • Thanks for the input
    – ravi
    Aug 8, 2019 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.