Python script uses 100% CPU

Hey I was working with python. In my python file I just have 2 lines like :

#!/usr/bin/env
print("hello")

and I make my .py file executable and run it(./hello.py) on ubuntu server. With "top" command, i listed all processes. hello.py uses 100% CPU. Why it use 100% CPU(Server has 512MB 1 CPU)

Upvotes: 0

Views: 2143

Answers (1)

user2357112
user2357112

Reputation: 280251

Your incorrect shebang line of

#!/usr/bin/env

causes the system to launch /usr/bin/env to handle the script, as follows:

/usr/bin/env ./hello.py

/usr/bin/env treats the first argument not containing = and not starting with - as a program to run, so it tries to launch ./hello.py. Due to the incorrect shebang line, this once again runs

/usr/bin/env ./hello.py

It's an infinite loop.

Upvotes: 5

Related Questions