Attach to running program with powershell - c#

I have a Raspberry Pi v2 running Windows 10 IoT and am trying to run a dotnet 2.0 console application on it.
I have a powershell script start it on startup, but then I do not get the console output from it. I was wondering if there was a way to attach to the running process with powershell on my computer, like you would to a docker container, to get the console output.
I do not want to run my program on my computer, and the Raspberry Pi is the only computer I want to leave on overnight to run the program.

If you can't or don't want to re-start your Application
You are rather limited in this case, since, as stated in another SE Answer by Harry Johnston:
Windows does not provide any mechanism to retroactively give another
process a standard output handle if it was created without one.
(Besides, in most cases an application will check the standard output
handle only during startup.)
If you need to read something that already has been written to the Console, you are probably out of luck.
You should however be able to get any future output by attaching to the process with debugging tools.
In case of the Visual Studio Remote Debugger, this would even allow you to debug the execution of your Console Application, provided you wrote it.
An accepted answer on Superuser.com suggests that StraceNT is also capable of read the output of a running Console Application. This will probably be your best bet if the running Application wasn't written by yourself.
If the Application can be stopped and restarted
Powershell Remoting is probably the best way.
You would need to enable Powershell Remoting on your Raspberry Pi, and then you can use enter-pssession [RaspberryPi-Hostname-or-IP] in your local Powershell console to connect to your Raspberry Pi.
At that point you can just start your console application as if you were running it on your local machine and get all the console output you want.
No need to attach to anything.

You can redirect the output of your console app to a txt file in the current directory. If you run the console app from powershell you can do it like this:
.\[YOURAPP].exe | tee test.txt
And you can access the test.txt from Windows file sharing.

Related

C#/dotnet - How to detect if a previously executed process is running on Linux?

I have a dotnet console application which executes other dotnet console applications only if they are not running.
In windows if I run like following (via process.Start()):
dotnet run Bookstore.SendSignUpEmails
dotnet run Bookstore.SetDatabaseFields
It set process names on Windows like Bookstore.SendSignUpEmails and Bookstore.SetDatabaseFields where I can find them easily by Process.GetProcessesByName and detect if they are still running or not.
But on Linux processes named dotnet and I cannot find any metadata around them. So I cannot determine if I should run those projects again or not.
So, how can I detect whether a project executed before is still running or not? Should I maybe write to a file then check it there? I still see there can be some race conditions and not very safe. Any suggestions?

Console Application, detect launched from an exe file or from terminal

I developed an console application, with the dotnet framework.
I want the application to run when called in a terminal session and when executing the .exe file.
Now I got a problem with the design of my application.
When launching the exe, I need to ask if the program should be terminated, so that the user can read the output of my application (otherwise the application and therefore the terminal would close before any human could read what hs been printed).
But exactly this process of asking before the application stops executing is annoying if the application has been launched from the terminal, let's say by running dotnet myapp.dll.
This is because when launching a terminal session the terminal doesn't close after an application has terminated.
So my question is if there exists any advice/ best practice/ or if there is a way of detecting if the application has been launched via an exe file or in the terminal.
Thanks for your advice.

How remote debug C#/.Net app on Linux when the app is run under a different user?

I'm running a .Net Core microservice on Linux (Ubuntu) and am trying to remote debug with Visual Studio over SSH. But the service is run under the user svcuser and my user is mainuser. Main user is in the same group as the service user.
In visual studio, I can see the process that the service is running under, but when I try to attach I get:
One or more errors occurred. Failed to attach to process. The .Net Debugger has insufficient privileges to debug the process. To debug this process, vsdbg must be running with root permissions.
I checked in MS documentation but for Linux all they have is this: https://learn.microsoft.com/en-us/visualstudio/debugger/remote-debugging-dotnet-core-linux-with-ssh?view=vs-2019 which has no info on running the service with a different user
And the only info they have on fixing such a problem is for windows only: https://learn.microsoft.com/en-us/visualstudio/debugger/error-the-microsoft-visual-studio-remote-debugging-monitor-on-the-remote-computer-is-running-as-a-different-user?view=vs-2019
If you have sudo privileges then this is relatively easy, and can be kept secured to those users with sudo privs. Avoids needing to reconfigure users/environments, and allows you to debug any process on the machine regardless of which user account it is running as.
If you use Visual Studio to make an initial attempt to debug you will find that a ~/.vs-debugger folder has been created in the home directory of the user account you were attempting to use. This command will help you locate the vsdbg binary which was installed. You can install VsDbg manually but I find leveraging the automated process is easier. If you are using VSCode this becomes a manual process, and an exercize left for the reader, but I would still use VS2019 IDE to prep the target just to keep things consistent between tools.
find ~ | grep vsdbg
For my installation the binary is located at ~/.vs-debugger/vs2019/vsdbg and this path will most likely change over time.
First, rename the binary to something convenient:
mv ~/.vs-debugger/vs2019/vsdbg ~/.vs-debugger/vs2019/vsdbg-bin
Second, create a script to replace the binary:
touch ~/.vs-debugger/vs2019/vsdbg
chmod 770 ~/.vs-debugger/vs2019/vsdbg
nano ~/.vs-debugger/vs2019/vsdbg
The script content might look something like this, note the full path to vsdbg-bin, the use of $# ensures all command-line args passed to your script are forwarded to VsDbg.
#!/bin/bash
sudo ~/.vs-debugger/vs2019/vsdbg-bin $#
Now retry your debug session from Visual Studio, if you did things correctly you should be able to attach to any remote process on the target machine using SSH->VsDbg. "Works on my machine." ;) This was confirmed with VS2019 16.8.4, .NET 5.0, and VsDbg 16.9.20122.2 debugging an ASP.NET Core application running on Debian 5.4.8 (x64) launched by systemd under a service user account in Azure. "Sweet."
HTH!

