Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32 - Run process in background || Detach from Console 1

Status
Not open for further replies.

SgtB

IS-IT--Management
Oct 3, 2002
447
US
I've got a perl script I'd like to run. Its an infinite loop script so starting it and leaving a command prompt open simply does not work for me. I'd like to be able to include something within the script that simply backgrounds the whole process.

I've been pointed to Win32::process, but that doesn't seem like it will work for me. The create process requires that you point it to an executable, and it creates the process out of that...not the perl script.

This particular project is going to use perl2exe too, so I need to keep everything contained in one file since the hosts this will be run on will not have perl installed.

Is there a standard way in Perl on Win32 to background processes or detach them from the console?

Thanks :)

I'll see your DMCA and raise you a First Amendment.
 
Can't even run that stuff. After installing it Here's the error I get.

Can't locate loadable object for module Win32::Daemon in @INC (@INC contains: C:
/Perl/lib C:/Perl/site/lib .) at daemon.pl line 1
Compilation failed in require at daemon.pl line 1

Even so, the issue seems to be the same here as with Win32::process (if I'm reading things correctly). To create the service (which I don't really want to do anyway) you need to specify the full path to the executable. In the examples thats perl.exe with the script as its arguement. Hosts won't have Perl installed though which is why I'm having to use perl2exe.

Thanks for the tip though.

At any rate, why is this such a problem? You'd think this would be a regular occurance. Having to run scripts in the background that is.

PS - Can't rely on scheduling using the at command either.

To describe what the end result is, we need the client to be able to simply double click the exe and forget about it. It will then run every time the user boots. The blank command prompt poses a significant problem to as users get click happy and would most likely close it every time they see it.

I'll see your DMCA and raise you a First Amendment.
 
What are the steps you took to install it?
 
Put them in the respective bin and site/lib directories.

Specifcally,
daemon.dll -> c:\perl\bin
daemon.pm -> c:\perl\site\lib\win32
Win32-Daemon.ppd -> c:\perl\site\lib\win32

daemon.pm in any other directory Perl cannot find it. With it in the above directory, I get the previously mentioned error.

Hopefully I'm doing something wrong here (chances are I am) because I'd like to work a little with this.

Running Activestate Perl 5.8.6
Perl2Exe does not work with 5.8.7

I'll see your DMCA and raise you a First Amendment.
 
Ok, lets try to install it another way. Do you have ppm working? If you do, take a look at:
faq219-3489
Specifically the one line:

PPM> rep add Roth
If you don't have ppm, report back. Otherwise, let us know how it goes with the FAQ.

X
 
Well I think I'll call it quits on the Win32::Daemon. Actually got this working with Win32::process...with a little weird thinking anyway.

Here's what I did. Total hack job, but it actually works.

use Win32::process;

$proc = shift;

if ($proc ne run) {
Win32::process::Create($Process,
"c:\\path\\to\\app.exe",
"app run",
0,
DETACHED_PROCESS,
".") || die "Create: $!";
} else {
<script that actually runs in the background>
}

See what happens here? A simple double click will contain no arguments to app.exe. Thus a new process is created with Win32::process, this time calling itself (app.exe) *with* an argument (run). The argument causes the if statement to be false and on it goes to the else statement which is the actual app I want to run in the background.

Presto! The executable created by perl2exe with the infinite loop runs in the background.

Thanks for the help on this one Xaqte. I was about to give up before I started in with Win32::Daemon. Messing with that gave me the idea on how to get this to work...not sure how, but it did. ;)

I'll see your DMCA and raise you a First Amendment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top