LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   execute system command in c (https://www.linuxquestions.org/questions/programming-9/execute-system-command-in-c-342709/)

alaios 07-13-2005 01:45 AM

execute system command in c
 
Hi i need to execute the following command in c
rm /root/test.log

so i have used the following code

status=execl("/bin/rm","/root/test.log");
fprintf(logfile,"status %d \n",status);

but still this code returns -1 which means failure.. Where am i wrong? Thx

sind 07-13-2005 01:58 AM

Hi alaios,

Try adding this after the call to execl(), it will tell you what the error was:

Code:

perror("execl() failed");
~sind

alaios 07-13-2005 05:02 AM

thx but this wont help... this code is being executed by a child so i cant see the error message

hk_linux 07-13-2005 05:26 AM

1. I think you can use unlink to delete a file. Will it serve your purpose.

Quote:

Extract from man execl.
The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as
arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that
represent the argument list available to the executed program.
The first argument, by convention, should point to the file name associated with the file being executed.
The list of arguments must be terminated by a NULL pointer.
2. The usual syntax i use for execl is

Code:

execl ("/bin/rm", "/bin/rm",  "/root/test/log", NULL);
Though i am not sure, if what you do is incorrect.

HTH


All times are GMT -5. The time now is 04:56 PM.