Running a shell script as a Windows Service using cygwin
Sometimes one wants to run a shell script or any other program as a Windows Service. The Windows Service API is unfortunately quite complex. Fortunately, with CygWin there exists a simple solution with the cygrunsrv
command: So assuming your script is in the home directory of Administrator and called myservice.sh
the command would be:
The -I
parameter means install a service. The -t
is the start type, the -p
is the path to the program (cygwin absolute path) and the -a
are the arguments to the program (in our case instructing bash to run the myservice.sh
script).
Your script may be something like:
I use it during development to test the DRBD reactor port I am currently working on.
As with any shell script or executable program the UNIX eXecute permission has to be set. To do so, type
Note that now you can start and stop the service just like any other service with the sc
(or net
) start/stop
commands:
To watch the file you can use tail -f
as in:
Note that you can specify any program for the -p
parameter. You can also redirect stdout
and stderr
of the program to a file (with the -1
and -2
parameters).
cygrunsrv
has many options, there is a comprehensive help so I won’t repeat it here: just do
to get started.
Best wishes, Johannes