Python Forum
Methods of running a script on Linux distro
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Methods of running a script on Linux distro
#1
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?
Reply
#2
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
Reply
#3
Normally you would chmod +x script.py to make it executable without having to specify python on the command line explicitly.
Reply
#4
(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?
Reply
#5
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.
Recommended Tutorials:
Reply
#6
Thanks!
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 566 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 454 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Is possible to run the python command to call python script on linux? cuten222 6 710 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Help Running Python Script in Mac OS emojistickers 0 331 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Trying to make a board with turtle, nothing happens when running script Quascia 3 652 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Python script running under windows over nssm.exe JaroslavZ 0 699 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Running script with subprocess in another directory paul18fr 1 3,657 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Running 3rd party libs on Steam Deck (Arch Linux) with restricted access metulburr 0 1,831 Jan-07-2023, 10:41 PM
Last Post: metulburr
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 922 Nov-16-2022, 07:58 PM
Last Post: Winfried
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,313 May-24-2022, 07:02 AM
Last Post: netanelst

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020