Run compiled.exe as a service?

Get help with using AutoHotkey and its commands and hotkeys
Peter2
Posts: 252
Joined: 21 Sep 2014, 14:38
Location: CH

Run compiled.exe as a service?

28 Oct 2015, 06:49

Hi

I made a script, compiled it to EXE, and it works fine. Now there is the request to "run it as service" (Windows Server 2008 R2).

So the question is: can it be done?

- some modifications in the code?
- add an interface / a wrapper / something else?
- complete recoding?
- can not be done in AHK?

Thanks and regards

Peter
Peter (AHK Beginner) / Win 7 x64, AHK Version v1.1.22.xx
lexikos
Posts: 6976
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Run compiled.exe as a service?

28 Oct 2015, 22:18

For what purpose?

As far as I'm aware, a service cannot send keystrokes, detect hotkeys, or any number of other things that ordinarily make AutoHotkey useful.
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

Re: Run compiled.exe as a service?

28 Oct 2015, 23:50

Agree... for what purpose? Maybe a scheduled task would do? You can execute scheduled tasks on user login, station unlock, or many many other triggers without needing to run it as a service. This might suffice to give the program wide availability and to load in the background even across different user logins (which in many cases is where a service might be handy, so just a guess at what something that might fill that gap).
Peter2
Posts: 252
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Run compiled.exe as a service?

29 Oct 2015, 02:00

Hi
the EXE I made does a kind of monitoring: it watches files and folders and starts and watches other programs. There is no user input necessary, no hotkeys, no keystrokes. It runs on Windows Server 2008 R2, on a virtual machine with remote-access.

It should run 24/7, start for every user (at the moment it starts via Startup folder for one single user-login), should also run after log-off.

If it all can be done with "scheduled tasks" please give me more information.

Thanks
Peter (AHK Beginner) / Win 7 x64, AHK Version v1.1.22.xx
JJohnston2
Posts: 193
Joined: 24 Jun 2015, 23:38

Re: Run compiled.exe as a service?

29 Oct 2015, 22:30

Just run Task Scheduler in Windows and try it out... built into the OS. Use Google if you get stuck on something. The usual. https://technet.microsoft.com/en-us/lib ... 66428.aspx
lexikos
Posts: 6976
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Run compiled.exe as a service?

30 Oct 2015, 00:13

It should run 24/7, start for every user [...], should also run after log-off.
I think what you mean is that it should start once with the system, not for each and every user. If it must run after log-off, it probably needs to be a service. If it started for a user, it would not run after log-off (unless it has multiple processes, running in each user session and as a service). I would look into third-party service managers, like NSSM. AutoHotkey itself is not designed to run as a service.
starts and watches other programs
If any of these programs are interactive and the script is not running in each user's session, the Run command will probably be insufficient.
Peter2
Posts: 252
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Run compiled.exe as a service?

30 Oct 2015, 02:05

Thanks for your advices.
lexikos wrote:I would look into third-party service managers, like NSSM.
Yes, I will take a look.
lexikos wrote:...If any of these programs are interactive and the script is not running in each user's session, the Run command will probably be insufficient.
The EXE starts a batchmanager which handles a CAD-software which prints to PDf (using driver). I suppose that all these has to run "as service" (or something like this), and that will be the problem ....
Peter (AHK Beginner) / Win 7 x64, AHK Version v1.1.22.xx
TXShooter
Posts: 156
Joined: 13 Dec 2017, 09:27

Re: Run compiled.exe as a service?

01 Mar 2018, 08:22

My apologies for reviving an older thread, but I too am interested in running a compiled EXE as a service, for the sole purpose of remotely telling that specific machine to reboot. Perhaps this could be a feature request (if it hasn't already... this post is a few years old)?

My idea is to have a service monitor one of the cloud folders for a specific file, read that file, test the validity of that file (pass code or something similar) and determine the reboot type, then enact that reboot type. However, I don't want there to be an icon in the system tray that could confused with other scripts that run on that computer that sometimes need to be interrupted before script completion. (Of course, this does depend on the internet and lan to be working like they should.)

For example, let's say that I have a machine that is having issues with VNC, and I can't access the files on it via FTP either, I would put a file on a common cloud folder with a command to reboot (ie, "Is this for me?" If yes then Shutdown Normal, authenticate code=something). That computer would continually monitor that cloud folder every minute or so for that file, and upon getting it read the file, validate, and perform two steps: 1) Delete requested file on the cloud, and 2) Shutdown 1, Sleep 60000, Shutdown 4 (if the computer hasn't rebooted in 5 minutes, force it to reboot).

Deleting the file serves two purposes; one is to keep the computer from rebooting after rebooting (preventing cyclic issues), and also to tell the remote requester that the command has been accepted. Perhaps a dialog box could pop up warning of the impending shutdown to let any user know it's going to happen, but this isn't necessary.

How should I go about making this a service, or how to take some other approach so as to not create a system tray icon and it runs above normal priority?
Guest

Re: Run compiled.exe as a service?

01 Mar 2018, 08:54

There is the #NoTrayIcon directive https://autohotkey.com/docs/commands/_NoTrayIcon.htm and you can run a script as administrator of course.
+ the Process command for setting the priority of a script (it that is needed)
User avatar
Flipeador
Posts: 1173
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Run compiled.exe as a service?

01 Mar 2018, 10:45

Hello :)
As said guest you can use #NoTrayIcon.
If someone is still interested in how to run a script as a service, follow these steps:
Spoiler
User avatar
Jovannb
Posts: 243
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Run compiled.exe as a service?

01 Mar 2018, 16:03

Hi

Ive some compiled Ahk-Scripts running in W2K8R2 Server and therefore I use RunasSvc.exe (as far as I know from Microsoft) and its quite simple.

Once set/installed now my ahk-exe's are Services.

J.B.
AHK-Release 1.1.30.03 Ansi 32-bit, Win10 (1903 18362.295) 64-bit, german
User avatar
Flipeador
Posts: 1173
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Run compiled.exe as a service?

01 Mar 2018, 18:49

Jovannb wrote:Ive some compiled Ahk-Scripts running in W2K8R2 Server and therefore I use RunasSvc.exe (as far as I know from Microsoft) and its quite simple.
:wave: I did not know this program ... even so, it works "half strange" on my Windows 10 x64... It does not seem to be from Microsoft, but of PrimaSoft ;)
Anyway ... it does exactly the same as my Script above.
Jovannb wrote:and then you have a Windows-Service with all its properties and possibilities (call services.msc in CMD-Window to see them)
There is a problem, if you look at the script that I put above, Microsoft says that you should call some WinApi functions when your application runs as a service, otherwise, you have no way of handling it. For example, your application has no way of knowing if any other application wants to suspend your service, or any other action. You have no control over your own service.
User avatar
Jovannb
Posts: 243
Joined: 17 Jun 2014, 02:44
Location: Austria

Re: Run compiled.exe as a service?

02 Mar 2018, 01:52

Hi,

for sure, your are right it is from Pirmasoft (german page: http://www.pirmasoft.de/cms/freeware/runassvc).

Running a compiled AHK-Exe is quite simple as

RunAsSvc.exe -i --displayname "Compressor-Mover AHK-Exe" --exe "c:\xxx\watcher_for_compressor.exe"

and then you have a Windows-Service with all its properties and possibilities (call services.msc in CMD-Window to see them).

J.B.
AHK-Release 1.1.30.03 Ansi 32-bit, Win10 (1903 18362.295) 64-bit, german

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, littlegandhi1199, RubbeH, sobuj53, TAC109, Xtra and 34 guests