Batch file start command launching process but not launching GUI window - batch-file

So i'm trying to do a simple task restart on a box that needs a GUI app running on the desktop. I'm using the start command to call the application. For some reason when i run it from the task scheduler it does everything it's supposed to except launch the application's GUI window. I can see the process running in Task Manager, but no GUI launches. This is Windows 2008 R2.
Here's the script replacing the app with notepad (which has the same problem).
#echo off
:: Kill notepad.exe if running.
TaskKill /IM notepad.exe /F
:: Wait for app to close.
PING 1.1.1.1 -n 1 -w 8000 >NUL
:: Check to make sure app isn't running, then start it.
tasklist /FI "IMAGENAME eq notepad.exe" 2>NUL | find /I "notepad.exe">NUL
IF NOT "%ERRORLEVEL%"=="0" START "" "notepad.exe"
EXIT %ERRORLEVEL%
Is this a Windows 2008R2 Task Scheduler gotcha, or a batch file gotcha?

You must have the option to "Run only when user is logged in" selected. Otherwise the task will be launched as an invisible background process.

Related

Task Scheduler, run .bat NOT starting java app

This seems like such a basic thing..kill app & restart it..
I have this batch file configure to run daily through windows task scheduler:
#ECHO OFF
::Daily reboot to limit ram usage
taskkill /F /IM javaw.exe
::
:: start app again
::
start app.jar
The script is run each day successfully according to windows but it is only closing the program not starting it again. When I double click my .bat file it works just fine..
What am I missing?
Well I'm in my first year of bachelor only using BlueJ so far to learn java so I apologize.
Happy that I now know the task run from c:\win\sys32. I could see the logs from the java app there not finding the libraries. so the #CD /D "%~dp0" I'll try to remember for sure. Thx
final code:
#CD /D "%~dp0"
::Daily reboot to limit ram usage
taskkill /F /IM javaw.exe
::
:: start app again
::
start java -jar d:\path\to\app.jar
full path to javaw.exe was not needed, only diff when running the app with task scheduler is that the last line in log should be "added to SystemTray" making the app visible. Now it is running hidden, which is ok for it's use but I'll try to figure out on my own why.
(just "start app.jar" is also working just fine btw with the #CD /D "%~dp0" on top.)
thx,
First make sure you have configured environment variable for running java.exe . If not, refer this .
Secondly, always use complete path to start/kill your jar execution.
The command to run your application is:
java -jar app.jar

Call chrome from bat and close bat when chrome browser closes

I want to open chrome using bat file, but when chrome specific process closes the cmd should close as well. I've tried it but doesn't seem to work.
"C:\Users\andreas\AppData\Local\Chrome\Application\chrome.exe"
Shouldn't the bat file be attached to the app and when the app closes the cmd window should close as well?
No it won't. Starting programs using or not using start is documented in start /? help.
When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt. This new behavior does NOT occur if executing
within a command script.
Also see http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135
New behaviour refers to the Win NT4 vs Win 2000 versions of CMD.exe.
You could use start /WAIT, designed to wait for GUI application to close, but it won't solve your problem. (It would for Firefox, for example).
The thing is, when you start Chrome, it actually acts as a launcher and spawns a bunch of children processes, and then exits immediately, leaving them to show you a page.
So, if you ran start /WAIT ...\chrome.exe, it would exit at that point.
What can be done about that?
You can monitor chrome instances checking the process list like that:
launch_chrome.bat
#echo off
start /wait "chrome" "C:\Users\andreas\AppData\Local\Chrome\Application\chrome.exe"
:loop
tasklist /FI "ImageName eq chrome.exe" | find /i "chrome" > nul || goto :chrome_exited
:: wait 2 seconds before checking again
timeout /t 2 >nul
goto :loop
:chrome_exited
echo Chrome exited
Still, it won't solve your problem if you want it to run along with your already running usual chrome processes. The script will wait for you to close all of them.
In this case the options left for you are to use tools like AutoIt, AutoHotkey.
They can check the titles on all the chrome windows and if they don't see some specific string they can close the batch.

How do you make a simple batch file that will open multiple programs and uwp apps

First of all, I am not a coder or developer and I apologize for any ignorance from this moment forward.
I am trying to make a simple batch file that will open multiple programs and uwp apps. For example, at the start of my work day I need several programs and apps to get up and running such as edge, skype, outlook16, onenote (uwp app not program), etc.
I can get normal windows programs that use .exe to do this but I am having difficulty finding out how to do the same with windows UWP.
Any help would be appreciated.
It is a bit tricky with UWP apps, but you can definitely make it.
Put the live tiles of the UWP apps you want a shortcut to in the start menu. And then drag and drop the tile itself on the Desktop. This will create a shortcut to that app. Now you can manually launch the shortcut via the batch file, or even more easily - copy the created shortcuts into the Startup folder:
C:\Users\{username}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
You can open this folder even faster by hitting Windows + R and typing shell:startup.
Other option for launching the apps would be to use their associated URI protocol. OneNote has onenote-cmd: or onenote:, Edge has microsoft-edge: (or you can use any website URL if it is your default browser). There are more URI schemes, a short list is available here for example.
I do not know about UWP, but these may be.
I wrote this answer because it was high on my search and most answer I found had little examples.
And this provides a few more ideas for programs that run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start outlook
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM outlook.exe
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc for gaming.

Killing service through batch or something similar

The WIA service(stisvc) on my friend's win8 computer quite often stops working properly. When stopping/restarting from MMC it returns error "Service didn't respond in time", so I kill it from the Task Manager and then start it from within MMC.
Is there a way to make a script to kill the stisvc and then start it again?
This works for you (You should have Admin rights on your system):
#echo off
taskkill /F /IM svchost.exe /FI "SERVICES eq stisvc"
sc query stisvc | find /i "running"
if errorlevel 1 (
echo Not running...
echo Starting Windows Image Acquisition (WIA) service....
net start stisvc
)
Dependencies:
Windows Image Acquisition (WIA) will not start, if the following services are stopped or disabled:
Remote Procedure Call (RPC)
Shell Hardware Detection

slow xcopy startup script

I have a .bat file using Xcopy to copy a folder.
I'm using this bat to run as startup script via Group Policy Editor
The .bat file contains:
xcopy "D:\temp\Temporary Firefox Files\*.*" "r:\TEMP\Temporary Firefox Files\" /E /I /H /Y /C /Q
If I run it after logon it takes about 5 seconds to complete.
But when run at startup (before logon) it runs for about 2 minutes. (after logon I start task manager and see Xcopy process still running for 2 minutes).
As a hint, if I run the same .bat as shutdown script (also via GPO) it takes only 5 seconds to complete.
I'm testing this on Windows 7 Ultimate X64 and on Windows XP Professional X86. The result is the same.
Any ideas?

Resources