I'm starting machines from an AMI (Amazon Machine Image) on AWS (Amazon Web Services) and want to load the crontab dynamically from a file instead of using the AMI's crontab. One approach I thought about was setting the AMI's crontab to something like:

@reboot crontab [CRONTAB_FILE]

The problem with this approach is that @reboot schedules in CRONTAB_FILE are not going to run using it.

Any other way to achieve this? Any way to alter crontab before the daemon starts?

My machines are running Debian if that makes a difference.

  • Why don't you use your user's crontab? – terdon Jun 16 '15 at 12:01
  • @terdon, not sure I understand your suggestion... (I'm using the root user for all activity on the machine), could you please elaborate? – lribinik Jun 16 '15 at 13:55
  • I just can't imagine why you would want to source an external crontab instead of just running crontab -e as your normal user (root or lribinik or whoever) and adding the commands there. I may well be missing something here, I know nothing about AWS, I just don't see the point of importing crontabs instead of writing them directly. – terdon Jun 16 '15 at 14:20
  • @terdon, I want to be able to use a source-controlled crontab – lribinik Jun 16 '15 at 14:25
  • That shouldn't be a problem, user-specific crontabs are in /var/spool/cron/crontabs/. – terdon Jun 16 '15 at 14:48

I usually just crontab -u user file in someting like /etc/rc.local and have that load a cron tab. Other then that, there is not a lot to do.

I would normally do something like: (in rc.local)

wget -O /tmp/instance  
INSTANCE=`cat /tmp/instance`
CONFIG=`ec2-describe-tags -K $KEY -C $CERT -F "resource-id=$INSTANCE" -F "key=ConfigBucket" | awk '{print $5}'`
if  [ -n $CONFIG ]; then
    s3cmd -c /etc/.s3cfg sync s3://$CONFIG/etc/ /etc
    chmod +x /etc/post-ec2-start.sh

exit 0

Then in /etc/post-ec2-start.sh

wget -O /tmp/instance
INSTANCE=`cat /tmp/instance`


ec2-attach-volume -K $KEY  -C $CERT $VOL1 -i "$INSTANCE" -d /dev/sdf
ec2-attach-volume -K $KEY  -C $CERT $VOL2 -i "$INSTANCE" -d /dev/sdg
ec2-associate-address -K $KEY  -C $CERT -i "$INSTANCE" $IP
sleep 90
mount /dev/sdf1 /home/
mount /dev/sdg1 /var/lib/mysql

hostname some.fqdn.com

#register crontabs
#crontab -u coteyr /etc/coteyr.crontab
crontab -u root /etc/root.crontab

#fix home directoris
cd /home/
for i in *; do 
    useradd $i
    chown -R $i:$i /home/$i

This has the benefit of loading config from S3 then running based on that config. If I needed stuff to run on (re)boot I would just add it to /etc/post-ec2-start.sh or /etc/rc.local depending on if I needed it as part of setting up the configuration or after the configuration was complete. Something similar should work for you.

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.