what is wrong with this conditional (if-else) execution in batch script? - if-statement

I am testing some small scripts that I have. This particular one is driving me crazy. In my mind it should work, but it is not doing what I thought it would/should.
#echo off
echo testing the issue
echo.
ECHO Checking the log file for errors...
echo %errorlevel%
FINDSTR /C:"open failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking1)
:TSTErrChecking1
FINDSTR /C:"does not exist" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking2)
:TSTErrChecking2
FINDSTR /C:"Logon failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking3)
:TSTErrChecking3
FINDSTR /C:"Failure" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking4)
:TSTErrChecking4
FINDSTR /C:"RC (return code) = 0" C:\automatic_deployment\test1.txt
echo %errorlevel%
IF %ERRORLEVEL% == 0 (
ECHO Deployment was successful.
pause
GOTO quit) ELSE (
ECHO Deployment failed.
pause
GOTO quit)
:quit
exit
As you can see I am using "FINDSTR" to find certain words/strings from a small test1.txt file and act accordingly. I want it to say "deployment failed" and quit asap if it sees any "fail"/ "does not exist"/"open failed". If it cannot find anyone of those above and finds the "RC (return code) = 0" I want it to say it was "successful".
So, i put all those test words and strings in my test1.txt file and tested the batch script, but it keeps skipping (i think) all those fail hints/words and keeps saying it was successful. Plz, help. Thanks in advance.

it could be that you already have an %ERRORLEVEL% variable defined in your environment
Read
Raymond Chen's ERRORLEVEL is not %ERRORLEVEL% http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx
and then try
FINDSTR /C:"RC (return code) = 0" test.txt
IF ERRORLEVEL 1 ECHO Deployment failed.

I just didn't have the matching strings to search for. I didn't know that FINDSTR looked for EXACTLY the same case letters when searching and comparing. That was the problem.

Related

Find and remove remote hidden file

