Within a C-program I’m reading line by line the output of /bin/ps -fu1000 and searching for a string, for example “gnome”.
When I found the string (gnome), how can I get the pid?
The whole line is saved in a char buf[256].
cm 12556 1 0 10:47 ? 00:00:13 gnome-terminal
… and yes it’s part of a homework.
After reading some comments:
I had to use C. Goal of the task is to write a program which will send signals to running processes, containing a specified string in its name.
My approach is like:
fp = popen("/bin/ps -fu1000", "r");
while(fgets(line, sizeof line, fp)){
if(strstr(line, "gnome")){
printf("found\n");
/* do some nice stuff to get the PID */
}
}
If it is in C try looking at the
sscanfstandard library function. Documentation should be available through either via a man page on a Unix type system such as Linux, or an online reference such as the GNU C Reference.