当我的 EC2 VM (Ubuntu) 启动时,我希望它以
myscript.py
而不是默认的 ec2-user
用户身份执行 python 脚本 root
。这包括使用 ec2-user
的 python 可执行文件、已安装的软件包和二进制文件(例如 chromedriver)
我该怎么做?
到目前为止我做了/尝试过的事情:
ec2-user
登录,我验证了python myscript.py
、python --version
和which python
按预期工作root
登录,运行python
结果为Command 'python' not found
root
身份登录,运行 su ec2-user -c python
结果为 Command 'python' not found
。与sudo -u ec2-user bash -c 'python --version'
root
身份登录,运行 /path/to/ec2-user/python_executable /path/to/myscript.py
确实启动脚本,但由于找不到二进制文件(例如 chromedriver)而崩溃su ec2-user -c 'echo "Switched to User ${USER} with $(python --version) in $(which python)" >> /path/to/logfile.log'
将 Switched to User ec2-user with in
行添加到日志文件中,因此在调用 ec2-user
时不假定
python
那么,我怎样才能让我的用户
ec2-user
在实例启动时在用户数据的执行中执行myscript.py
?
参考资料:
我终于通过使用带有
cronjob
的@reboot
解决了这个问题,在每次启动时运行所需的命令