Skip to content

Commit 5c68d23

Browse files
committed
server : add pidfile option
So we can track the pid of this process Signed-off-by: Eric Curtin <[email protected]>
1 parent 860a9e4 commit 5c68d23

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

common/arg.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,5 +3373,9 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
33733373
}
33743374
).set_examples({LLAMA_EXAMPLE_SERVER}));
33753375

3376+
add_opt(common_arg({ "--pidfile" }, "FILE", "path to PID file for server process",
3377+
[](common_params & params, const std::string & value) { params.pidfile = value; })
3378+
.set_examples({ LLAMA_EXAMPLE_SERVER }));
3379+
33763380
return ctx_arg;
33773381
}

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ struct common_params {
366366
std::string hostname = "127.0.0.1";
367367
std::string public_path = ""; // NOLINT
368368
std::string chat_template = ""; // NOLINT
369+
std::string pidfile = ""; // path to PID file for server process // NOLINT
369370
bool use_jinja = false; // NOLINT
370371
bool enable_chat_template = true;
371372
common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_DEEPSEEK;

tools/server/server.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
#include <unordered_map>
3232
#include <unordered_set>
3333

34+
#ifdef _WIN32
35+
#include <process.h>
36+
#define getpid _getpid
37+
#endif
38+
3439
using json = nlohmann::ordered_json;
3540

3641
constexpr int HTTP_POLLING_SECONDS = 1;
@@ -3691,6 +3696,19 @@ inline void signal_handler(int signal) {
36913696
shutdown_handler(signal);
36923697
}
36933698

3699+
static int create_pidfile(const std::string & pidfile) {
3700+
FILE * f = ggml_fopen(pidfile.c_str(), "w");
3701+
if (!f) {
3702+
LOG_ERR("Unable to open pidfile %s: %s\n", pidfile.c_str(), strerror(errno));
3703+
return -1;
3704+
}
3705+
3706+
fprintf(f, "%d\n", getpid());
3707+
fclose(f);
3708+
3709+
return 0;
3710+
}
3711+
36943712
int main(int argc, char ** argv) {
36953713
// own arguments required by this example
36963714
common_params params;
@@ -3699,6 +3717,10 @@ int main(int argc, char ** argv) {
36993717
return 1;
37003718
}
37013719

3720+
if (!params.pidfile.empty() && create_pidfile(params.pidfile)) {
3721+
return 1;
3722+
}
3723+
37023724
common_init();
37033725

37043726
// struct that contains llama context and inference

0 commit comments

Comments
 (0)