/* findps.c, // a Q&D program to repeatedly look for something in the output of ps wwaux. // Written by and copyright Joel Rees, Amagasaki, Japan, August 2014. // // Permission to use granted if the following three conditions are all met: // Don't try to steal my copyrights. // Don't blame me if it gets stuck or goes boom. // Do use it for good purposes, by your definition of good. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> /* # Compile it like this: cc -Wall -o findps findps.c # ---------- # Test it like this: ./findps nobody # ---------- # Install it something like this: chmod o-rwx findps sudo cp -i findps /usr/local/bin/ sudo chown root:root /usr/local/bin/findps sudo touch /var/log/foundps.log # To catch the culprit running as nobody # and holding up your shutdown processes, # call it in your shutdown script something like this: findps nobody 100 > /var/log/foundps.log & # See also man ps # and look for where to find the numeric process id. # If it gets out of control, # getting another terminal session should help. Then man kill # Also, the return key and # ctrl-s # may be useful. But # ctrl-z # might not be so useful. But man bg # anyway. Or, I mean, man sh # and remember to search with the "/" command: # /bg # /job # and so forth. # An equivalent shell script, without parameter checks, etc. might look like this: # ------------ #! /bin/bash lim=$2 for (( ct = 0; ct < lim; ++ct )) ; do ps wwaux | grep $1; sleep 1; done # ------------ */ #define BIGCOMMANDSZ 1024 int main( int argc, char * argv[] ) { char cmdbuff[ BIGCOMMANDSZ + 1]; int limit = 10; /* Maximum times through the loop. */ if ( argc < 2 ) { fprintf( stderr, "Usage: %s <search-term> [<limit-count>]\n", argv[ 0 ] ); fprintf( stderr, "\tDefault limit count is %d\n", limit ); return EXIT_FAILURE; } if ( argc > 2 ) { limit = (int) strtol( argv[ 2 ], NULL, 10 ); } cmdbuff[ BIGCOMMANDSZ ] = '\0'; strncpy( cmdbuff, "ps wwaux | grep ", BIGCOMMANDSZ ); strncat( cmdbuff, argv[ 1 ], BIGCOMMANDSZ ); for ( ;limit > 0; --limit ) { system( cmdbuff ); sleep( 1 ); /* So it doesn't run away too fast. */ } return EXIT_SUCCESS; }