Try this:
echo '#!/usr/bin/env python' > myscript
cat myscript.py >>myscript
chmod +x myscript
./myscript
Of course you will have to change the code to kill a process named "myscript"
On UNIX systems, an executable file contains a few bytes at the beginning which tell the OS what binary format is being used. If the first two bytes are #!
then the OS assumes that this is actually a text file which can be executed by another program, and the OS loads the other program and passes to it, the text file.
In this case I probably could have written #!/usr/bin/python
on the top line, but if your python is in /usr/local/bin
, then it would not work. Instead, I leverage env
to get it to search your normal path for python. All UNIX systems have env in /usr/bin. For a bit more info you can type in man env
.