0

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)

5
  • 5
    Not sure if it'll fix your cpu consumption problem, but the shebang needs python, i.e. add python to the header: #!/usr/bin/env python Commented Sep 19, 2016 at 22:16
  • When I write #!/usr/bin/env python it says "/usr/bin/env: ‘python’: No such file or directory" Commented Sep 19, 2016 at 22:20
  • Check what which python returns. Then try changing that line to use that python version, perhaps it needs to be something like #!/usr/bin/env python3 Commented Sep 19, 2016 at 22:27
  • Yes I did it like that and it works. Thanks... Commented Sep 19, 2016 at 22:28
  • of course, cheers Commented Sep 19, 2016 at 22:29

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.