Execute two commands as a single action in windows command prompt - windows

We have a system where an application A starts the second application B to run in background (no gui or console window should be shown). However, the first application should be able to execute a command and continue working without waiting for the second one to finish. After B has finished, an empty file is created to indicate this to application A. This is the requirement because this system is built so that it can run on both Windows and Linux machines as well as clusters and therefore the file cannot be directly created by application B.
Here is the equivalent Linux command issued by application A:
{ B; touch done.txt; } &
The question is how to produce the same effect in Windows?

Related

Start/stop process in embedded linux

I have my own embedded Linux system on PocketBeagle board. I have developed a simple gpio application in C that issues an on/off command to one of the pins of the connectors of the board. The application is called gpio_aa6 and located at /root.
The first challenge was to find a way to launch my application automatically after booting the board. I found two ways to do that; the first was to add an entry to etc/rcS directory. This entry is a simple script file that launches my application. The second way was to edit /etc/inittab file and add an entry to that file (::respawn:/root/gpio_aa6). In both these ways my application was launched successfully: but I am still not sure if this is the right way to launch my application automatically.
Then I came to the second challenge, how can I stop my running application, as the respawn re-launches the application if it's terminated?
I am communicating with the board in two ways; via a serial communication (using screen terminal) and via web sever (root#192.168.42.2). I have tried to use Ctrl+C, Ctrl+Z, Ctrl+\, but couldn't stop the program from being continue running. Then I used command "killall" with killsignals -9 or -15, it seems that the program is interrupted but it's launched again directly after that.
My application is to run infinitely, but I need to stop it sometimes to update it and re-launch it again.
Is there any suggestion how to overcome this problem?
Thanks.
Both solutions you have used are correct. I personally prefer the option of adding an init script to /etc/init.d though.
I believe the behavior that you observe that you apparently can't kill the program is because you are starting your program from inittab, with the respawn keyword, which precisely tells the init program to restart your application when it exits. If you actually check the PID of your application, you will see that it changes everytime you kill it.
Therefore, I would recommend you to use an init script instead, with which you can implement start and stop actions. See ./package/lldpd/S60lldpd for a basic example in Buildroot.

How to launch a non-interactive process from an interactive session

The goal is to be able to test run a PowerShell script non-interactively with as little ceremony as possible.
It purposely runs differently on the CI server than it does when I run it interactively and I'd like to debug it. Ideally I could test this without having to set up a scheduled task. A command line utility would be fantastic.
What's different between the CI server (TFS build, non-interactive) and my machine (running interactively) is the value of [Environment]::UserInteractive. When I type powershell -c [Environment]::UserInteractive at the command line, I get True as expected. The goal is to be able to type someutility powershell -c [Environment]::UserInteractive and have it print False, just like it would print running under TFS build.
I'm willing to write the someutility in C++ if someone can outline how this works. I'm researching but an hour hasn't yielded anything. Everyone is already running non-interactive or wants to launch interactive, and I'm in the exact reverse scenario. It seems that there should be a way to do this from a non-administrator command line since the launched process should have the same user permissions but be restricted to non-interactive.
So far, I can get this desired [Environment]::UserInteractive = false behavior using a scheduled task and picking "Run whether user is logged on or not." But it's a pain, and I can't see the non-interactive stdout which I know is possible because TFS build shows it live. Also, the scheduled task doesn't end when run non-interactively so it's hard to tell when it's done.
The reference code for UserInteractive can be found here. It sets the property to false if and only if the window station for the process does not have the WSF_VISIBLE flag set. So your utility should create a new window station and desktop (see CreateWindowStation and CreateDesktop) and launch the child process there.
(Only the default window station for any given session is interactive, so presumably manually created window stations will not have WSF_VISIBLE set by default. If this does not turn out to be the case, you should be able to use SetUserObjectInformation to toggle the flag.)
To choose the window station and desktop for a child process, specify it in the lpDesktop member of the STARTUPINFO structure in the call to CreateProcess.

Is it possible to run a batch file when a program is launched?

I have a batch file that toggles aero. Is there a way that when you open a certain program the batch file will run when it is launched and run again when the program is closed?
Well yes, but on the Properties for the program you can tick a checkbox to do this on the compatability tab.
Find out info on what the browser is doing. Sart your program and then use Task manager to find your game (it is a real game and not some jscript web thing?).
Compatability layers are scritable.
See http://support.microsoft.com/kb/286705
set __compatlayer=256Color (note wrong spelling)
Running a program will have same problem as setting compatability. You have to find something to run it on.
Windows can start a debugger automatically when a program is started. You can substitute any program that can start a program (as a batch can) for the debugger.
You can also run a script that triggers on program start. Task Scheduler can run tasks when certain events occur.
But you have to know what exact object to trap. Use task manager in the first instance.

Install newer .NET and run a child process that uses it from one parent process?

I'm trying to automate some test runs in my company and constraint I operate under is that I have one process (that I can change to run whatever I want), but once it finishes automation cleans up machine.
I cannot change state of machine before this process starts.
Requirement for test is to install .NET 4.5 and then start another process that uses it.
Problem I'm facing is that second process cannot find some of .NET 4.5 dll's that get installed in one of the previous steps (I basically start installer process, it finishes and then I kick off tests).
If I remote desktop into that machine and start test process manually again it loads and works correctly.
So my quiestion is - is there any way to start test process as a child of "system" or can I make it reload environment somehow?

How to restart program automatically if it crashes in Windows?

How can I start my program automatically if it crashes on windows 2003 server? Sometimes my program just crashes, is there a way in windows or settings that I can set?
There are several ways to create a process supervisor/guardian process on Windows.
First, is to leverage windows command line capabilities. Create a bat file:
#echo off
:start
start /w "your app to watch.exe"
goto start
start /w will wait for the process to exit. When the process crashes and exits, the bat script will relaunch it.
Another option is to use free supervisor tool https://github.com/chebum/Supervisor. It allows to restart the crashed app, plus it allows to monitor two or more apps at once and it will automatically close these apps when supervisor's window is closed.
The usual approach is to run what is known as a guardian process. This is a separate process, often a service, that monitors the state of the main process. When the guardian detects that the main service has died, it re-spawns it.
To the very best of my knowledge, there is not built in Windows functionality to do this for you.
Notice: running self-looping bat files can be useful, but unless you know what you're doing, they can wreak all kinds of havoc. This goes especially if you run them on startup. You have been warned.
Anyway. I just remembered something from my 286 days, when I played around a lot with BAT files. If you write the file
yourprogram.exe
some other event
the BAT file will run yourprogram, and then pause and wait around in the background until the program exits. After that it will run "some other event". This used to be kind of annoying if you wanted to run multiple things at once, but here it's actually useful. Using this, it's possible to make it run a loop that restarts the program (and reruns the bat file) as soon as it exits. Combine this with https://superuser.com/questions/62525/run-a-completly-hidden-batch-file, and you'll never even see it happening.
The final BAT file ("restart.bat" in this example) will look something like:
c:\[location]\yourprogram.exe
wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
That's about it. Start the program (on startup via task or even just startup folder) with line 2, and this ought to solve your problem :)
Oh, if you want to stop the loop, just rename the bat file or put "// " in front of the two lines, save it, and exit the program.
If the program you are running requires admin rights, the solution I found was using psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to run both the program and the bat with elevated privileges. In that case the BAT will look like:
c:\[location]\psexec -h c:\[location]\yourprogram.exe
c:\[location]\psexec -h wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
Then you run the bat as administrator, or run the second line (without the psexec part) from task scheduler with elevated privileges. BEWARE: running it as a normal user and clicking "no" on the UAC prompt gave me a BSOD, probably because it looped "can't run program because of lacking privileges" a couple of billion times or something :)
You can use RegisterApplicationRestart.
"If you register for restart and the application encounters an
unhandled exception or is not responsive, the user is offered the
opportunity to restart the application; the application is not
automatically restarted without the user's consent. "
For automatic restart without user intervention, there is also RestartOnCrash. Works with all Windows versions.
I was looking for something similar. There are two options to handle this - either you can write a small script by yourself or use something that is already existing.
After some googling I came across this nice list. The blogger has compiled about 8 tools to automatically restart a crashed or closed application.
Unfortunately there are no settings in Windows to automatically restart a regular program when it crashes.
Do you need to actively interact with your application's GUI? Some of the Service Wrappers (designed to run any application as a Windows Service) will monitor your application and restart it when it fails, but be sure investigate Session 0 Isolation to ensure that it won't get in the way.
You may use some special app like BDV SystemEvents or any other. It allows you to specify application which will be started if some another application is closed. Specify the same application as a Condition and as an Action and you will get expected results.

Resources