A C language tool for looking into the process list in a shutdown time script. See the comments. コメント文を参照してくださ。(英語でごめん。)

形式
C
投稿日時
2014-08-17 00:47
公開期間
無期限
  1. /* findps.c,
  2. // a Q&D program to repeatedly look for something in the output of ps wwaux.
  3. // Written by and copyright Joel Rees, Amagasaki, Japan, August 2014.
  4. //
  5. // Permission to use granted if the following three conditions are all met:
  6. // Don't try to steal my copyrights.
  7. // Don't blame me if it gets stuck or goes boom.
  8. // Do use it for good purposes, by your definition of good.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. /*
  15. # Compile it like this:
  16. cc -Wall -o findps findps.c
  17. # ----------
  18. # Test it like this:
  19. ./findps nobody
  20. # ----------
  21. # Install it something like this:
  22. chmod o-rwx findps
  23. sudo cp -i findps /usr/local/bin/
  24. sudo chown root:root /usr/local/bin/findps
  25. sudo touch /var/log/foundps.log
  26. # To catch the culprit running as nobody
  27. # and holding up your shutdown processes,
  28. # call it in your shutdown script something like this:
  29. findps nobody 100 > /var/log/foundps.log &
  30. # See also
  31. man ps
  32. # and look for where to find the numeric process id.
  33. # If it gets out of control,
  34. # getting another terminal session should help. Then
  35. man kill
  36. # Also, the return key and
  37. # ctrl-s
  38. # may be useful. But
  39. # ctrl-z
  40. # might not be so useful. But
  41. man bg
  42. # anyway. Or, I mean,
  43. man sh
  44. # and remember to search with the "/" command:
  45. # /bg
  46. # /job
  47. # and so forth.
  48. # An equivalent shell script, without parameter checks, etc. might look like this:
  49. # ------------
  50. #! /bin/bash
  51. lim=$2
  52. for (( ct = 0; ct < lim; ++ct )) ; do ps wwaux | grep $1; sleep 1; done
  53. # ------------
  54. */
  55. #define BIGCOMMANDSZ 1024
  56. int main( int argc, char * argv[] )
  57. {
  58. char cmdbuff[ BIGCOMMANDSZ + 1];
  59. int limit = 10; /* Maximum times through the loop. */
  60. if ( argc < 2 )
  61. { fprintf( stderr, "Usage: %s <search-term> [<limit-count>]\n", argv[ 0 ] );
  62. fprintf( stderr, "\tDefault limit count is %d\n", limit );
  63. return EXIT_FAILURE;
  64. }
  65. if ( argc > 2 )
  66. { limit = (int) strtol( argv[ 2 ], NULL, 10 );
  67. }
  68. cmdbuff[ BIGCOMMANDSZ ] = '\0';
  69. strncpy( cmdbuff, "ps wwaux | grep ", BIGCOMMANDSZ );
  70. strncat( cmdbuff, argv[ 1 ], BIGCOMMANDSZ );
  71. for ( ;limit > 0; --limit )
  72. { system( cmdbuff );
  73. sleep( 1 ); /* So it doesn't run away too fast. */
  74. }
  75. return EXIT_SUCCESS;
  76. }
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト