/// <exception cref="System.Exception"/> public virtual void TestShellCommandTimeout() { if (Shell.Windows) { // setExecutable does not work on Windows return; } string rootDir = new FilePath(Runtime.GetProperty("test.build.data", "/tmp")).GetAbsolutePath (); FilePath shellFile = new FilePath(rootDir, "timeout.sh"); string timeoutCommand = "sleep 4; echo \"hello\""; PrintWriter writer = new PrintWriter(new FileOutputStream(shellFile)); writer.WriteLine(timeoutCommand); writer.Close(); FileUtil.SetExecutable(shellFile, true); Shell.ShellCommandExecutor shexc = new Shell.ShellCommandExecutor(new string[] { shellFile.GetAbsolutePath() }, null, null, 100); try { shexc.Execute(); } catch (Exception) { } //When timing out exception is thrown. shellFile.Delete(); Assert.True("Script didnt not timeout", shexc.IsTimedOut()); }
private static bool IsSetsidSupported() { if (Org.Apache.Hadoop.Util.Shell.Windows) { return(false); } Shell.ShellCommandExecutor shexec = null; bool setsidSupported = true; try { string[] args = new string[] { "setsid", "bash", "-c", "echo $$" }; shexec = new Shell.ShellCommandExecutor(args); shexec.Execute(); } catch (IOException) { Log.Debug("setsid is not available on this machine. So not using it."); setsidSupported = false; } finally { // handle the exit code if (Log.IsDebugEnabled()) { Log.Debug("setsid exited with exit code " + (shexec != null ? shexec.GetExitCode( ) : "(null executor)")); } } return(setsidSupported); }
/// <exception cref="System.Exception"/> public virtual void TestShellCommandTimerLeak() { string[] quickCommand = new string[] { "/bin/sleep", "100" }; int timersBefore = CountTimerThreads(); System.Console.Error.WriteLine("before: " + timersBefore); for (int i = 0; i < 10; i++) { Shell.ShellCommandExecutor shexec = new Shell.ShellCommandExecutor(quickCommand, null, null, 1); try { shexec.Execute(); Fail("Bad command should throw exception"); } catch (Exception) { } } // expected Thread.Sleep(1000); int timersAfter = CountTimerThreads(); System.Console.Error.WriteLine("after: " + timersAfter); Assert.Equal(timersBefore, timersAfter); }
/// <summary>Static method to execute a shell command.</summary> /// <remarks> /// Static method to execute a shell command. /// Covers most of the simple cases without requiring the user to implement /// the <code>Shell</code> interface. /// </remarks> /// <param name="env">the map of environment key=value</param> /// <param name="cmd">shell command to execute.</param> /// <param name="timeout">time in milliseconds after which script should be marked timeout /// </param> /// <returns>the output of the executed command.o</returns> /// <exception cref="System.IO.IOException"/> public static string ExecCommand(IDictionary <string, string> env, string[] cmd, long timeout) { Shell.ShellCommandExecutor exec = new Shell.ShellCommandExecutor(cmd, null, env, timeout); exec.Execute(); return(exec.GetOutput()); }