Question

I'm trying to execute ping command within my program. I'm working on newest Raspian on RaspBerry Pi B Model. It tells me that: /bin/sh-c: 0: Can't open ping -c 1 192.168.0.12 My code(it's not finished jet, i know that following code can have some issues):

int status;
pid_t pid;
pid = fork();
const char* commandChar = commandName.c_str();
if(pid == 0)
{
    execl(SHELL, SHELL "-c", commandChar, NULL);
    _exit(1);
}else if(pid < 0)
{
    return false;
}
commandChar = NULL;
    delete commandChar;
return true;

Ping (/bin/ping) have rwsr-xr-x permissions, so why I can't execute it and how to make it work?

enter image description here

Was it helpful?

Solution

execl expects a file path, you are giving it the file path "/bin/ping -c 192.168.0.12", which of course doesn't exist. Look at the man page for execl, you will see that the last argument is used for the arguments to the program to be run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top