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:
[email protected]:~/Desktop$ sudo chmod +x script.py [sudo] password for rob: chmod: cannot access 'script.py': No such file or directory
So I tried:

[email protected]:~/Desktop$ sudo chmod +x script
[email protected]:~/Desktop$

So it worked?? I am not sure, lol how do I check if the link was created any idea?
Reply
#5
Quote:[email protected]:~/Desktop$ sudo chmod +x script
[email protected]:~/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.
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
  Running script on multiple files Afrodizzyjack 1 401 May-14-2021, 10:49 PM
Last Post: Yoriz
  Error when running script on startup in Linux NoahTheNerd 0 399 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Login to NordVPN on Linux with python script AGreenPig 2 986 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  Refresh data in python script while running in Terminal frankenchrist 4 761 Feb-03-2021, 09:54 AM
Last Post: Larz60+
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 1,288 Jan-16-2021, 10:20 PM
Last Post: Miletkir
  Need help with 3 errors when running script pythoncoder999 3 787 Dec-19-2020, 04:04 AM
Last Post: Larz60+
  Keep Application running after Python script ends PEGylated_User 0 447 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  running python script showing an icon in the tray notification area chubbychub 1 663 Nov-01-2020, 03:52 PM
Last Post: jefsummers
  Python 3.6.10 fails when running a localized script. Mikee 2 648 Jul-09-2020, 10:25 PM
Last Post: Mikee
  How to kill a bash script running as root from a python script? jc_lafleur 4 1,392 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur

Forum Jump:

User Panel Messages

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