Question

Run shell script on droplet using API

Hey, is there a way I can run a shell script I created on my droplet through the API?


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
March 19, 2023

Hi there,

You can run a shell script during the Droplet creation process using the user-data option described here:

https://docs.digitalocean.com/products/droplets/how-to/provide-user-data/

Once the Droplet is created, the DigitalOcean API would not have access to run any commands on the Droplet itself, as the Droplets are unmanaged meaning that only you would have root access to them.

You could run your shell scripts in a variety of different ways:

  • Use an automation tool like Ansible for example
  • Run the shell scripts via SSH, you can do that without actually connecting to the server first and then running the script, but instead use the following:
ssh root@server_ip 'bash -s' < ./location_to_your_script_on_the_server.sh

There are other wise of doing this as well, feel free to share more information about your use case and I will be happy to make more suggestions!

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel