run batch script after windows server restart - windows

I am trying to run a script to startup some services after windows reboot.
I have tried one method: copying shortcuts of batch script in shell:startup folder. which eventually will start the scripts automatically but someone has to login to the system.
I am thinking about the possible ways to run the script after server restart without logon to the server.
Any help will be highly appreciated.

Create a Windows scheduled task and set it to run on system startup:

Related

Microsoft Azure Backup (MARS) not running from scheduled tasks

We've got several servers running MAR's the lastest version 2.0.9145.0
I can run all backup's manually and its runs without problems. But when i try to run it from scehduled backups it shows in task scheduler that its run but gives this error code:2147942401
If i manually input the arguements into powershell it runs fine. However the task scheduler is not kicking in the backups.
I've altered the permissions to a domain admin, changed PS path to syswow64. So now i'm lost.
Has anyone had similar and any ideas on how to fix it or at least run the commands from a bat file instead?

Starting jenkins Slave agent using Jnlp using Task Scheduler always starts as headless

I am trying to find solution for the jenkins nodes to automatically connect to master in non headleass mode.
Here is what I have done so far
I have written batch script which calls the powershell script. Powershell script has series of commands to start the jenkins slave agent using jnlp.
This batch script is configured in Task Scheduler to run at the startup. The jenkins slave node is connected to master as soon as node starts up.
But execution of GUI test is not visible in the node when the test starts.
When I manually run this batch script from command line, it works fine i.e non headless mode and I can see the execution of GUI test.
I've read few articles about this kind of setup and none worked.
I also wrote a small piece of powershell script to allow a Windows Service to Interact with Desktop. This script runs after the batch script to start jenkins slave agent.
Task scheduler job is configured to Run as Administrator and I login as administrator to see the execution.
Since this batch script is running via Task scheduler I am guessing its running in a different session.
How do I solve it.
Try to put your script into shell:startup.
On a Windows 10 machine with the Jenkins slave under the user you need, press Windows+R (Run...) and type shell:startup and put your script into opened folder. It will run once the user logged in.
This is how you'll see console and any windows it spawns.

Windows Server 2008 R2 backup does not start as scheduled with no error messages

I have been trying to configure regular automated backups to a shared network drive using the Windows Server Backup console. When I backup manually, it works, however it does not run on its own as scheduled. I have made sure to enable run backup while not logged in. There are no error messages when it does not run according to schedule, it just skips to logging the next scheduled backup as the next day.
I have also tried using the wbadmin command line. My script is similar to the following:
wbadmin enable backup –addtarget:\backupshare\myshare –include: c:\ –user:DOMAIN\mylogin –password:mypassword –schedule:19:00 -systemState -quiet -allowDeleteOldBackups -
I have not received any errors with my script and the windows command line acknowledges that there is a scheduled backup to run. However, the backup does not run and when I check wbadmin get status at the time it is scheduled, it will tell me there is no back up running at the moment with no error codes.
I am not sure why my back ups will not run as scheduled as they will run manually.
Any help would be greatly appreciated,
Thanks!
I’m going to assume that you’re running it with heightened privileges and for running the task you’re using a Domain Admin account, I would suggest running the command manually from command line and add in this argument “get status > \task.log”

Is there any way to open the CMD through teamcity so it will open and displayed on the desktop?

I have a batch job that i want to deploy from Teamcity to several servers,
to access several servers i use Winexe tool.
the batch is running but i can't see the session because it's started from teamcity,
but i can see that its running when looking at the process list.
My issue is that sometimes this job is having some errors,
which are being displayed on the cmd window when i run it manualy
but since i'm running it through TeamCity i can't see the CMD window so i can't see the error.
My question is:
Is there any way to open the CMD through teamcity so it will open and displayed on the desktop when i access the server as the same user?
note: bare in mind that i need to deploy it to several servers so i can't install several
agents via ZIP File.
So I found kind of a work-around to solve this problem,
I created a schedule task in windows that will run my batch.
when creating this task you need to set those settings:
1.) Run as: the user name that TeamCity is logging in.
2.) check the Run only if logged on check box.
3.) in the security tab give the user you use full permitions.
In order to run the schedule task you need to run this batch script:
Schtasks.exe /Run /TN name_of_schedule_task

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