Mono restart console application - linux

I have written a console application in mono for linux.
i have to start it with
"sudo mono app.exe"
Is there some posibility to restart the app when something happens.
For examlpe I run this app on the raspberry Pi, and when the app
detects some voltage on a special IO pin the app should restart automatically.
Please help me.

You can launch a new application instance using methods in the Process class and later exit.
http://www.dotnetperls.com/process-start

On Windows you can use cmd.exe to execute the script. In this blog post the author asks cmd to wait for a while, and then delete the executable,
http://blog.pedroliska.com/2010/05/20/c-self-destruct-windows-app/
You can use the same trick to restart the executable if on Windows.
var startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
// The delay is just making sure the exe to delete is done
// running.
var delayPings = 2;
var exeName = AppDomain.CurrentDomain.FriendlyName;
process.StandardInput.WriteLine("(ping -n " + delayPings + " 127.0.0.1) && (CALL " + exeName + ")");
Now for Linux, you just need to use bash (or another Shell) to replace cmd and also modify the command passed to it.

Related

How to solve the driver hanging on sending IRPs to delete EXE files which require Administrator authority?

I want to delete files by sending IRP via a WDK driver. It works well for deleting all files no matter *.pdf or *.pptx, except some EXEs which require Administrator to run, such as EXE installer and setup.exe etc. I don't know why it hangs on IoCallDriver once I try to delete EXE installers. I have also tried to set a timeout for KeWaitForSingleObject but no luck, the driver keeps hanging on IoCallDriver.
Does Windows limit drivers to remove these Administrator authority EXEs? How to solve this? Thanks a lot.
NTSTATUS send_delete_file_irp(PFILE_OBJECT file_object) {
KEVENT event;
PDEVICE_OBJECT device_object = IoGetBaseFileSystemDeviceObject(file_object);
PIRP irp = IoAllocateIrp(device_object->StackSize, false);
// Set the complete routine that will free the IRP and signal the event
KeInitializeEvent(&event, SynchronizationEvent, false);
IoSetCompletionRoutine(
irp,
io_complete,
&event,
true,
true,
true);
FILE_DISPOSITION_INFORMATION file_disposition;
file_disposition.DeleteFile = true;
IO_STATUS_BLOCK io_status_block;
irp->AssociatedIrp.SystemBuffer = &file_disposition;
irp->UserEvent = &event;
irp->UserIosb = &io_status_block;
irp->Tail.Overlay.OriginalFileObject = file_object;
irp->Tail.Overlay.Thread = (PETHREAD)KeGetCurrentThread();
irp->RequestorMode = KernelMode;
IO_STACK_LOCATION* stack_location = IoGetNextIrpStackLocation(irp);
stack_location->MajorFunction = IRP_MJ_SET_INFORMATION;
stack_location->DeviceObject = device_object;
stack_location->FileObject = file_object;
stack_location->Parameters.SetFile.Length = sizeof(FILE_DISPOSITION_INFORMATION);
stack_location->Parameters.SetFile.FileInformationClass = FileDispositionInformation;
stack_location->Parameters.SetFile.FileObject = file_object;
IoCallDriver(device_object, irp);
KeWaitForSingleObject(&event, Executive, KernelMode, true, nullptr);
return STATUS_SUCCESS;
}

How to check whether a Node.js service is running - in my case Azurite emulator?

I am developing a C# application that should run in Azure. I want to use the Azurite emulator to test it locally. What I want to achieve is: Have my tests detect whether Azurite is running and abort quickly with a nice error message if it is not running.
Apparently Azurite runs on Node.js.
With the old Microsoft Azure Storage Emulator, I can check it like this:
public static class AzureStorageEmulatorDetector
{
public static bool IsRunning()
{
const string exePath = #"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe";
if (!File.Exists(exePath))
return false;
var processStartInfo = new ProcessStartInfo {FileName = exePath, Arguments = "status", RedirectStandardOutput = true};
var process = new Process {StartInfo = processStartInfo};
process.Start();
process.WaitForExit();
var processOutput = process.StandardOutput.ReadToEnd();
return processOutput.Contains("IsRunning: True");
}
}
I want to accomplish something similar with Azurite.
I have installed Azurite like this:
npm install -g azurite
I run it like this:
azurite --silent --location C:\temp\Azurite --debug c:\temp\Azurite\debug.log
I notice that the Azurite command-line application has no parameter that tells me whether it is already running. And when I start Azurite from the console I don't see any process or service in Task Explorer called anything like "azurite". So I don't know what process I'm supposed to check for.
EDIT: Apparently Azurite runs on Node.js. There is indeed a process called node.exe running, but that's not a sufficient condition. Can I query my running Node.js instance and get it to tell me what it is doing?
I am on Windows.
Does anyone know?
Inspired by the comment by Ivan Yang and this answer I did this:
private static bool IsAzuriteRunning()
{
// If Azurite is running, it will run on localhost and listen on port 10000 and/or 10001.
IPAddress expectedIp = new(new byte[] {127, 0, 0, 1});
var expectedPorts = new[] {10000, 10001};
var activeTcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
var relevantListeners = activeTcpListeners.Where(t =>
expectedPorts.Contains(t.Port) &&
t.Address.Equals(expectedIp))
.ToList();
return relevantListeners.Any();
}
EDIT: Alternatively, check out this thread on their GitHub for other possibilities: https://github.com/Azure/Azurite/issues/734

