0

I have a larger program (150 lines or so) but this seems to be the only issue I have. I have managed to cut down the problem and have made it into a smaller program. Essentially, it forks the program and tries to execute a linux command (I am running this with ubuntu). I get the following output:

Current instruction is bin/ls
Current instruction is bin/ls

Child PID is 984
Traceback (most recent call last):
 File "./Test.py", line 17 in <module>
   makeFork("bin/ls")
 File "./Test.py", line 12, in makeFork
   os.execl(instruction, instruction)
 File "/usr/lib/python2.7/os.py", line 314, in execl
   execv(file, args)
OSError: [Errno2] No such file or directory

Parent PID is 4

Below is the code for the program

import os
from time import sleep
os.system("clear")

def makeFork(instruction):
    PID = os.fork() #Creating a fork for the child process
    sleep(2)
    print("Current instruction is " + instruction)
    if PID == 0:
        print("\nChild PID is " + format(os.getpid()))
        os.execl(instruction, instruction)
    sleep(2)
    print("\nParent PID is " + format(os.getppid()))


makeFork("bin/ls")

Where am I going wrong?

  • 1
    I'd argue that this is eligible for close-as-typo; it's not a bug specifically related to `os.execl()` (or to Python), or that people trying to use `os.execl()` are more likely to hit than those specifying executable paths in any other context (including contexts that aren't programming-related at all). – Charles Duffy Apr 01 '19 at 15:14
  • ...you'd get the same error (even if printed/exposed differently) running `bin/ls` from a non-root directory in bash. – Charles Duffy Apr 01 '19 at 15:25

1 Answers1

1

bin/ls is not /bin/ls: The name without the leading / is relative to your current working directory, and so requires your current directory to have a subdirectory named bin containing an executable ls.

Because no such directory exists, you get an errno 2 ("no such file or directory"). Change your invocation to:

makeFork("/bin/ls")

...and it runs correctly.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441