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
Answer a question...

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.