Windows Linux Subsystem: start Bash Application as a Service - windows

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.

Related

Start local meteor app after windows boot

A local meteor (1.2) application should get started in the background right after windows has been booted. What are my options to get the job done?
You can use the following steps, if you want to start Meteor automatically when a user logs in:
Create a batch file.
Open the newly created batch file and place the following lines into it:
#echo off
cd "<the path to your Meteor project>"
meteor
timeout /t 10
start "C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe" http://localhost:3000
Create a shortcut:
Open the Run window (Win + R).
Open shell:startup (or go to: C:\Users\John Doe\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup).
Copy the shortcut into the folder.
Now, when a user logs in, Meteor should start your project and Microsoft Edge should open http://localhost:3000 after 10 seconds.
Applications cannot run without a user being logged in. So "started in the background right after windows has been booted" is not something that is going to happen. You would need a service for that, and services are not interactive. Assuming that you're talking about the Meteor JS framework, you can't build a service out of that.
If you're talking about starting an application when a user logs in, then you can do that easily by adding a shortcut to the executable to the user's "Startup" folder. On modern versions of Windows, the path would be something like:
C:\Users\UserName\AppData\Local\Microsoft\Windows\Start Menu\Programs\Startup

Start Windows GUI program in the PuTTY shell

I created an SSH session into my localhost through PuTTY. I am running OpenBSD server on the localhost which is a Windows 7 machine. The login was successful. Now I want to start programs in the PuTTY shell for example notepad.exe. I changed the directory to c:\windows\system32. The command start notepad runs without error but I don't see any window.
Is the notepad running in the background without a windows. If yes, how to make the window visible??
Yes, it's quite likely the Notepad is run in an invisible Windows session on the server. Note that it does not make a difference in this case that the server is the local machine.
If you hoped for the window to appear on the "local" machine magically, you are out of luck.
Generally, it's possible to run a GUI application on remote machine and see the GUI locally. On Unix it's quite common. Typically X Window is used for that.
But Windows GUI applications cannot be "tunneled" via SSH session to a local machine.

How I can start a service on my Unix server from windows

I need to stop/start an application service on Unix, while I being on windows. I don't want to do putty and login to unix server and stop service.
What batch script I can write to do this from windows?
If you don't want to do this manually, make sure you can run the ssh command from your batch file (by instaling it and extending your PATH if necessary to where it is installed) and do:
ssh root#your-system-name stop service-name
ssh root#your-system-name start service-name
This assumes that your linux machine has start and stop commands, otherwise you might need to invoke /etc/init.d/service-name stop or other Linux distro specific command.

How to run Hadoop on Cygwin with proper credentials to enable setting file permissions, etc.?

I cannot change the permissions on files when I run Hadoop in Cygwin:
java.io.IOException: Failed to set permissions of path: \tmp\hadoop-James\mapred\staging\James-1143336710\.staging to 0700
From what I've gathered you can't really run Cygwin as root since Windows doesn't really have a notion of root (reference), and I've tried to run Cygwin as the Administrator user but this option isn't available to me when I right click on the Cygwin shortcut in Windows XP (I've also tried changing the Cygwin shortcut's properties to allow me to run as another user but that option is disabled).
Can anyone advise me as to how I can get past this issue? Thanks in advance for your help.
Here is a simple-to-use workaround for this particular problem:
https://github.com/congainc/patch-hadoop_7682-1.0.x-win
This issue is not about file permissions per se. Rather, it is an issue with the Java VM's support for setting file permissions on Windows, and an intransigent attitude among the Hadoop committers not to work around the problem. See HADOOP-7682 for the gory details:
https://issues.apache.org/jira/browse/HADOOP-7682
run ssh-host-config. it will set up the prvileged user "cyg_server" and set up sshd
as a windows service.
in "/etc/passwd" give the user a home "/home/cyg_server" and shell "/bin/bash".
create a password for the user. then create the ssh keys and add them to
~/.ssh/authorized_keys.
start the windows service. in a cygwin shell, "ssh cyg_server#localhost".
--- edit ---
forgot to mention: when you create the password for the cyg_server user, you need a root cygwin shell (run cygwin bash as Administrator). also give the user a valid shell (/bin/bash).

