Python Forum
Methods of running a script on Linux distro - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Methods of running a script on Linux distro (/thread-12360.html)



Methods of running a script on Linux distro - Vysero - Aug-21-2018

I am currently running ubuntu 16.04. I have a python script that is supposed to create a symlink from the current linux header directory to /usr/src/linux:

#!/usr/bin/env python 
import os 
import sys 
import subprocess
def _runningKernel():
    result = subprocess.check_output(['uname', '-r']).strip()
    return result
def _kernelHeaderPath(kernelName):
    return os.path.join('/', 'usr', 'src', 'linux-headers-%s' % kernelName)
_linkName = os.path.join('/', 'usr', 'src', 'linux')
_headersPath = _kernelHeaderPath(_runningKernel())
if not os.path.isdir(os.path.join(_headersPath, '.')):
    sys.stderr("Error: Did not the header files directory at %s\n" % _headersPath)
    sys.exit(1)
if (not os.path.islink(_linkName)) and os.path.exists(_linkName):
    sys.stderr("Error: %s is not a symlink.  (Cowardly refusing to overwrite it.)\n" % _headersPath)
    sys.exit(1)
if os.path.islink(_linkName):
    os.remove(_linkName);
os.symlink(_headersPath, _linkName)
The script was given as text for me to copy. I have saved the the code into a file which I named script.py. Now when I tried to run the script.py with a sudo ./script.py command it did not work. I have also tried calling the file script (no .py extension) and running sudo ./script but that doesn't work either.

I have also tried just running the script with python interpreter like: python script.py (py3 interpreter) and I think it ran but I am not sure. My question is:

Is there something I have to do to the script in order to get it to run with a ./ command on linux? I have ran scripts before with ./ command why won't this one run that way?


RE: Methods of running a script on Linux distro - woooee - Aug-21-2018

Does your user have permission to do this? We can't help further because there is no way for us to know what this means

> it wont work


RE: Methods of running a script on Linux distro - heras - Aug-21-2018

Normally you would chmod +x script.py to make it executable without having to specify python on the command line explicitly.


RE: Methods of running a script on Linux distro - Vysero - Aug-21-2018

(Aug-21-2018, 05:12 PM)heras Wrote: Normally you would chmod +x script.py to make it executable without having to specify python on the command line explicitly.

I have the script.py on my desktop. I am in: ~/Desktop: directory and when I type: sudo chmod +x script.py. It gives the error:

Output:
rob@linux038:~/Desktop$ sudo chmod +x script.py [sudo] password for rob: chmod: cannot access 'script.py': No such file or directory
So I tried:

rob@linux038:~/Desktop$ sudo chmod +x script
rob@linux038:~/Desktop$

So it worked?? I am not sure, lol how do I check if the link was created any idea?


RE: Methods of running a script on Linux distro - metulburr - Aug-21-2018

Quote:rob@linux038:~/Desktop$ sudo chmod +x script
rob@linux038:~/Desktop$
If this worked then it appears you have two copies of script in your desktop. one called script and one called script.py. It doesnt matter whether there is an extension or not, but if you got a prompt and not an error when you ran this, then it did make it executable and you should be able to now do
~/Desktop$ sudo ./script
(Aug-21-2018, 03:31 PM)Vysero Wrote: I have also tried just running the script with python interpreter like: python script.py (py3 interpreter) and I think it ran but I am not sure.
im pretty sure that ubuntu 16.04 defaults python to python2.x and python3 to python3.x. The same is true there. If you get a prompt after running anything, the program ran and is done without error.

Your shebang line (1st comment line) is important when you run a script as an executable in linux. When you put python3 script.py you are running the script under python3 (or whatever it is mapped to). When you run it as an executable, you are telling it to run under what is in the shebang line. This could cause confusion as you might be thinking it is running under py2, when you are really using py2. This is assuming you have not changed the default though or symlinked it elsewhere.


RE: Methods of running a script on Linux distro - Vysero - Aug-21-2018

Thanks!


RE: Methods of running a script on Linux distro - heras - Aug-21-2018

I would like to point out that you should use sudo judiciously, i.e. only if you need to. In this case it was not required and chmod as normal user would have been sufficient.