Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run command as background process through AWS SSM?

I am trying to sync a S3 bucket which takes close to 3 hours to completely sync.

sync-bucket.sh:

nohup aws s3 sync "$source_bucket/$folder/" "s3://$destination_bucket/" \
        --profile abc --acl bucket-owner-full-control --sse "aws:kms" \
        --sse-kms-key-id "$KEY_ARN" > /var/log/$folder.log 2>&1 &

echo "Successfully triggered the sync job"

I was hoping to trigger the sync job using AWS SSM send command something like below:

trigger.sh:

COMMAND=$(aws ssm send-command --document-name "AWS-RunShellScript" \
            --targets "Key=instanceids,Values=${RECOVERY}"  \
            --parameters '{"executionTimeout":["10800"],"commands":["/opt/scripts/sync-bucket.sh"]}' \
            --output-s3-bucket-name "some-bucket" \
            --timeout-seconds 10800 \
            | jq -r '.Command.CommandId')

My observation is that SSM waits for this background job to finish before marking the execution as 'Success'. Is there a way we could perhaps just trigger the background job and make SSM finish the execution without having to wait for background job to finish?

Or is there a better way of doing this? I am basically trying to automate the process here and happy to let the job running in background on demand without having to login to instance and run command manually.

Thanks for your time.

like image 981
Yogesh Gupta Avatar asked Mar 14 '26 05:03

Yogesh Gupta


1 Answers

The issue isn't that SSM is waiting for your background command to finish, it's that your command isn't actually put into background because your nohup command waits for input. To fix, replace nohup whatever_your_command_is & by nohup whatever_your_command_is < /dev/null 2> /dev/null > /dev/null &

like image 177
MyUsername112358 Avatar answered Mar 16 '26 02:03

MyUsername112358



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!