Question

Initiate cron jobs on create droplet LAMP

Hello everyone, I’m working on a project trying to plug Woocommerce and DigitalOcean. Digital Ocean’s API works brilliant, I’ve resolved every issue with them except one.

My droplet is based on cronjobs. It checks for another website every minute and do something based on user choices.

I’ve created a snapshot of it and, when a new user subscribe to WordPress i create a droplet using this snapshot. Right after droplet creation I’ve to pull new version from Github and schedule cron.

So I’ve used user_data on droplet create method.

#!/bin/bash

git -C /var/www/buyer pull
line="* * * * * php /var/www/html/artisan schedule:run >> /dev/null 2>&1"
crontab -l | { cat; echo "$line"; } | crontab -
crontab -l > /root/test.txt

Everything works as expected, git pull works and crontab is edited correctly.

The only problem is that cronjob doesn’t start. The only way to start cronjob is connect through SSH to my droplet, edit the default root password and run crontab -l

Only after run crontab -l it works. Seems like crontab is in rest mode or something like that.

Hope somebody can helps me because this issue is driving me crazy.

Thanks a lot


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I’ve resolved. You should add SSH key to droplet in order to start cronjob automatically.

I’ve already tried it. Isn’t working

Can you add this at the end of your user_data script:

service cron restart

See if that helps.

Cheers

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.