I'm trying to create a small script that looks for a specific file in an hidden folder and remove it using IF ELSE, but it doesn't seem to work. It works when I try to find and delete a local file, but not when I try a remote hidden file.
This is my code:
#echo off
set /p destination="Enter destination PC name: "
#echo off
set /p username="username: "
echo.
echo [33m'Removing 'notes.ini' file[0m
timeout /T 2 /NOBREAK > nul
echo.
if exist "\\%destination%\C$\Users\%username%\AppData\Local\Lotus\Data\notes.ini" (
del /A /Q /F "\\%destination%\C$\Users\%username%\AppData\Local\Lotus\Data\notes.ini"
echo [92m'Notes.ini' cleared.[0m
) else (
echo [33m'Notes.ini' doesn't exist.[0m
)
echo.
timeout /T 2 /NOBREAK > nul
echo [92mScript clompleted[0m
I tried with a local file, and then the scripts works, but not with a remote hidden file. I always get the "else" statement as return with hidden files

Unicorn doesn't pick up changes with new deploy of Rails app (Capistrano, Nginx)

I'm new to deploying, so this is probably a rookie mistake, but here it goes.
I have a Rails 4 app that I'm deploying to a Linux server using a combination of Capistrano, Unicorn, and Nginx. The deploy script runs fine and the app is now reachable at the desired IP, so that's great. The thing is, a) Unicorn doesn't restart upon deployment (at least, the PIDs don't change) and b) not surprisingly, the new changes aren't reflected in the available app. I don't seem to be able to do anything other than completely stopping and restarting unicorn in order to refresh it. If I do this, then the changes are picked up, but this process is obviously not ideal.
Manually, if I run kill -s HUP $UNICORN_PID then the pids of the workers change but not the master, and changes aren't picked up (which, apparently they are supposed to be); using USR2 appears to have no effect on the current processes.
Here's the unicorn init script I'm using, based on suggestions from other stack overflow questions with similar problems:
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="deploy"
APP_NAME="app_name"
APP_ROOT="/path/to/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT/current && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
TIMEOUT=${TIMEOUT-60}
# make sure the app exists
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}
case $1 in
start)
sig 0 && echo >&2 "Already running" && exit 0
echo "Starting $APP_NAME"
su - $USER -c "$CMD"
;;
stop)
echo "Stopping $APP_NAME"
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
echo "Force stopping $APP_NAME"
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo "reloaded $APP_NAME" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $OLD_PID && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $OLD_PID
then
echo >&2 "$OLD_PID still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su - $USER -c "$CMD"
;;
rotate)
sig USR1 && echo rotated logs OK && exit 0
echo >&2 "Couldn't rotate logs" && exit 1
;;
*)
echo >&2 $USAGE
exit 1
;;
esac
Using this script, start and stop work as expected, but reload/restart do nothing (they print the expected output but don't change the running pids) and upgrade fails. According to the error log, it's because the first master is still running (ArgumentError: Already running on PID: $PID).
And here's my unicorn.rb:
app_path = File.expand_path("../..", __FILE__)
working_directory "#{app_path}"
pid "#{app_path}/../../shared/pids/unicorn.pid"
# listen
listen "#{app_path}/../../shared/sockets/unicorn.sock", :backlog => 64
# logging
stderr_path "#{app_path}/../../shared/log/unicorn.stderr.log"
stdout_path "#{app_path}/../../shared/log/unicorn.stdout.log"
# workers
worker_processes 3
# use correct Gemfile on restarts
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{working_directory}/Gemfile"
end
# preload
preload_app false
before_fork do |server, worker|
old_pid = "#{app_path}/shared/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
end
Any help is very much appreciated, thanks!
It is hard to say for certain, since I haven't encountered this particular issue before, but my hunch is that this is your problem:
app_path = File.expand_path("../..", __FILE__)
working_directory "#{app_path}"
Every time you deploy, Capistrano creates a new directory for your app at the location releases/<timestamp>. It then updates a current symlink to point at this latest release directory.
In your case, you may mistakenly be telling Unicorn to use releases/<timestamp> as its working_directory. (SSH to the server and check the contents of unicorn.rb to be certain.) Instead, what you should do is point to current. That way you don't have to stop and cold start unicorn to get it to see the new working directory.
# Since "current" is a symlink to the current release,
# Unicorn will always see the latest code.
working_directory "/var/www/my-app/current"
I suggest rewriting your unicorn.rb so that you aren't using relative paths. Instead hard-code the absolute paths to current and shared. It is OK to do this because those paths will remain the same for every release.
The line
ENV="production"
looks extremely suspicious to me. I suspect that it wants to be
RAILS_ENV="production".
without this won't rails wake up not knowing which environment it is?

Batch - User Input to Command Prompt (IF statement issue)

I am currently creating a segment of a larger batch file that needs to let the user interact with the command prompt, but I seem to be having issues with the IF Statements.
The following code is what I've been trying:
:ONE2
cls
echo Free Roam is used like command prompt, but with additional commands.
echo Type FRHELP for a list of additional commands.
:COMMANDLOOP
echo.
set /P TEMPCMD=%CD% :
IF %TEMPCMD% == QUIT (
GOTO END2
) else if %TEMPCMD% == FRHELP (
GOTO COMMANDSLIST
) else (
%TEMPCMD%
GOTO COMMANDLOOP
)
:COMMANDSLIST
echo.
echo FRHELP = Display Free Roam Commands
echo QUIT = Leave your current Free Roam Session
GOTO COMMANDLOOP
What happens is, I can make multi-part commands (EG: cd ..) without the IF statements. But with the IF statements, it will only allow me to make single part commands (EG: dir).
If I have the IF statements as listed above, it will give me the "'..' was unexpected" error and exit.
Is there any way to pass my TEMPCMD variable to the command prompt with these IF statements and not get this error?
try this, it works for me without any error:
#echo off &setlocal
:COMMANDLOOP
echo.
set "TEMPCMD=%CD%"
set /P "TEMPCMD=%CD% :"
IF "%TEMPCMD%"=="QUIT" (GOTO END2
) else (
if "%TEMPCMD%"=="FRHELP" (
GOTO COMMANDSLIST
) else (
GOTO COMMANDLOOP
)
)
pause
:COMMANDSLIST
echo.
echo FRHELP = Display Free Roam Commands
echo QUIT = Leave your current Free Roam Session
GOTO COMMANDLOOP

I dont know why my batch if and goto isnt working

I dont know why my batch if and goto isnt working
it works until the user has to pick a choice to run. The whole thing just shutsdown
Here is my script:
#Echo off
:Password
set input=
set b=
set c=
set /p input=Password:
if %input%==******** goto yes
if not %input%==******** goto no
cls
:yes
cls
echo Access Accepted! Welcome Tucker!
echo.
set /p op=Enter Your Full-Name For Access:
if %op%== TuckerGarySiegel goto home
cls
:no
echo Wrong Password. Access Denied. No Entry.
goto password
cls
:home
color 1f
This is what I think is the problem area
Echo Welcome Tucker!
echo 1) My Batch Files
echo 2) Google Chrome
set /p input=Type Your Selection:
if %c%== 1 goto batch
if not %c%== 1 goto home
pause
cls
:batch
echo Choose File:
echo 1) Password Script
set /p b=Make Selection:
if %b%== 1 goto passscript
pause
cls
:passscript
i still need to make the rest
please help
set /p input=Type Your Selection:
if %c%== 1 goto batch
if not %c%== 1 goto home
You're setting the variable input but then checking the variable c. Try this instead, which will allow it to work:
set /p input=Type Your Selection:
if %input%== 1 goto batch
if not %input%== 1 goto home
P.S. you don't need to check an if and an if not. Doing this would be just fine:
set /p input=Type Your Selection:
if %input%== 1 goto batch
goto home

