I am trying to execute the commands when specific IAM users perform session manager on EC2 instance in the background. How do I proceed with this?
You can use Amazon EventBridge to trigger automation when an IAM user uses Session Manager on an EC2 instance. See the instructions here: https://docs.aws.amazon.com/systems-manager/latest/userguide/monitoring-systems-manager-events.html