cppthread 1.1.16
C++ Thread Library
process_is_running.cpp
1// Copyright (c) 2020-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20// cppthread
21//
22#include <cppthread/thread.h>
23
24
25// libexcept
26//
27#include <libexcept/file_inheritance.h>
28
29
30// C++
31//
32#include <iostream>
33
34
35// C
36//
37#include <string.h>
38
39
40// last include
41//
42#include <snapdev/poison.h>
43
44
45
46int main(int argc, char * argv[])
47{
48 libexcept::verify_inherited_files();
49
50 bool quiet(false);
51 bool all(true);
52 bool found(false);
53 for(int i(1); i < argc; ++i)
54 {
55 if(strcmp(argv[i], "--help") == 0
56 || strcmp(argv[i], "-h") == 0)
57 {
58 std::cerr << "Usage: " << argv[0] << " [--opts] <pid> ..." << std::endl;
59 std::cerr << "where --opts is one of:" << std::endl;
60 std::cerr << " --and | -a all the processes must exist" << std::endl;
61 std::cerr << " --help | -h print out this help screen" << std::endl;
62 std::cerr << " --or | -o at least one of the process must exist" << std::endl;
63 std::cerr << " --quiet | -q do not generate any output" << std::endl;
64 return 3;
65 }
66 else if(strcmp(argv[i], "--and") == 0
67 || strcmp(argv[i], "-a") == 0)
68 {
69 all = true;
70 }
71 else if(strcmp(argv[i], "--or") == 0
72 || strcmp(argv[i], "-o") == 0)
73 {
74 all = false;
75 }
76 else if(strcmp(argv[i], "--quiet") == 0
77 || strcmp(argv[i], "-q") == 0)
78 {
79 quiet = true;
80 }
81 else
82 {
83 found = true;
84 bool const is_running(cppthread::is_process_running(atoi(argv[i])));
85 if(all && !is_running)
86 {
87 // at least one is not running
88 if(!quiet)
89 {
90 std::cout << argv[i] << " is not running." << std::endl;
91 }
92 return 1;
93 }
94 if(!all && is_running)
95 {
96 // at least one is running
97 if(!quiet)
98 {
99 std::cout << argv[i] << " is running." << std::endl;
100 }
101 return 0;
102 }
103 }
104 }
105
106 if(!found)
107 {
108 std::cerr << "error: no <pid> where specified." << std::endl;
109 return 2;
110 }
111
112 if(all)
113 {
114 if(!quiet)
115 {
116 std::cout << "all processes are running." << std::endl;
117 }
118 return 0;
119 }
120
121 if(!quiet)
122 {
123 std::cerr << "none of these processes are running." << std::endl;
124 }
125 return 1;
126}
127
128// vim: ts=4 sw=4 et
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.