1

I'm having some problem making a python file run everytime the AWS server boots.

I am trying to run a python file to start a web server on Amazon Webservice EC2 server.

But I am limited to edit systemd folder and other folders such as init.d

Is there anything wrong?

Sorry I don't really understand EC2's OS, it seems a lot of methods are not working on it.

What I usually do via ssh to start my server is:

python hello.py

Can anyone tell me how to run this file automatically every time system reboots?

J.Zhou
  • 27
  • 1
  • 7
  • You don't say what problems/errors you are experiencing, but you might want to refer to `hello.py` via a full path name (eg `/users/home/ec2-user/hello.py`). – John Rotenstein Jun 11 '18 at 21:54

1 Answers1

1

It depends on your linux OS but you are on the right track (init.d). This is exactly where you'd want to run arbitrary shell scripts on start up.

Here is a great HOWTO and explanation: https://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html

and another stack overflow specific to running a python script:

Run Python script at startup in Ubuntu

if you want to share you linux OS I can be more specific.

EDIT: This may help, looks like they have some sort of launch wizard: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

When you launch an instance in Amazon EC2, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts. You can pass two types of user data to Amazon EC2: shell scripts and cloud-init directives. You can also pass this data into the launch wizard as plain text, as a file (this is useful for launching instances using the command line tools), or as base64-encoded text (for API calls).

Talel BELHADJSALEM
  • 3,199
  • 1
  • 10
  • 30
PoweredBy90sAi
  • 1,163
  • 7
  • 7
  • Thank you for your reply, I am using windows powershell to access amazon web server via ssh. So the system should be AMAZON EC2 linux system. I searched a lot of methods but they all failed for access permission reasons – J.Zhou Jun 11 '18 at 17:00
  • Thanks. Amazon EC2 instances are some standard linux distro, im just not sure which one. (or if you can choose) . Try some of these: https://www.howtogeek.com/206240/how-to-tell-what-distro-and-version-of-linux-you-are-running/ And if you get permissions issues try running sudo before the command: sudo this will prompt you for password to run as a super user. This may be your SSH credential as well but i doubt it. You could check amazons documentation, that might help answer these questions. – PoweredBy90sAi Jun 11 '18 at 17:38
  • Oh man, they dont indicate on my cursory search: https://aws.amazon.com/mp/linux/ Documentation may be the key. – PoweredBy90sAi Jun 11 '18 at 17:41