Question

execute doctl using cronjob + shell script failed

i have this following code to make droplets (and another shell script code to destroy this droplets), I want to cron this code on the cronjob but failed, can anyone tell me how to cron doctl?

#!/bin/bash
#set environtment variable
database_id=$(<./config/database_ID)
region=sgp1
num_nodes=3
size=db-s-2vcpu-4gb

#create readonly
echo "create readonly on ${database_id}"
for i in {1..10}
do
        echo "create db-ro-${i}"
        doctl database replica create $database_id db-ro-$i --size db-s-4vcpu-8gb --region $region
done

#resizing database
doctl databases resize $database_id --num-nodes $num_nodes --size $size  -v

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.

Bobby Iliev
Site Moderator
Site Moderator badge
November 27, 2022
Accepted Answer

Hi there,

When running scripts from a corn job, you need to make sure that you reference files with their full paths.

I believe that you just need to change this line here:

database_id=$(<./config/database_ID)

To include the full path to your config directory, eg:

database_id=$(</full/path/to/the/config/database_ID)

Let me know how it goes.

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
November 28, 2022

Hi @sleepingseagreenmorel,

You need to use full paths in your scripts rather than relative ones. This could be a problem for the system as it can detect different paths and thus provide different outcomes.

In your case

#!/bin/bash
#set environtment variable
database_id=$(<./config/database_ID)

The database_id should have the full path.

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.