1

I have C program saved under /usr/sbin/. This program prints "Hello World" every two seconds. I can call/execute this program from any folder.

When I write C program with system("a.out"); in it, it's executing from any folder, but when I use execl() I need to give the absolute path, execl("/usr/sbin/a.out", "a.out");, otherwise it's not executing. Why?

  • 4
    Isn't that what execlp is for? – muru Sep 25 '18 at 5:13
  • @muru thank you very much, I didn't know – user41965 Sep 25 '18 at 5:22
  • 2
    Did you try man execl? – RalfFriedl Sep 25 '18 at 5:57
  • @RalfFriedl thank you very much, I'll look into it – user41965 Sep 26 '18 at 11:05
3

The system() C library function uses sh to execute the given command. In doing so, it does a search for the executable in the directories in $PATH unless the program is already specified with an absolute path. The execl() C library function does not do such a lookup.

Would you want to use the exec family of functions to execute your program, then use execlp() which does do a PATH lookup.

See man execlp on your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.