Question

run a single script after droplet deployment

this is likely answered before, but is it possible to have a droplet execute a single script after deployment (the script is inside the snapshoot used)


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
July 20, 2024

Hey 👋

Yep, you can do this with Cloud init:

https://docs.digitalocean.com/products/droplets/how-to/automate-setup-with-cloud-init/

The above documentation goes over on how to set this all up, but let me know if you have any questions!

Here’s a general approach you could take:

  1. Add your script is present in the snapshot, for example /root/post-deploy-script.sh.
  2. Set up a cloud-config file or user data script that will run when the Droplet first boots. This can be done through the DigitalOcean control panel when creating the Droplet, or via their API.
  3. In the cloud-config or user data, include a command to execute your script.

For example, your user data might look something like this:

#cloud-config
runcmd:
  - /bin/bash /root/post-deploy-script.sh

Alternatively, instead of creating the script file separately, you could define it directly in your Cloud init file as per the documentation above, e.g.:

#cloud-config
users:
  - name: example-user
    shell: /bin/bash
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    ssh_import_id:
      - gh:<your-GitHub-username>
disable_root: true
packages:
  - nginx
runcmd:
  - 'export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)'
  - 'echo Droplet: $(hostname), IP Address: $PUBLIC_IPV4 > /var/www/html/index.html'

- Bobby

KFSys
Site Moderator
Site Moderator badge
July 21, 2024

Heya,

Yep, that is what Cloud-init is for and can help you greatly. Here is an example usage of Cloud-init:

https://www.digitalocean.com/community/questions/automate-nginx-reverse-proxy-setup-for-node-js-with-cloud-init

You can take inspiration from there.

There are a few ways to do it. You can use rc.local file or Cloud init to do it. You can also use crontab for this like below,

Create the script,

nano /opt/post-startup.sh

Paste your commands,

#!/bin/bash

#Add your commands below

Make it executable

chmod +x /opt/post-startup.sh

And then add following to your crontab,

@reboot bash /opt/post-startup.sh > /dev/null 2>&1

Source Linux: How to execute a bash script at system startup using crontab

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.