| 612 | } |
| 613 | |
| 614 | UInt64 isRunning(const fs::path & pid_file) |
| 615 | { |
| 616 | UInt64 pid = 0; |
| 617 | |
| 618 | if (fs::exists(pid_file)) |
| 619 | { |
| 620 | try |
| 621 | { |
| 622 | ReadBufferFromFile in(pid_file.string()); |
| 623 | if (tryReadIntText(pid, in)) |
| 624 | { |
| 625 | fmt::print("{} file exists and contains pid = {}.\n", pid_file.string(), pid); |
| 626 | } |
| 627 | else |
| 628 | { |
| 629 | fmt::print("{} file exists but damaged, ignoring.\n", pid_file.string()); |
| 630 | fs::remove(pid_file); |
| 631 | } |
| 632 | } |
| 633 | catch (const Exception & e) |
| 634 | { |
| 635 | if (e.code() != ErrorCodes::FILE_DOESNT_EXIST) |
| 636 | throw; |
| 637 | |
| 638 | /// If file does not exist (TOCTOU) - it's ok. |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (!pid) |
| 643 | { |
| 644 | auto sh = ShellCommand::execute("pidof clickhouse-server"); |
| 645 | |
| 646 | if (tryReadIntText(pid, sh->out)) |
| 647 | { |
| 648 | fmt::print("Found pid = {} in the list of running processes.\n", pid); |
| 649 | } |
| 650 | else if (!sh->out.eof()) |
| 651 | { |
| 652 | fmt::print("The pidof command returned unusual output.\n"); |
| 653 | } |
| 654 | |
| 655 | WriteBufferFromFileDescriptor std_err(STDERR_FILENO); |
| 656 | copyData(sh->err, std_err); |
| 657 | |
| 658 | sh->tryWait(); |
| 659 | } |
| 660 | |
| 661 | if (pid) |
| 662 | { |
| 663 | if (0 == kill(pid, 0)) |
| 664 | { |
| 665 | fmt::print("The process with pid = {} is running.\n", pid); |
| 666 | } |
| 667 | else if (errno == ESRCH) |
| 668 | { |
| 669 | fmt::print("The process with pid = {} does not exist.\n", pid); |
| 670 | return 0; |
| 671 | } |