Question

Invoke a script or command using after creating a droplet

When using the DigitalOcean API to create a new droplet, is there a way to have the droplet run a command or script once it spins (using the API)?

Thanks.

Show comments

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Happy to provide a bit more clarification:

User-data can be either YAML or a script format supported by the droplet’s OS by default (ex. Bash). When using a non-YAML script whitespace is only as much of a concern as it would be for any other script in the language you use.

You can paste a user-data script into the create page in the control panel. Check the user-data checkbox in the optional settings and the text box will appear to allow pasting the script.

You can also send user-data via the API populating the user-data field in your request to the create endpoint.

On first boot after it fetches it’s network configuration and other basic settings cloud-init will automatically run your script. Make sure that it is non-interactive as any prompt for user input will cause it to stall since there is no user to enter the needed information. You can log into your droplet and view the output of your script (if you’re installing a lot of things you can use tail to watch it actually run in real-time if you’re quick enough) in the file /var/log/cloud-init-output.log

You can also send user-data via the API populating the user-data field in your request to the create endpoint.

Exactly what I was looking for!

Thank you @ryanpq

And also thanks @alastairc for your suggestions as well.

Hi @alastairc, thanks for replying.

Just a few more questions

you specify it in the user-data tab,

Where do I find the user-data tab?

Can those be built by using the DO API as well (i.e. built programmatically)?

Thanks!