Azure Linux App Service with .Net Core Stack. Unable to use NodeJS

I am hosting a .NET Core Application on MS Azure (on a Linux Service Plan) and I want to run some NodeJS code in the .NET Core Application. I did this a while ago on a Windows Service Plan, there it was working. Now I am trying with a Linux Plan and it is not working.
First I was trying to use "Jering.Javascript.NodeJS" and then also "INodeServices" from Microsoft (which is obsolete). But "node" was not found. I also tried to start directly a Process (Code below), but also not working. "node" is not found.
var proc = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "node",
Arguments = " -v",
RedirectStandardOutput = true
}
};
result += "RUN: " + proc.StartInfo.FileName;
proc.Start();
var reader = proc.StandardOutput;
NodeJS is installed on the server and also the command works there but it seems that the .NET Core app is hosted as docker and does not have any access outside to run NodeJS. Image
I found a useful information here.
The problem is that Node is not present in the container so it is
necessary to have a script to install and start it before starting the
app itself.
Reproduceļ¼š
Here is my script:
//using System.Diagnostics;
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "bash";
//startinfo.FileName = "/etc/opt/nodejs/14.15.0/bin/node"; //it's no use even node package located here.
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//install and start nodejs
process.StandardInput.WriteLine("apt-get install curl");
process.StandardInput.WriteLine("curl -sL https://deb.nodesource.com/setup_12.x | bash");
process.StandardInput.WriteLine("apt-get install -y nodejs");
//Run "node -v"
process.StandardInput.WriteLine("node -v");
string line = string.Empty;
while (!process.StandardOutput.EndOfStream)
{
line = process.StandardOutput.ReadLine();
_logger.LogInformation(line);
}
process.WaitForExit();
return string.Empty;
It works on my .net Core app based on Linux.
I think I found a better solution ;)
In an app service you can mount a storage. In my case I mounted a storage, which contains the nodeJS lib.
Azure Portal Screenshot
Now i can execute the following code:
string result = "";
var proc = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "/externallibs/node/bin/node",
Arguments = " -v",
RedirectStandardOutput = true
}
};
result += "RUN: " + proc.StartInfo.FileName;
proc.Start();
var reader = proc.StandardOutput;
return result + reader.ReadToEnd();
You can create on azure portal an environment var named POST_BUILD_COMMAND with a command to fix your environment path.
Linux Service Plans runs on Oryx which is documented here
POST_BUILD_COMMAND=PATH=/usr/bin/node:$PATH

How to execute a .bat or exe file from CodedUI script using Process.Start()?

I am new to Coded UI. I have written a simple code to execute a .bat file from a CodedUITestMethod1() as below:
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.FileName = #"C:\BVTBatch\PlayBack.bat";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
strException = thisProcess.StandardOutput.ReadToEnd();
Problem statement: When I debug the script, it gets executed but the batch file does not run. I tried executing iexplorer.exe, and observed same issue. The script gets executed with pass, but IE browser does not start.
However if I execute the same code from other console application or Unit Test project method, it gets executed successfully.
Can someone suggest what is the reason behind this? and how can we fix this in CodedUI?
Thanks in advance.
This would seem legit:
thisProcess.StartInfo.FileName = ("C:\BVTBatch\PlayBack.bat");
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = new ProcessStartInfo ("C:\BVTBatch\PlayBack.bat");
p.Start();

Haxe Run System Commands as an adminstrator

As I am very new to haxe.
The following is my program in haxe where I am trying to retrieve the list of files opened in windows client. Openfiles is the command which gives the list of files opened in windows machine , which needs to be executed as an administrator. I am failing to execute the program which is giving no output.
class Hello {
public static function main() {
trace("Hello World!");
if(Sys.systemName()=="Windows"){
//var x = Sys.command("Openfiles",[]);
var output = new sys.io.Process("ipconfig", []).stdout.readAll().toString();
trace("output:::"+output);
}
if(Sys.systemName()=="Linux"){
//var x = Sys.command("Openfiles",[]);
var output = new sys.io.Process("ifconfig", []).stdout.readAll().toString();
trace("output:::"+output);
}
}
}
How to execute Openfiles system command as an administrator ?
for Linux you can do this:
var output = new Process("bash", ["-c 'echo rootS_PASswoRd | sudo -S ifconfig'"]).stdout.readAll().toString();
trace("output:::"+output);

Resources