adding cron’s to crontab from a script

so i needed to add things to a couple of boxes without completely killing off the existing crontab from a script. i came up with this:

TMP_FILE=$(mktemp --tmpdir cron.$$.XXXXXXXXXX)
crontab -l > ${TMP_FILE}
echo '# * * * * * script' >> ${TMP_FILE}
crontab ${TMP_FILE}
rm -rf ${TMP_FILE}

it will take a copy of the existing crontab, then append the new line at the end, then push the temp file to cron. its a hacky way of doing it but it can be refined.

if you need to blow away your crontab and just add one line, you can do that by doing this:
echo '# * * * * * script' | crontab -
this will remove your existing crontab and replace it with whatever you specify.

«
»

    Leave a Reply

    Your email address will not be published. Required fields are marked *