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 --versionandwhich pythonwork as intended - logged in as
root, runningpythonresults inCommand 'python' not found - logged in as
root, runningsu ec2-user -c pythonresults 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.pydoes 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 into the logfile, hence does not assume theec2-useruser 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:
I finally solved the problem by using a
cronjobwith@reboot, running the desired commands at every startup