I need help writing this batch script

I want to have this script execute another script if a file name starts with one thing, another if it starts with another thing, and to continue on through the rest of the code if it starts with anything else. I want it to iterate through all the files in a folder and check against this. I have been working on it for a while and am looking for a fresh set of eyes since apparently I can't seem to get it working properly. Any help would be appreciated!
:========================================================================================
:: purpose: 1. copy file(s) from the export dir (SOURCE_DIR) (ARCHIVE_DIR)
:: 2. copy file(s) from the export dir (SOURCE_DIR) (TARGET_DIR)
:: 3. once file(s) are processed delete them from the export dir (SOURCE_DIR)
::=======================================================================================
::
#echo off
setlocal
:: Display the command line on the window's Title Bar
title %0 %1
set LOG_FILE="here.txt"
set SOURCE_DIR=here\there
set TARGET_DIR=here\nowHere
set ARCHIVE_DIR=here\oldStuffnThings
set FILE=FileStartingWithBlahBlergOrOther.docx
echo --------------------------------------------------------------------------------------------------------------------------------------- >> %LOG_FILE%
echo %DATE% %TIME% Starting file move >> %LOG_FILE%
if not exist "%SOURCE_DIR%%FILE%" (
echo %DATE% %TIME% "%SOURCE_DIR%%FILE%" not found >> %LOG_FILE%
echo %DATE% %TIME% no files to move so stopping >> %LOG_FILE%
exit
)
FOR /R %completepath% %%G IN (FileStartingWithBLAH*) DO (
call C:\otherScript1
)
FOR /R %completepath% %%G IN (FileStartingWithBLERG*) DO (
call C:\otherScript2
)
:: one at a time copy files to archive, copy to dir, then delete from the export dir
FOR /R %SOURCE_DIR% %%G IN (%FILE%) DO (
echo %DATE% %TIME% %%G was found for processing >> %LOG_FILE%
XCOPY %%G "%ARCHIVE_DIR%"/Y
echo %DATE% %TIME% %%G was archived status %ERRORLEVEL% >> %LOG_FILE%
XCOPY %%G "%TARGET_DIR%"/Y
echo %DATE% %TIME% %%G was copied to dir status %ERRORLEVEL% >> %LOG_FILE%
del %%G
echo %DATE% %TIME% %%G was deleted from the export dir status %ERRORLEVEL% >> %LOG_FILE%
)
rem set err_code=%errorlevel%
rem if %err_code% NEQ 0 (
rem echo %DATE% %TIME% **ERROR moving files %err_code% >> %LOG_FILE%
rem exit
rem )
rem echo %DATE% %TIME% File(s) moved successfully >> %LOG_FILE%
rem echo %DATE% %TIME% Process Complete >> %LOG_FILE%
A problem is that you have to put a backslash after SOURCE_DIR=here\there or it will look for here\thereFileStartingWithBlahBlergOrOther.docx