-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4245 from chenyukang/yukang-daemon
Support easy run daemon mode for Linux/MacOS
- Loading branch information
Showing
11 changed files
with
327 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use ckb_app_config::{DaemonArgs, ExitCode}; | ||
use colored::*; | ||
use nix::sys::signal::{kill, Signal}; | ||
use nix::unistd::Pid; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use std::{fs, io}; | ||
|
||
pub fn daemon(args: DaemonArgs) -> Result<(), ExitCode> { | ||
let pid_file = &args.pid_file; | ||
if args.check { | ||
// find the pid file and check if the process is running | ||
match check_process(pid_file) { | ||
Ok(pid) => { | ||
eprintln!("{}, pid - {}", "ckb daemon service is running".green(), pid); | ||
} | ||
_ => { | ||
eprintln!("{}", "ckb daemon service is not running".red()); | ||
} | ||
} | ||
} else if args.stop { | ||
kill_process(pid_file, "ckb")?; | ||
fs::remove_file(pid_file).map_err(|_| ExitCode::Failure)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn check_process(pid_file: &PathBuf) -> Result<i32, ExitCode> { | ||
let pid_str = fs::read_to_string(pid_file).map_err(|_| ExitCode::Failure)?; | ||
let pid = pid_str | ||
.trim() | ||
.parse::<i32>() | ||
.map_err(|_| ExitCode::Failure)?; | ||
|
||
// Check if the process is running | ||
match kill(Pid::from_raw(pid), None) { | ||
Ok(_) => Ok(pid), | ||
Err(_) => Err(ExitCode::Failure), | ||
} | ||
} | ||
|
||
fn kill_process(pid_file: &PathBuf, name: &str) -> Result<(), ExitCode> { | ||
if check_process(pid_file).is_err() { | ||
eprintln!("{} is not running", name); | ||
return Ok(()); | ||
} | ||
let pid_str = fs::read_to_string(pid_file).map_err(|_| ExitCode::Failure)?; | ||
let pid = pid_str | ||
.trim() | ||
.parse::<i32>() | ||
.map_err(|_| ExitCode::Failure)?; | ||
eprintln!( | ||
"stopping {} deamon service with pid {} ...", | ||
name, | ||
pid.to_string().red() | ||
); | ||
// Send a SIGTERM signal to the process | ||
let _ = kill(Pid::from_raw(pid), Some(Signal::SIGTERM)).map_err(|_| ExitCode::Failure); | ||
let mut wait_time = 60; | ||
eprintln!("{}", "waiting ckb service to stop ...".yellow()); | ||
loop { | ||
let res = check_process(pid_file); | ||
match res { | ||
Ok(_) => { | ||
wait_time -= 1; | ||
eprint!("{}", ".".yellow()); | ||
let _ = io::stderr().flush(); | ||
std::thread::sleep(std::time::Duration::from_secs(1)); | ||
} | ||
_ if wait_time <= 0 => { | ||
eprintln!( | ||
"{}", | ||
format!( | ||
"ckb daemon service is is still running with pid {}..., stop it now forcefully ...", | ||
pid | ||
) | ||
.red() | ||
); | ||
kill(Pid::from_raw(pid), Some(Signal::SIGKILL)).map_err(|_| ExitCode::Failure)?; | ||
break; | ||
} | ||
_ => { | ||
break; | ||
} | ||
} | ||
} | ||
eprintln!("\n{}", "cbk daemon service stopped successfully".green()); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
# Init/Service Scripts | ||
# CKB Init Scripts | ||
|
||
## Run CKB in deamon mode | ||
|
||
CKB has a builtin deamon mode, command to run CKB in deamon mode(only for Linux/MacOS): | ||
|
||
```bash | ||
ckb run --deamon | ||
``` | ||
|
||
Check deamon satus: | ||
|
||
```bash | ||
ckb deamon --check | ||
``` | ||
|
||
Stop deamon process: | ||
|
||
```bash | ||
ckb deamon --stop | ||
``` | ||
|
||
The deamon mode is only for Linux/MacOS, and the CKB service will not be started automatically after reboot. | ||
|
||
## Init/Service Scripts | ||
|
||
This folder provides the init/service scripts to start CKB node and miner as | ||
daemons on various Unix like distributions. | ||
|
||
See the README in each folder for the detailed instructions. | ||
|
||
## Disclaimer | ||
### Disclaimer | ||
|
||
Users are expected to know how to administer their system, and these files | ||
should be considered as only a guide or suggestion to setup CKB. |
Oops, something went wrong.