Creating Cronjob using .sh file

Hi all,

I have created .sh file which contains oracle's export command to export the tables for backup.

Also, used crontab for scheduling the above.

currently, cronjob is defined manually using crontab -e command from the root user, but
want to know how to write a shell script which on running by the support team will schedule the cronjob automatically.?

thanks

I guess crontab is just like any other normal file so you should be able to edit it from other scripts without any issues.

It's not that simple, because cron would not know about the changed file. You have to use the crontab command to change the crontab file and signal cron to reread the file. The crontab command reads $EDITOR to find out, which editor to start to edit a temporary copy of the crontab file. If the editor exits with exit code 0, the temporary copy is used to overwrite the real crontab file and cron is signalled to reread it.

This can be used to call a shellscript to modify the temporary copy, let's say to add a new entry.

First you need a shellscript like this (the name of the temporary file is passed as argument):

#!/bin/ksh
print "0 0 * * 0 echo Hello world" >>$1

Save this script as change_crontab.sh and execute it:

$ crontab -l
# my crontab
$ EDITOR=./change_crontab.sh crontab -e
$ crontab -l
# my crontab
0 0 * * 0 echo Hello world