This code snippet will run the date command. The execl() function is very useful for executing a command within your C program.You replace the NULL identifiers with any extra arguments to pass to the program. I prefer this over the system() function.
#include <unistd.h> int main(void) { execl("/bin/date", "%c", NULL, NULL, NULL); return 0; } |
Just compile this example.
localhost% cc exec.c |
And then run it to get the date.
localhost% ./a.out Thu May 25 10:03:37 AEST 2017 |
The system() function would be used this way. It is a little simpler to use.
#include <stdio.h> int main (void) { system("date +%s"); } |
The man execl command on Linux will bring up some more information about using the various related functions for executing another program within your C program.