Running Cygwin application as a Windows Service - windows

I am working on WinDRBD (https://github.com/LINBIT/windrbd) a port of the Linux DRBD driver to Microsoft Windows.
We want to run the user mode helper as a Windows Service (DRBD sometimes calls user space applications with call_usermodehelper(), which we emulate by a daemon that retrieves those request from the kernel driver, runs them and returns the exit status to the kernel).
When we run the daemon in a cygwin shell, everything works fine. However when running the daemon as a Windows Service it seems that cygwin cannot find its installation directory (which is C:\cygwin64 on my machines).
The registry entry (HKLM/Software/CygWin/setup/rootdir) points to the correct location, but I am not sure if it can also be accessed by the Windows Service?
/bin/sh isn't found by the service, however /cygdrive/c/cygwin64/bin/sh
exists, so when I run the shell with that path it can start
(and also finds the DLLs it requires to run). However shell
complains with:
bash.exe: warning: could not find /tmp, please create!
which definitely exists when running cygwin the normal way.
Has anyone ever tried to run a CygWin compiled EXE as a Windows
Service? Here is the output of sc query windrbdum:
SERVICE_NAME: windrbdum
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
(um is for user mode).
Thanks for any insights,
Johannes

As matzeri pointed out, cygrunsrv is the cygwin tool when it comes to running cygwin binaries as a service under Windows. It serves both as a wrapper (that does the Windows specific service API and event handling) as well as a tool to install, remove, start and stop services (this can still be done with the sc utility like
sc start <servicename>
).
To install a service (I) do:
cygrunsrv.exe -I windrbdlog -p /cygdrive/c/windrbd/usr/sbin/windrbd.exe \
-a log-server \
-1 /cygdrive/c/windrbd/windrbd-kernel.log \
-2 /cygdrive/c/windrbd/windrbd-kernel.log
where windrbdlog is the Windows name of the service, /cygdrive ... is the
full path to the cygwin application (no need to code any Windows Service API
calls there, it's just a Cygwin/POSIX executable), log-server is the argument
to the binary (so what is being started is windrbd log-server) and -1 and -2
are rediects for stdout and stderr. Exactly what I need, thanks to matzeri
for pointing me to cygrunsrv.

Related

Windows Linux Subsystem: start Bash Application as a Service

As the title already summarizes:
How can I start a bash-script automatically, when the computer starts - ideally without the need to log in to windows - using the Microsoft Subsystem for Linux's Bash.
At the moment, this isn't supported, because the WSL session manager service will close after the last bash.exe wrapper instance closes. There are a few options, but the absolute simplest one at the moment is to use the run utility from the Xming developer and just add a shortcut to your startup folder (in the start menu) pointing to
run.exe bash.exe -c "/home/user/daemoninit.sh ; /bin/bash". Unfortunately, if your daemon initialization requires root access, for example, something like sshd, you will need to add an exception to sudoers that allows anybody to run the daemon with root privileges.
Also, there are problems getting it to run as a true Windows system service, since each lxss installation is user-specific. Some people have gotten it to run on system startup, but it launches in a separate Windows session for that user and makes it so you can't launch bash.exe in your current user session.

Unable to start cygwin sshd service

I entered ssh-host-config into the cygwin prompt (started with admin privileges), said yes to privilege separation, new local account sshd, install sshd as a service; I entered no value for CYGWIN for daemon; I entered no for using a different name; yes for creating new privilege user account.
In my services.msc I am unable to start the service:
The CYGWIN sshd service on Local Computer started and then stopped.
Some services stop automatically if they are not in use by other services
or programs.
In the cygwin prompt, net start sshd produces:
The CYGWIN sshd service could not be started.
The service did not report an error.
More help is available by typing NET HELPMSG 3534
In the cygwin prompt, cygrunsrv -S sshd produces:
cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:
The service has not been started.
My /var/log/sshd.log says the following:
Unable to initialize device PRN
I've searched the questions on SO related to this issue, as well as the general Internet, and I guess what makes my question unique has to do with the sshd.log. I can't find anyone else who has received this.
I'm doing this to install Hadoop on my Windows. I cannot run a virtual machine on this slow computer as everything just bogs down.
I followed the steps in this topic and the problem was still happening, then I checked the sshd log file and it was complaining that the privilegies of the ssh private key were to open.
I executed the follow command:
chmod 400 /etc/ssh_host_ecdsa_key
Then I run the service:
net start sshd
It finally worked (BTW: I am using Windows 8)
I had the same problem and here is the solution that worked for me.
Solution 1
Restart the machine after you set the path environment variables to point to Cygwin bin directories and then start the service.
Solution 2
First add the account that runs the Cygwin sshd demon service to administrators group (this is by default)
Go to your cygwin installation folder (mine is at c:\cygwin64 and yours may differ)
add administrators group as full control. remove the property
Remove Readonly for your cygwin installation folder so anyone can write into it
Run the cygwin terminal as administrator and remove the service by typing
cygrunsrv -R sshd
Reboot your system
Run the cygwin terminal as administrator and reinstall the service again by typing
ssh-host-config -y
Run the cygwin terminal as administrator and start the service by typing
net start sshd
Your service now be running!
I tried above solutions but nothing worked for me.
I am using Windows 8 and was able to solve it.
My sshd.log file says : "Privilege separation user sshd does not exist FAILED"
So to remove this error while starting sshd as service just following below steps:
Edit the file /etc/passwd --
add " sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin ".
Edit the file /etc/group --
add " sshd:x:74: ".
Now start service as
net start sshd
It worked for me!
Followed the steps provide by Andrea Solution 1 and solution 2 , it did not work. Event viewer did not provide any info.
So took a look at the log var/log/sshd.log first it complained that it was unable to load the dll:
/usr/sbin/sshd.exe: error while loading shared libraries: cygcrypto-1.0.0.dll
Solution:
Reinstalled the package and ensured that the dll were in lib.
Started the service again still it failed but this time it complained about missing host key.
Could not load host key: /etc/ssh_host_dsa_key
Could not load host key: /etc/ssh_host_ecdsa_key
Disabling protocol version 2. Could not load host key
sshd: no hostkeys available -- exiting.
Solution:
go to the Cygwin Command prompt (with run as admin ) and
enter ssh-keygen -A
the missing host key were generated .
Finally I was able to start it.
I would recommend looking at the log file further if the above solution does not work.
In my case (using a Polish version of Windows 8) I had to manually modify the /etc/passwd and /etc/group files.
Some lines for Administrator, Administrators, Guest, Guests, etc. had been missing, so I merely inserted them, and copied all other fields from their non-English versions. Eg.
nano /etc/passwd
SYSTEM:*:18:544:,S-1-5-18::
LocalService:*:19:544:U-NT AUTHORITY\LocalService,S-1-5-19::
NetworkService:*:20:544:U-NT AUTHORITY\NetworkService,S-1-5-20::
Administrators:*:544:544:,S-1-5-32-544::
Administratorzy:*:544:544:,S-1-5-32-544::
TrustedInstaller:*:4294967294:4294967294:U-NT SERVICE\TrustedInstaller,S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464::
Administrator:unused:500:513:U-gordito\Administrator,S-1-5-21-580982140-4090956935-1935414389-500:/home/Administrator:/bin/bash
Guest:unused:501:513:U-gordito\Gość,S-1-5-21-580982140-4090956935-1935414389-501:/home/Gość:/bin/bash
Gość:unused:501:513:U-gordito\Gość,S-1-5-21-580982140-4090956935-1935414389-501:/home/Gość:/bin/bash
HomeGroupUser$:unused:1004:513:HomeGroupUser$,U-gordito\HomeGroupUser$,S-1-5-21-580982140-4090956935-1935414389-1004:/home/HomeGroupUser$:/bin/bash
sshd:unused:1006:513:sshd privsep,U-gordito\sshd,S-1-5-21-580982140-4090956935-1935414389-1006:/var/empty:/bin/false
cyg_server:unused:1007:513:Privileged server,U-gordito\cyg_server,S-1-5-21-580982140-4090956935-1935414389-1007:/var/empty:/bin/false
nano /etc/group
root:S-1-5-32-544:0:
SYSTEM:S-1-5-18:18:
TrustedInstaller:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464:4294967294:
Administrators:S-1-5-32-544:544:
Administratorzy:S-1-5-32-544:544:
Czytelnicy dzienników zdarzeń:S-1-5-32-573:573:
Guests:S-1-5-32-546:546:
Goście:S-1-5-32-546:546:
IIS_IUSRS:S-1-5-32-568:568:
Users:S-1-5-32-545:545:
Users DCOM:S-1-5-32-562:562:
Użytkownicy:S-1-5-32-545:545:
Użytkownicy DCOM:S-1-5-32-562:562:
Użytkownicy dzienników wydajności:S-1-5-32-559:559:
Użytkownicy monitora wydajności:S-1-5-32-558:558:
Użytkownicy zarządzania zdalnego:S-1-5-32-580:580:
HomeUsers:S-1-5-21-580982140-4090956935-1935414389-1003:1003:
TelnetClients:S-1-5-21-580982140-4090956935-1935414389-1005:1005:
WinRMRemoteWMIUsers__:S-1-5-21-580982140-4090956935-1935414389-1000:1000:
None:S-1-5-21-580982140-4090956935-1935414389-513:513:
Brak:S-1-5-21-580982140-4090956935-1935414389-513:513:
Only after this, I was finally able to chmod g-rwx all the files that offended sshd, in a way that 'ls -la' indeed confirmed the dropped permissions for group and other.
Then finally, privileged CYGWIN services installed and started (sshd, cron)

Cygwin bash 'Cannot allocate memory' error

I have kind of a bizarre problem. I have a directory on a network share that contains the cygwin binaries. This cygwin dir used to be on a dell poweredge box and was mounted as a virtual directory in IIS8. A webservice would execute a call then to a bash script like so:
\\poweredgeshare\cygwin\bin\bash --login -c "//poweredgeshare/mytools/myshellscript.sh 2>&1 | tee -a //poweredgeshare/logs/logoutput.txt"
This worked great until we got a new dell Equalogic fileserver with an FS7600 front end and tried to execute that call from IIS8 on a windows server 2012 box. With that combination, I now get the following error:
/usr/bin/bash: //fs7600/mytools/myshellscript.sh: Cannot allocate memory
This WORKS if I mount that exact same fs7600 share on my windows 8 box's IIS8 as a virtual directory. All web calls function as expected. It just blows up after I deploy it to the production (server 2012) machine.
I have found that if I remove the 2>&1 | tee -a and just use > instead it works in all cases. So there is something about the pipe to tee that is causing an issue.
I am not a shell script expert- I didn't write this, I'm just trying to debug it and understand why the exact same cygwin binaries calling the exact same shell scripts but just on a new filesystem blows up.
So to sum up what worked and what doesn't work:
- old fileshare works on both dev (windows 8) and production (server 2012) environment
- new fileshare works on dev, but NOT server 2012
Does anyone have any thoughts as to why this might be happening?

Shutdown Windows machine from linux terminal [closed]

Closed. This question is not about programming or software development. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 months ago.
Improve this question
I am running an application on linux machine. By giving the ip address of a windows machine as input, the application must shutdown the windows machine. If the machines run the same OS it is very easy but I'm confused how to do it in cross OS.
There may be more setup to do, especially for Windows Vista, Windows 7 and further windows versions, to allow remote shutdown:
Part A) On the Windows machine:
1) Add a remote shutdown security policy:
run secpol.msc
in the program tree, open Security Settings > Local Policies > User rights Assignment
Find the entry Force shutdown from a remote system
Edit the entry, add the windows user account that will be used for shutdown (ex: nouknouk)
2) Add registry keys to disable UAC remote restrictions:
Run regedit.exe as Administrator
Find HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System
Create a new registry DWORD(32) value named LocalAccountTokenFilterPolicy and then assign it the value 1
3) Start remote registry service:
Open cmd.exeas Administrator
Execute the two following commands:
sc config RemoteRegistry start= auto
sc start RemoteRegistry
Part B) On the Linux machine:
1) install the package samba-common:
It depends on your Linux distribution, but for Debian and derivated (Ubuntu, Linux Mint, ...), the apt-get command can be executed like that:
apt-get install samba-common
2) To actually shutdown your Windows machine from the Linux one, run the following command:
net rpc shutdown -f -t 0 -C 'message' -U userName%password -I xxx.yyy.zzz.ttt
Where:
-f means force shutting down all applications (may be mandatory)
-t 0 is the delay before doing it (0 means 'right now').
-U user%password is the local user and his password on the windows machine (the one that has been allowed to do remote shutdown in part A).
-I is the IP address of the windows machine to shutdown.
Command to shutdown windows system from linux -:
$ net rpc -S <ip address> -U <username>%<password> shutdown -t 1 -f
This command can be issued from bash or even set in cron job to shutdown the computer at a specific time and this command is shipped with many distros by default.
It's important to note that the above solution will not work if the username in question does not have a password set (at least that's how it was in my case).
For windows 10 (and below maybe, did not check) users one must go to the firewall settings and enable "Remote Service Management" for the linux box to be able to connect via rpc.
It depends on your infrastructure -- how you authenticate to the Windows machines, whether you can configure them yourself, etc. If it were me, I'd put Cygwin on the Windows boxes, then ssh to them and run shutdown -h. There are surely other ways to do it, of course.
You need a way to launch a shell on the Windows box so you can run th shutdown command built in to Windows.
You can install Cygwin for this, then install an SSH daemon in Windows. Once that's running, your Linux box can run commands on the Windows box just as if it were another Linux machine.
Here are some instructions for setting up Cygwin's sshd in Windows.
Option 1: Install SSH server on windows. Login to this server from any box and call shutdown command. We use Interix(Microsoft's unix like environment). It comes with a telnet server- allows to invoke windows commands from other machines..
Another option:
If you samba installed on windows it can connect to windows and call windows commands
net rpc SHUTDOWN ...
(from http://www.linuxforums.org/forum/red-hat-fedora-linux/60324-remote-shutdown-windows-linux-box.html#post573872 )
Another option:
try rdesktop to the windows machine with shutdown command ( I use it for running windows batch script which has shutdown in it, not sure if you can directly call shutodwn)
For windows 10, install openssh-server following this.
With permission setting by nouknouk here
It should be work by
ssh username#host 'shutdown /p'
Also don't forget to add an inbound rule for RPC in Windows firewall allowing port 445.
Use telnet command in Linux, make sure telnet is enabled in Windows system

Jboss Service started and then stopped

I'm attempting to get my Jboss Server running as a windows service using the JbossService.exe, and I had it working until I uninstalled it (with ./JbossService -uninstall JbossService), and now, while it will install and the service will appear in the windows service list, attempting to start it will only yield the error message:
"The JBossService on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service."
If it helps, the command I'm using to install it is:
./JBossService.exe -install JBossService "$java_home\jre\bin\server\jvm.dll"
-Xmx128m -Xrs -Djava.class.path="$java_home\lib\tools.jar;$jboss_home\bin\run.jar"
-Duser.timezone="Australia/Sydney"
-start "org.jboss.Main" -stop "org.jboss.Main"
-method systemExit -out "$jboss_home\server\default\log\stdout.log"
-err "$jboss_home\server\default\log\stderr.log"
-current "$jboss_home\bin"
Any ideas would be appreciated. If more information is required just let me know.
Are you using Cygwin? If not, this could be due to the fact that environment variables are referenced using %%, not $, on Windows. You might try the following instead:
JBossService.exe -install JBossService "%java_home%\jre\bin\server\jvm.dll"
-Xmx128m -Xrs -Djava.class.path="%java_home%\lib\tools.jar;%jboss_home%\bin\run.jar"
-Duser.timezone="Australia/Sydney"
-start "org.jboss.Main" -stop "org.jboss.Main"
-method systemExit -out "%jboss_home%\server\default\log\stdout.log"
-err "%jboss_home%\server\default\log\stderr.log"
-current "%jboss_home%\bin"
Even if you are using Cygwin, the $ notation still might not work if, for example, these parameters are being stored in the Registry and then read later from the OS (which does not understand Cygwin notation).
While this does have a valid answer, one reason this happened to me recently is because the user the JBoss Windows Service was running as had certain active directory permissions revoked from the server (in this case, administrative-like permissions); it could no longer access certain directories it required in order to process (the Java bin folder, for example).
Once the user the service ran as was added back to the appropriate Windows User Groups, the JBoss Windows Service started with no issues.
In my case, JAVA_HOME was not set. Once I did that, Automatically windows Services started.
:)

Resources