Skip to content

Commit

Permalink
Daemon run as linux service
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Dec 8, 2024
1 parent 961a043 commit 7ef9dda
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions platforms/unix/daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ ezrtc = "0.5.0"
webrtc = "0.11"
async-channel = "2.3.1"
wol-rs = "1"
clap = { version = "4.5.23", features = ["derive"] }
23 changes: 23 additions & 0 deletions platforms/unix/daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::{
routing::get,
Router,
};
use clap::Parser;
use ezrtc::host::EzRTCHost;
use ezrtc::socket::DataChannelHandler;
use futures::{sink::SinkExt, stream::StreamExt};
Expand All @@ -31,6 +32,8 @@ use webrtc::data_channel::RTCDataChannel;
use webrtc::ice_transport::ice_server::RTCIceServer;
use wol::{send_wol, MacAddr};

mod service;

#[derive(Serialize, Deserialize)]
pub struct GenericMessage<T> {
pub r#type: String,
Expand All @@ -44,8 +47,20 @@ pub struct AppState {
settings: Settings,
}

/// Modern hardware monitor with remote monitoring.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Setup coresd to run as a service
#[arg(required = false, long, short = 's')]
service: bool,
}

#[tokio::main]
async fn main() {
// Parse arguments
let args = Args::parse();

// Logger
CombinedLogger::init(vec![TermLogger::new(
LevelFilter::Info,
Expand All @@ -55,6 +70,14 @@ async fn main() {
)])
.unwrap();

if args.service {
service::setup_service();
} else {
warn!(
"You are running coresd as an executable, to setup it as a service, run `sudo ./coresd --service`"
);
}

// Get settings
let settings = get_settings();
info!("Connection code: {:?}", settings.connection_code);
Expand Down
57 changes: 57 additions & 0 deletions platforms/unix/daemon/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use log::{error, info};

pub fn setup_service() {
// copy current executable to /bin as sudo
let current_exe = std::env::current_exe().expect("Failed to get current executable");
let status = std::fs::copy(current_exe, "/bin/coresd");

if let Err(e) = status {
error!(
"Failed to copy executable to /bin, please run as root! Error: {}",
e
);
std::process::exit(1);
}

let file_contents = "[Unit]
Description=coresd
After=network.target
[Service]
User=root
ExecStart=/bin/coresd
Restart=always
[Install]
WantedBy=multi-user.target";

// create service file
let status = std::fs::write("/etc/systemd/system/coresd.service", file_contents);

if let Err(e) = status {
error!(
"Failed to create coresd service file, please run as root! Error: {}",
e
);
std::process::exit(1);
}

// enable service
std::process::Command::new("sudo")
.arg("systemctl")
.arg("enable")
.arg("coresd")
.status()
.expect("Failed to enable coresd service");

// start service
std::process::Command::new("sudo")
.arg("systemctl")
.arg("start")
.arg("coresd")
.status()
.expect("Failed to start coresd service");

info!("Service created successfully, please run `sudo systemctl status coresd` for more information");
std::process::exit(0);
}

0 comments on commit 7ef9dda

Please sign in to comment.