I need to modify a line from the root crontab file... Usually, if I need to do this I use the crontab -e, which opens up the file with Vi, then I edit it and there's no problem.
Now, I need to do this but on many servers at the same time... Like in 200. I know how to execute the script on them all, but what I'm thinking about is how can I modify these crontab files, since as far as I know it's kinda unhealthy to directly modify them from /var/spool/cron/crontabs/root. So I can't use my sed/awk commands over these files...
My idea is to run the little script from a NIM on all servers.
crontab can also accept a file argument, just crontab filename, to replace the current cron table with the contents of filename. crontab - will read from standard input instead of a file. If you're appending something to a crontab, something like:
crontab -l > /root/cron-backup-$$
( cat /root/cron-backup-$$
echo "# Comment line for new entry"
echo "0 2 * * * /path/to/command argument" ) | crontab -
That's nice. I didn't know I could tell crontab to use another file.
I don't have to append anything, actually, I have to modify (not add) a line in the middle of the crontab file, which I would do using sed/awk.
So I can copy /var/spool/cron/crontabs/root (the current crontab file) to /var/spool/cron/crontabs/root.new and then run
crontab /var/spool/cron/crontabs/root.new
And that should work fine, I even still have the old root crontab file in case something goes wrong.
Am I right?
I'm sorry, I just want to make sure I won't have to work all night long since alot of servers will be affected, hahaha.
it would be best to avoid directly manipulating files in /var/spool/cron/crontabs/. create a backup with crontab -l as Corona688 suggested and then install the new one via the crontab command. Don't store the backup in /var/spool/cron/crontabs.