Unable to run selenium in Jenkins [duplicate]

When I run my selenium test (mvn test) from jenkins (windows) I see only the console output. I don't see the real browsers getting opened . How can I configure jenkins so that I can see the browsers running the test?
I had the same problem, i got the solution after many attempts.
This solution works ONLY on windows XP
If you are using jenkins as a windows service you need to do the following :
1) In windows service select the service of jenkins
2) Open properties window of the service -> Logon-> enable the checkbox "Allow service to interact with desktop"
After then you should reboot the service jenkins
Hope this help you :)
UPDATE:
Actually, I'm working on a an automation tool using Selenium on Windows 10, I've installed Jenkins ver. 2.207 as windows application (EXE file), it's running as windows service and ALL drivers (Chrome, FireFox, IE) are visible during test executions WITHOUT performing a mere configuration on the System or Jenkins
I got the solution. I ran jenkins from command prompt as "java -jar jenkins.war" instead of the windows installer version. Now I can see my browser based tests being executed.
If you are already doing what #Sachin suggests in a comment (i.e. looking at the machine where Jenkins actually runs) and still do not see the browsers, then your problem may be the following:
If you run Jenkins as a service in the background it won't open apps in the foreground. You may either try to run it not as a service in the foreground, or run it as a Local System account and check Allow the service to interact with desktop option. In the latter case you may get into permission problems, though.
Update: To make sure this answer is understood properly by others: Jenkins Windows 'native' installation is not really native. It's a wrapper around Java that runs it as a service.
To interact with desktop GUI, you should launch slave agent via JNLP:
https://wiki.jenkins-ci.org/display/JENKINS/Distributed+builds#Distributedbuilds-LaunchslaveagentviaJavaWebStart
After adding the node in Jenkins (configured as Java Web Start launch), just make a startup batch script on the node machine:
java -jar slave.jar -jnlpUrl http://{Your Jenkins Server}:8080/computer/{Your Jenkins Node}/slave-agent.jnlp
(slave.jar can be downloaded from http://{Your Jenkins Server}:8080/jnlpJars/slave.jar)
See more answers here:
How to run GUI tests on a jenkins windows slave without remote desktop connection?
In the case of Windows 7 you should not install jenkins as windows application (because in this recent version, Microsoft decided to give services their own hidden desktop even you enable the functionality "interact with desktop" in jenkins service), you may have to deploy it from a war file as follows:
1) Download jenkins.war from Jenkins official site
2) Deploy it by the command prompt : java -jar {directoryOfJenkinsFile}/jenkins.war
3) Now you can access jenkins administration on http:// localhost:8080
Hope that helps you !
this is an issue for Jenkins. on Windows it is possible to access logon user's session (screen) under system account. to make the UI testing visible, Jenkins needs to bypass UAC (user access
control) at background. this solution works for me with my own service running as system account.
I also faced the same issue earlier in my local machine (Windows 10).
My test was running perfectly from the NetBeans but when I moved to Jenkins it was only running in console mode. I was unable to view the UI.
So for that, you just need to make your local machine as a Jenkins slave by creating a new slave node in your Jenkins and select that node to execute the Jenkins job.
If jenkins installed by windows installer it is showing only Console out put only. To see browsers download jenkins.war file and run java -jar jenkins.war from command line.
Go through this site:
http://learnseleniumtesting.com/jenkins-and-continuous-test-execution/
If you have the following situation,
You are able to login to the remote machine
You don't see the Jenkins agent window
This slave machine is accessed by many users then try the following,
then try the following suggestion.
Login to slave machine
Go to Task manager
Users
Logout all the users
Then login again.
This worked for me.

Launch a WPF application from a batch file or console application

I've made a WPF application based on .NET framework 4.
This application will be copied to a pendrive, and the client will have to run it from there.
In order to check if the client has Framework 4 installed, I've made a batch file that read the output of "clrver.exe", and if the framework 4 is installed it runs the application using the command
START "application path"
The problem is that few seconds after the WPF application is running it crashes.
So I've tried to make a C# Console application, just to check, and using Process.Start() I've started the WPF application. The behaviour is exactly the same.
If I try to run the WPF application by clicking directly on the executable it runs perfectly.
If I try to run it from a console, it crashes after few seconds (5~6 seconds).
Does anyone know the reason or an idea on how to run it?
The problem was dued by the working directory that cause some problem when the application is launched by a console application or a batch file.
I've solved replacing all relative paths in my code with absolute paths retrieved at runtime with: System.AppDomain.CurrentDomain.BaseDirectory

Categories

Resources