pub fn spawn(&self, vars: Option<&GlobalVars>) -> std::io::Result<u32> {
        let cmd = &self.command[0];
        let args = &self.command[1..];
        trace!("script is called with arguments: {:?}", &self.command);

        // optionally use args to start the script
        let mut child = match vars {
            None => Command::new(cmd)
                .args(args)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?,
            Some(vars) => {
                trace!("script is called with extra vars: {:?}", vars);
                Command::new(cmd)
                    .envs(vars)
                    .args(args)
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .spawn()?
            }
        };

        // now it's safe to unwrap to get pid
        let pid = child.id();
        info!("script:{:?} started, pid:{}", &self.command, pid);

        // wait for timeout
        self.sleep();

        // if async, don't wait and just leave
        if self.async_flag {
            trace!("async flag set, returning with pid:{}", pid);
            return Ok(pid);
        }

        // try to get the exit status
        match child.try_wait() {
            Ok(Some(status)) => {
                if !status.success() && self.exit_on_error {
                    #[cfg(not(test))]
                    Nagios::exit_unknown(&format!(
                        "script:{:?}, pid:{}, exit_on_error is set and exit code is:{} ",
                        &self.command,
                        pid,
                        status.code().unwrap()
                    ));
                    #[cfg(test)]
                    std::process::exit(0);
                } else {
                    info!(
                        "script:{:?}, pid:{}, exit code is:{} ",
                        &self.command,
                        pid,
                        status.code().unwrap()
                    );
                    #[cfg(test)]
                    std::process::exit(0);
                }
            }
            Ok(None) => {
                let result = child.kill();
                info!(
                    "script:{:?}, pid:{}, timeout occured, pid kill() result={:?}",
                    &self.command, pid, result
                );
                #[cfg(not(test))]
                Nagios::exit_unknown(&format!(
                    "script: {:?} timed-out, pid:{}",
                    &self.command, pid
                ))
            }
            Err(e) => Nagios::exit_unknown(&format!(
                "script {:?} couldn't start, error={} !",
                self.command, e
            )),
        }

        let output = child.wait_with_output().expect("failed to wait on child");
        info!(
            "stdout={:?}, stderr={:?}",
            std::str::from_utf8(&output.stdout),
            std::str::from_utf8(&output.stderr)
        );
        Ok(pid)
    }
/// Run command and optionnally wait for timeout
Run command and optionnally wait for timeout
Run command and optionnally wait for timeout
pub fn spawn(&self, vars: Option<&GlobalVars>) -> std::io::Result<u32> {
        let cmd = &self.command[0];
        let args = &self.command[1..];
        trace!("script is called with arguments: {:?}", &self.command);
        // optionally use args to start the script
        let mut child = match vars {
            None => Command::new(cmd)
                .args(args)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .spawn()?,
            Some(vars) => {
                trace!("script is called with extra vars: {:?}", vars);
                Command::new(cmd)
                    .envs(vars)
                    .args(args)
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .spawn()?
            }
        };
        // now it's safe to unwrap to get pid
        let pid = child.id();
        info!("script:{:?} started, pid:{}", &self.command, pid);
        // wait for timeout
        self.sleep();
        // if async, don't wait and just leave
        if self.async_flag {
            trace!("async flag set, returning with pid:{}", pid);
            return Ok(pid);
        }
        // try to get the exit status
        match child.try_wait() {
            Ok(Some(status)) => {
                if !status.success() && self.exit_on_error {
                    #[cfg(not(test))]
                    Nagios::exit_unknown(&format!(
                        "script:{:?}, pid:{}, exit_on_error is set and exit code is:{} ",
                        &self.command,
                        pid,
                        status.code().unwrap()
                    ));
                    #[cfg(test)]
                    std::process::exit(0);
                } else {
                    info!(
                        "script:{:?}, pid:{}, exit code is:{} ",
                        &self.command,
                        pid,
                        status.code().unwrap()
                    );
                    #[cfg(test)]
                    std::process::exit(0);
                }
            }
            Ok(None) => {
                let result = child.kill();
                info!(
                    "script:{:?}, pid:{}, timeout occured, pid kill() result={:?}",
                    &self.command, pid, result
                );
                #[cfg(not(test))]
                Nagios::exit_unknown(&format!(
                    "script: {:?} timed-out, pid:{}",
                    &self.command, pid
                ))
            }
            Err(e) => Nagios::exit_unknown(&format!(
                "script {:?} couldn't start, error={} !",
                self.command, e
            )),
        }
        let output = child.wait_with_output().expect("failed to wait on child");
        info!(
            "stdout={:?}, stderr={:?}",
            std::str::from_utf8(&output.stdout),
            std::str::from_utf8(&output.stderr)
        );
        Ok(pid)
    }
pub fn spawn ( & self , vars : Option < & GlobalVars > ) -> std :: io :: Result < u32 > { let cmd = & self . command [ 0 ] ; let args = & self . command [ 1 .. ] ; trace ! ( " " self command ) ; // optionally use args to start the script let mut child = match vars { None => Command :: new ( cmd ) . args ( args ) . stdout ( Stdio :: piped ( ) ) . stderr ( Stdio :: piped ( ) ) . spawn ( ) ? , Some ( vars ) => { trace ! ( " " vars ) ; Command :: new ( cmd ) . envs ( vars ) . args ( args ) . stdout ( Stdio :: piped ( ) ) . stderr ( Stdio :: piped ( ) ) . spawn ( ) ? } } ; // now it's safe to unwrap to get pid let pid = child . id ( ) ; info ! ( " " self command pid ) ; // wait for timeout self . sleep ( ) ; // if async, don't wait and just leave if self . async_flag { trace ! ( " " pid ) ; return Ok ( pid ) ; } // try to get the exit status match child . try_wait ( ) { Ok ( Some ( status ) ) => { if ! status . success ( ) && self . exit_on_error { # [ cfg ( not ( test ) ) ] Nagios :: exit_unknown ( & format ! ( " " self command pid status code ( ) unwrap ( ) ) ) ; # [ cfg ( test ) ] std :: process :: exit ( 0 ) ; } else { info ! ( " " self command pid status code ( ) unwrap ( ) ) ; # [ cfg ( test ) ] std :: process :: exit ( 0 ) ; } } Ok ( None ) => { let result = child . kill ( ) ; info ! ( " " self command pid result ) ; # [ cfg ( not ( test ) ) ] Nagios :: exit_unknown ( & format ! ( " " self command pid ) ) } Err ( e ) => Nagios :: exit_unknown ( & format ! ( " " self command e ) ) , } let output = child . wait_with_output ( ) . expect ( " " ) ; info ! ( " " std str from_utf8 ( output stdout ) std str from_utf8 ( output stderr ) ) ; Ok ( pid ) }
Run command and optionnally wait for timeout
Run command and optionnally wait for timeout
&self
vars
Option<&GlobalVars>
&self
vars
Option<&GlobalVars>