I have a Win32 console application (built from Visual Studio as Win32 console project) which does some log file (.txt) processing. I have a separate perl program (legacy program) which now needs to start this Win32 console application and then stop when done.
Perl program starts an instance of Win32 console app using Win32::Process APIs. It can kill the console app when done by either "kill -x pid" or Win32:Process:Kill. The problem is console app needs to know if its being killed/terminated so that it can flush log handling. The console app has already registered a handler via SetConsoleCtrlHandler API but doesn't get called when killed from perl program by say kill -2/3 pid.
What do I change in perl program or in Win32 console app so that it can know when its being terminated?
Thanks!
Signal handling in Windows is a little quirky if you're used to Unix. I have done a lot of investigation into this, and wrote up my findings here (starting at line 261).
Short answer: Windows processes can set $SIG{INT}, $SIG{QUIT}, or $SIG{BREAK}. All other signal handlers are ignored. Signal them from you separate app with the builtin kill:
kill 'INT', $the_win32_logger_pid;
kill 'QUIT', $the_win32_logger_pid;
kill 'BREAK', $the_win32_logger_pid;
Related
I am new to perl and CPAN. I see that perl is installed in my system.
However, I would like to install DBI package to run a script called INSTALL.PL.
When I type CPAN, I get the below error message
Loading internal null logger. Install Log::Log4perl for logging messages
Terminal does not support AddHistory.
There seems to be running another CPAN process (pid 3986). Contacting...
Other job not responding. Shall I overwrite the lockfile '/home/abcd/.cpan/.lock'? (Y/n) [y]
If I give no, I don't get the CPAN command prompt. Whereas if I give yes, I get the below command prompt
nolockcpan[1]
May I know what does the error message mean and what are the steps to follow to avoid this message again?
Can't I enter into cpan mode and press CTRL+Z exit and again get into cpan mode?
How can I avoid this error message and when can this happen?
It means that cpan wasn't properly exited the last time it was run. This could happen if you close your terminal while it's still running, for example.
You can use exit or quit to exit cpan. It will also exit when its STDIN reaches EOF (which can be done using Ctrl-D on unix).
You talk of Ctrl-Z, which stops (suspends) it, but doesn't exit it. Use fg to return to cpan after stopping it. That said, launching cpan while it's stopped or otherwise still running results in a different message (Other job is running. Shall I try to run in downgraded mode?).
I set the path to a program, say "foo.exe", to my system path and so typing foo in cmd/powershell starts the program. However when I type exit to get out of cmd/powershell foo.exe also closes with it. Why does this happen and how do I prevent this from happening?
This doesn't happen for all programs, only certain ones which means those certain ones should be added to path in a different way probably or should be started in a different way I'm guessing. However, searching over the internet for a long time didn't give me anything so a little help would be appreciated.
If foo.exe is a console application (one compiled for the Windows console subsystem), it will run synchronously in cmd.exe / PowerShell: that is, control won't be returned to the calling shell until the application exits. This means that you won't even get a chance to type exit until foo.exe has already exited.
However, it is possible to run a console application asynchronously, namely if you use a job to run it, via Start-Job or Start-ThreadJob; that is, foo.exe will then run in the background.
In that event, exiting the calling shell with exit will terminate the foo.exe process.
To prevent that, you can use the Start-Process cmdlet instead; on Windows, you can use it to launch foo.exe directly, which will open in a new console window by default; on Unix-like platforms, you must launch it via the nohup utility (which sends the program's output to a file named nohup.out in the current directory).
By contrast, if foo.exe is a GUI-subsystem application, it launches asynchronously and independently of the calling shell: that is, control returns to the calling shell right after successful creation of the new process, and exiting the shell has no effect on that new process.
I'm trying to convert some Ruby code to Swift. The Ruby code launches external programs with
system ...
I've managed to get almost the same behavior with the Process API in Swift:
Process
.launchedProcess(launchPath: ..., arguments: ...)
.waitUntilExit()
However, there are two differences:
The Swift version hangs when the launched program tries to do paging (so launching "git --no-pager diff ..." works, "git diff ..." hangs if the diff is long enough).
If I cmd+c to interrupt while the launched program is running, my Swift script exits but the launched program keeps running. In the Ruby version, both are interrupted.
How would I avoid these issues?
Update: worked around issue #2 by manually terminating any launched processes when my script receives an interruption signal:
var globalCommand: Process? = nil // May be set later
signal(SIGINT) { _ in
globalCommand?.interrupt()
exit(0)
}
I am using lot of portable application locally on my laptop and if i leave some of them opened when i shutdown Windows it doesn't close them properly and some applications don't save their settings. I am trying to use taskkill option in a autohotkey script which will be executed on windows shutdown and will close properly opened portable application.
I am using this script:
#SingleInstance, force
#Persistent
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
OnExit , closepgs
return
closepgs:
If (A_ExitReason="Shutdown" or A_ExitReason="Logoff")
{
Run, taskkill.exe /F /IM uTorrent.exe
}
ExitApp`
But when i shutdown Windows i receive this taskkill.exe error:
"Application failed to initialize properly (0xc0000142). Click on OK to terminate the application"
If i use the taskkill code outside the script like this it works fine:
^!Home::
Run, taskkill.exe /F /IM uTorrent.exe
But if i use it inside the script i receive taskkill.exe error.
Any idea why this error happening?
I have Windows XP Pro SP3.
Use Process, Close instead of taskkill.exe:
Process, Close, uTorrent.exe
It seems that as of Windows Vista and later, processes aren't allowed to spawn new processes during the shutdown phase. The only source a quick web search yielded was this one.
On a different note, using native functionality instead of calling programs is almost always preferable. Especially when using AHK, I recommend always looking for a built-in function to start with.
I need to build a test bench by sending appropriate inputs to an application. However,
once I launch the application, it takes control and does not return to the command
prompt (unless an exit command is executed from the application). In that case
is there any technique by which I can send a command to that application from the Perl
script and interpret the output from that application?
My operating system is Windows.
If it's a GUI application, take a look at the Win32::GuiTest module. It sends events to GUI applications - simulating user input.
For a command line application, I would normally recommend the Expect module. Unfortunately, Expect doesn't work under Windows.
If there is anyway to write or redirect the application output to a file, you can always open that file to process/interpret the output. If you are talking about a command-line application, it should be easy to redirect the terminal output to a file using the '>' and '>>' characters. It may not be as easy with a GUI app, though.