How To Execute a Command After Efs Has Been Mounted to Your EC2.

Get Mount Unit Name

First, find out which systemd mount unit handles your efs mount using this commands:

sudo systemctl list-units | grep '/path/to/mount' | awk '{ print $1 }'

Or check at all mount units to find the one you need:

sudo systemctl list-units --type=mount

Create a Service

Create a service file with the following content using this command:

sudo nano /etc/systemd/system/your-service-name.service

[Unit]
Description=Your service description here.    # Just a free text description of your service.
After=var-efs.mount    # Your mount unit name.
Requires=var-efs.mount    # Your mount unit name.

[Service]
Type=simple
Restart=no
User=root
ExecStart=/bin/sh -c "echo 123"    # Sh command that you need to execute once the efs is mounted.

[Install]
WantedBy=multi-user.target
  • Make sure to replace commented variables with what you need.

Enable Your Service

To run this service you first need to enable it with this command: sudo systemctl enable your-service-name

Then you need to start it: sudo systemctl start your-service-name

Check Your Service

To check if your service has been executed after a restart, run this command: systemctl -l status your-service-name


Ubuntu AWS Linux Ops