コード例 #1
0
        /// <summary>
        /// Create a detached process
        /// </summary>
        /// <param name="fileName">File name to execute</param>
        /// <param name="arguments">Arguments</param>
        public static void CreateDetachedProcess(string fileName, string arguments)
        {
            if (OSUtility.Instance.IsWindows)
            {
                var processInformation = new ProcessUtility.PROCESS_INFORMATION();
                var startupInfo        = new ProcessUtility.STARTUPINFO();
                var sa = new ProcessUtility.SECURITY_ATTRIBUTES();
                sa.Length = Marshal.SizeOf(sa);
                CreateProcess(null, "\"" + fileName + "\" " + arguments, ref sa, ref sa, false,
                              DETACHED_PROCESS,
                              IntPtr.Zero, Path.GetDirectoryName(fileName), ref startupInfo, out processInformation);
            }
            else
            {
                ProcessStartInfo info = new ProcessStartInfo
                {
                    // Linux uses " &" to detach the process
                    Arguments        = arguments + " &",
                    CreateNoWindow   = true,
                    FileName         = fileName,
                    UseShellExecute  = false,
                    WindowStyle      = ProcessWindowStyle.Hidden,
                    WorkingDirectory = Path.GetDirectoryName(fileName)
                };

                Process.Start(info);
            }
        }
コード例 #2
0
ファイル: ProcessUtility.cs プロジェクト: s4ndxyz/IPBan
        /// <summary>
        /// Create a detached process
        /// </summary>
        /// <param name="fileName">File name to execute</param>
        /// <param name="arguments">Arguments</param>
        public static void CreateDetachedProcess(string fileName, string arguments)
        {
            if (OSUtility.IsWindows)
            {
                Logger.Warn("Running detached process {0} {1}", fileName, arguments);

                var processInformation = new ProcessUtility.PROCESS_INFORMATION();
                var startupInfo        = new ProcessUtility.STARTUPINFO();
                var sa = new ProcessUtility.SECURITY_ATTRIBUTES();
                sa.Length = Marshal.SizeOf(sa);
                CreateProcess(null, "\"" + fileName + "\" " + arguments, ref sa, ref sa, false,
                              DETACHED_PROCESS,
                              IntPtr.Zero, Path.GetDirectoryName(fileName), ref startupInfo, out processInformation);
            }
            else
            {
                // ensure process is executable
                OSUtility.StartProcessAndWait("sudo", "chmod +x \"" + fileName + "\"");

                // use Linux at, should have been installed earlier
                ProcessStartInfo info = new()
                {
                    // Linux uses nohup and " &" to detach the process
                    // sudo -b to force it into the background
                    Arguments        = "-c \"echo sudo \\\"" + fileName + "\\\" " + arguments.Replace("\"", "\\\"") + " | at now\"",
                    CreateNoWindow   = true,
                    FileName         = "/bin/bash",
                    UseShellExecute  = false,
                    WindowStyle      = ProcessWindowStyle.Hidden,
                    WorkingDirectory = Path.GetDirectoryName(fileName)
                };

                Logger.Warn("Running detached process {0} {1}", info.FileName, info.Arguments);

                // do not get a reference and do not dispose, this process is orphaned from this process
                Process.Start(info);
            }
        }
    }