Run batch file as a Windows service

In order to run one application, a batch file has to be kicked off (which does things like start Jetty, display live logs, etc). The application will work only if this batch file is running. I am hence forced to have this batch file running and not logout from the Windows server.
Can this batch file be run as a service? I am experimenting with one of the suggestions from a similar question.
NSSM is totally free and hyper-easy, running command prompt / terminal as administrator:
nssm install "YourCoolServiceNameLabel"
then a dialog will appear so you can choose where is the file you want to run.
to uninstall
nssm remove "YourCoolServiceNameLabel"
There's a built in windows cmd to do this: sc create. Not as fancy as nssm, but you don't have to download an additional piece of software.
sc create "ServiceName" start= demand displayname= "DisplayName" binpath= [path to .bat file]
Note
start=demand means you must start the service yourself. Options include: boot, system, auto, demand, disabled, delayed-auto
whitespace is required after =
I did encounter an error on service start that the service did not respond in a timely manner, but it was clear the service had run the .bat successfully. Haven't dug into this yet but this thread experienced the same thing and solved it using nssm to install the service.
No need for extra software. Use the task scheduler -> create task -> hidden. The checkbox for hidden is in the bottom left corner. Set the task to trigger on login (or whatever condition you like) and choose the task in the actions tab. Running it hidden ensures that the task runs silently in the background like a service.
Note that you must also set the program to run "whether the user is logged in or not" or the program will still run in the foreground.
On Windows 2019 Server, you can run a Minecraft java server with these commands:
sc create minecraft-server DisplayName= "minecraft-server" binpath= "cmd.exe /C C:\Users\Administrator\Desktop\rungui1151.lnk" type= own start= auto
The .lnk file is a standard windows shortcut to a batch file.
--- .bat file begins ---
java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar
--- .bat file ends ---
All this because:
service does not know how to start in a folder,
cmd.exe does not know how to start in a folder
Starting the service will produce "timely manner" error, but the log file reveals the server is running.
If you need to shut down the server, just go into task manager and find the server java in background processes and end it, or terminate the server from in the game using the /stop command, or for other programs/servers, use the methods relevant to the server.
As Doug Currie says use RunAsService.
From my past experience you must remember that the Service you generate will
have a completely different set of environment variables
have to be carefully inspected for rights/permissions issues
might cause havoc if it opens dialogs asking for any kind of input
not sure if the last one still applies ... it was one big night mare in a project I worked on some time ago.
While it is not free (but $39), FireDaemon has worked so well for me I have to recommend it. It will run your batch file but has loads of additional and very useful functionality such as scheduling, service up monitoring, GUI or XML based install of services, dependencies, environmental variables and log management.
I started out using FireDaemon to launch JBoss application servers (run.bat) but shortly after realized that the richness of the FireDaemon configuration abilities allowed me to ditch the batch file and recreate the intent of its commands in the FireDaemon service definition.
There's also a SUPER FireDaemon called Trinity which you might want to look at if you have a large number of Windows servers on which to manage this service (or technically, any service).
Since NSSM is no longer maintained, you can consider using WinSW. It has binaries that would work with or without .Net.
Basically you create an XML file and then install it. Here is a sample of a minimal XML:
<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>myapp</id>
<!-- Path to the executable, which should be started -->
<!-- CAUTION: Don't put arguments here. Use <arguments> instead. -->
<executable>%BASE%\myExecutable.exe</executable>
</service>
And then you can install and start it:
winsw install myapp.xml
winsw start myapp.xml
Install NSSM and run the .bat file as a windows service.
Works as expected
My easest way is using opensource svcbatch (https://github.com/mturk/svcbatch/) as wrapper of CMD(BAT) in sc :
sc create myservice binPath= ""%cd%\svcbatch.exe" myservice.bat"

Resources