Running batch file from scheduled task on windows server 2012 won't write to disc - batch-file

I wrote a short batch file for restarting two services every night and write some lines to a txt-file in the same folder. According to the history of the scheduled task the task ran succesfully, but the txt-file is not created.
At first I thought this was beacause of permissions, but when I escalated the permissions of the user running the task to max on both the batch file and the folder, the result stayed the same.
If I run the batch-file manually it does what it's supposed to do.
What am I missing here?
Source:
#echo off
set now=%date:~6,4%%date:~3,2%%date:~0,2%
#echo. >>servicerestartlog.txt
#echo.%now% >>servicerestartlog.txt
net stop "IntegratorService"
IF ERRORLEVEL 0 #echo STOP INTEGRATOR SUCCESS>>servicerestartlog.txt
IF NOT ERRORLEVEL 0 #echo STOP INTEGRATOR FAILED>>servicerestartlog.txt
net stop "SchedulerService"
IF ERRORLEVEL 0 #echo STOP SCHEDULER SUCCESS>>servicerestartlog.txt
IF NOT ERRORLEVEL 0 #echo STOP SCHEDULER FAILED>>servicerestartlog.txt
net start "IntegratorService"
IF ERRORLEVEL 0 #echo START INTEGRATOR SUCCESS>>servicerestartlog.txt
IF NOT ERRORLEVEL 0 #echo START INTEGRATOR FAILED>>servicerestartlog.txt
net start "SchedulerService"
IF ERRORLEVEL 0 #echo START SCHEDULER SUCCESS>>servicerestartlog.txt
IF NOT ERRORLEVEL 0 #echo START SCHEDULER FAILED>>servicerestartlog.txt
exit

Solution that worked for me:
In addition to the path to the script I wanted to run I had to add the folder the script was supposed to start in. This can be set in the Start in (optional) - textbox under edit action. Seems it's not so optional after all...

Related

Execution script bat using control-M

i have case regarding control-M.
I have simple script batch and plan execute using control-M job, below detail script :
1.MasterParam.bat
#echo
Set PathPackage=D:\test\
2.Main.bat
#echo off
Set PackageName=Package.dtsx
Call MasterParam.bat
Dtexec /f %PathPackage%%PackageName%
If %errorlevel% NEQ 0 ( exit /b %errorlevel%)
Question :
Can control-M execution script.bat with containing Call another scritp.bat?
wheter control-M stop the job if script.bat return errorlevel !=0?
In addition to the original answers, note that you can run Control-M jobs as direct commands (instead of executing .bat or .cmd files) which is often a better way to control the task (and works fine with dtexec /F ...).
If %errorlevel% NEQ 0 command means Not Equal to 0 and yes control-M can stop the job if script.bat return errorlevel !=0
By using Call MasterParam.bat or even by Call MasterParam
u can also pass parameters while calling the script -Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call.

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Batch Copy Issue

We have an integration engine, which creates txt files for the opposite host system. Our system writes files to local folder. I created a bat file like that and scheduled for every 1 minute:
xcopy /v /y E:\*.txt Z:\
move E:\*.txt E:\Processed (for backup purpose)
Z:\ is the mapping folder of the host system and that folder is being scanned frequently. If a file is processed it will be deleted immediately by the host system.
My problem is, sometimes files are written duplicated. I mean users see the activies as twice. I think that's because of this; consider a moment which the host system processes my file at the same time of xcopy executes and things get messed up. I know it is impossible to happen those at the same time but maybe network lags causing machines to behave like that?
Any ideas?
Thanks
What about using following batch file?
#echo off
set "SleepTimeInSeconds=60"
rem For sleep time using PING command 1 must be added
rem because the first trial is always successful.
set /A SleepTimeInSeconds+=1
:NextRun
echo %TIME% Searching for files to copy and move ...
for %%I in (E:\*.txt) do (
copy /B /V /Y /Z "%%I" Z:\
move /Y "%%I" E:\Processed\
)
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %SleepTimeInSeconds% >nul
goto NextRun
It copies each file separately on the server and then moves the file to the backup directory.
See How to sleep for 5 seconds in Windows's Command Prompt for the sleep time solution using command PING. You could perhaps also use command TIMEOUT depending on version of Windows which would be even better.
This batch file must be started only once on startup of machine and should run forever until shutdown of machine. Don't run this batch file as scheduled task every minute.
The scheduled task starting the batch file every minute caused the problem with processing files twice because if copying and then moving all *.txt files does not finish within 1 minute caused for example by a temporary network problem, another batch process was started doing the same as the still running batch process.

Need assistence merging two batch files (.bat)

So now i have 2 .bat files. one copies some file if it was updated ( robocopy C:\location C:\destination) and another one that executes a some kind of .exe file (start c:\BAT\fraps.exe) , now what i need is maybe a one file, so that WHEN a file was copied using "robocopy" the executive file would run automaticaly. So maybe there is a way to merge them into one or smth.
Errorlevels are set by robocopy: errorlevel 1 means that a file was successfully copied.
robocopy C:\location C:\destination
if errorlevel 1 if not errorlevel 2 start c:\BAT\fraps.exe
Here is proof of concept code - following extended comments:
#echo off
md test1
:loop
>test1\testfile.txt echo aaa
robocopy test1 test2
if errorlevel 1 if not errorlevel 2 pause
del test1\testfile.txt
goto :loop
Use /WAIT option, when the application is stared then it will wait until it terminates.
Use /B option, when application is started then it will not create a new window.
Example:
start /wait Command CALL D:\YourFirstScript.bat
start /wait program.exe
start /wait Command CALL X:\YourSecondScript.bat
It's a good idea to print a message before and after.
Example:
ECHO Starting program.
start /wait program.exe
ECHO Finished.
See below link for more details.
How do I launch multiple batch files from one batch file with dependency?
Note: When you run script as administrator then you need to set full path as the default is set to "C:\Windows\System32".
The easiest way to set is
start %~dp0Directory\program.exe
See for details about "%~dp0" here
What does %~dp0 mean, and how does it work?
This is my first post and I hope that this will help you.

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

Resources