Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Windows service from PERL script on Windows 7 64-bit

Does anybody have an example of how to create a windows service on windows 7 64-bit from PERL script?

On windows XP professional 32-bit, I have created windows service successfully with Win32::Daemon which has call back functions. This doesn't work on 64-bit.

I have seen this http://nssm.cc/usage and created a service using that but it doesn't keep state and it gives errors. If anybody has a proper example perhaps ...

Some idea much appreciated.

like image 344
user3480788 Avatar asked Oct 17 '25 04:10

user3480788


1 Answers

I managed to solved this now.

I now have my perl script running as a windows service now on windows 7 64-bit.

Basically win32::Daemon works on win 7 64-bit but the service creation needs to be done manually. I.e. the callback functions and start up is fine.

Here is a example to create perl windows service from scratch.

  1. Create folder c:/myservice

  2. Copy the code example from this link and save it to the directory above. Call it myservice.pl (http://www.roth.net/forums/topic.php?id=106)

  3. Add these two lines after the print hello statement in the script.

    $Context->{last_state} = SERVICE_RUNNING;

    Win32::Daemon::State( SERVICE_RUNNING );

    This is needed to keep the service running. Otherwise it stops.

  4. Open a Dos cmd terminal in admin mode. Create service using following command

% sc create myservice binpath= "c:\strawberry\perl\bin\perl.exe"

It will display following message on success. [SC] CreateService SUCCESS

  1. Now we need to edit the registry. Open registry editor. (start -> then type regedit)

  2. Find the service under HKEY_LOCAL_MACHINE->SYSTEM->CurrentControlSet->Services->myservice

  3. Click on 'myservice' and edit the imagePath variable to be : c:\strawberry\perl\bin\perl.exe -I "C:\myservice" "C:\myservice\myservice.pl" --run

  4. Now open the services window and start the service. (start->control panel -> Administrative tools -> services)

  5. In the current directory of the script a log is created and updated every couple of seconds. If using cygwin for windows, you can tail it.

    % tail -f *.log

Process will print Hello! periodically ...

Thanks.

like image 76
user3480788 Avatar answered Oct 19 '25 01:10

user3480788



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!