exec appears more straightforward, but 'execlp("sh", "-c", "du -sh /", NULL)' says 'du -sh /: command not found'. I don't want to seperate the arguments myself as I don't know how many will be entered in.
Anyways, system() by itself with an '&' appears to work, but I keep getting random "Segmentation Fault"s. I think it is because I did not add the '&' properly.
Also, while using fork(), do I have to worry about zombie processes or anything like that?
Here is the function using 'execlp':
Code:
void execute(const char *cmd)
{
switch(fork())
{
case -1:
perror("execute: fork");
return;
case 0:
if (execlp("sh", "-c ", cmd, NULL) == -1)
perror("execute: exec() failed");
default:
}
return;
}
Here is the function using system() with '&':
Code:
void execute(const char *cmd)
{
char * copy = malloc(sizeof(cmd) + sizeof(char));
strcpy(copy, cmd);
strcat(copy, '&');
system(copy);
free(copy);
return;
}