[Script] automatically crontab update


Sometimes you need to update a crontab configuration in multiple enviroments (staging, production...).


If you have properly configured ssh, you can use this shell function:


function addToCrontabIfNotAlreadyPresent() {
HOST=$1

ssh -o "BatchMode yes" $USER@$HOST << EOF

crontab_line="5 0 * * * cd ~/yourproject/ && ./script/run.sh >/dev/null 2>&1-"
alreadyInCrontab="\$(crontab -l | grep "\$crontab_line" )"

if test "\$alreadyInCrontab" == ""; then
crontab -l > /tmp/updating_crontab.tmp
echo "\$crontab_line" >> /tmp/updating_crontab.tmp
crontab /tmp/updating_crontab.tmp
rm /tmp/updating_crontab.tmp

echo "Crontab updated";
else
echo "Crontab ok";
fi
EOF
}

USER='your_user'
addToCrontabIfNotAlreadyPresent "10.10.10.10"

Nessun commento: