When my EC2 VM (Ubuntu) starts, I'd like it to execute a python script myscript.py
as ec2-user
instead of the default root
user. This includes using the ec2-user
's python executable, installed packages and binaries (e.g. chromedriver)
How can I do this?
What I did/ tried so far:
- logged in as
ec2-user
, I verified thatpython myscript.py
,python --version
andwhich python
work as intended - logged in as
root
, runningpython
results inCommand 'python' not found
- logged in as
root
, runningsu ec2-user -c python
results inCommand 'python' not found
. Same result withsudo -u ec2-user bash -c 'python --version'
- logged in as
root
, running/path/to/ec2-user/python_executable /path/to/myscript.py
does start the script, but crashes due not not finding binaries (e.g. chromedriver) - adding
su ec2-user -c 'echo "Switched to User ${USER} with $(python --version) in $(which python)" >> /path/to/logfile.log'
adds the lineSwitched to User ec2-user with in
to the logfile, hence does not assume theec2-user
user when callingpython
So, how can I make my user ec2-user
execute myscript.py
within the execution of the user data at instance startup?
references: