Automated server reboot script, without affecting users in citrix.

Sekar Chinnakannu
Sekar Chinnakannu used Ask the Experts™
on
I am looking for script to wait all users to log off from citrix server and once there are no users logged on, then the server will have to reboot itself. Please help me to get the script.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
Check the last post with script. It waits until there are no active users:

http://www.dabcc.com/posts.aspx?thread=330&forum=67
This will disable the logins on the server and will give three messages to users before 7, 5 and 1 minute and then it will enable login and will initiate server reboot.  You can save below script with extension "xxxx.cmd" and put in server's schedule task to automate reboot.

Please find below batch script to reboot server and cleaning system.

ECHO OFF
CLS

:: === Variable Section =======================================================
SET mDate=%DATE%
SET mTime=%TIME%
SET mVersion=1.0

:: --- Set LOG FILE location and name -----------------------------------------
::     The LOG FILE is the same name as the CMD file with TXT extension
::     Initialize the LOG
SET mLogFileFolder=%SystemRoot%\Debug
FOR /F %%I IN ("%0") DO SET mLog=%mLogFileFolder%\%%~nI.txt
ECHO -------------------------------------------------------------------- >%mLog%

:: --- Profile and Temp Folders ---------------------------------
SET mProfDir="%SystemDrive%\Documents and Settings"
SET mTempDir=%SystemRoot%\Temp
SET mCTempDir=%SystemDrive%\Temp


:: --- Start of Script Section ------------------------------------------------
ECHO Automatic Server Reboot : Version: %mVersion%           >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
ECHO Server %Computername% has initiated a automatic restart sequence...  >>%mLog%
ECHO Date and Time : %mDate% @ %mTime%                                    >>%mLog%
ECHO.                                                                     >>%mLog%

ECHO ==================================================================== >>%mLog%
ECHO SCRIPT: Dump of Script Variables...                                  >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
ECHO Log File............: %mLog%                                         >>%mLog%
ECHO.                                                                     >>%mLog%

ECHO ==================================================================== >>%mLog%
ECHO LOGON: Disable logins to this server...                              >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
CHANGE LOGON /DISABLE

MSG * /TIME:30 This server will be rebooted in 7 minutes. Please save all work, close applications and log out. Other Servers will be available for Application Service...
ECHO - Pause script for 7 minutes to allow users to finish their work...  >>%mLog%
SLEEP 120

MSG * /TIME:30 This server will be rebooted in 5 minutes. Please save all work, close applications and log out. Other Servers will be available for Application Service...
ECHO - Pause script for 5 minutes to allow users to finish their work...  >>%mLog%
SLEEP 240

MSG * /TIME:30 This server will be rebooted in 1 minute. Please save all work, close applications and log out. Other Servers will be available for Application Service...
ECHO - Pause script for 1 minute to allow users to log out...             >>%mLog%
SLEEP 60

ECHO - Closing any active sessions...                                     >>%mLog%
QUERY session >%mLogFileFolder%\session.txt
ECHO.                                                                     >>%mLog%
TYPE %mLogFileFolder%\session.txt                                         >>%mLog%

FOR /F "skip=2 tokens=3," %%i in (%mLogFileFolder%\session.txt) DO LOGOFF %%i
SLEEP 15
DEL /F /Q %mLogFileFolder%\session.txt
ECHO.                                                                     >>%mLog%

GOTO :CONTINUE

:CONTINUE
ECHO ==================================================================== >>%mLog%
ECHO MAINT: Clean stuck Profiles...                                       >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
ECHO - Deletes Profiles except All Users, Default User, Administrator, AM Accounts and VScanServ... >>%mLog%
DIR %mProfDir% /AD /B |FIND /V /I "All Users" |FIND /V /I "LocalService" |FIND /V /I "NetworkService" |FIND /V /I "ddevsvc" |FIND /V /I "srvshedr" |FIND /V /I "Default User" |FIND /V /I "Administrator" |FIND /V /I "VScanServ" |FIND /V /I "osgvservice" |FIND /V /I "am" >%mLogFileFolder%\Profs.txt
FOR /F "tokens=1-6" %%a IN (%mLogFileFolder%\Profs.txt) DO CALL :DELPROFS %%a %%b %%c %%d %%e %%f
DEL /F /Q %mLogFileFolder%\Profs.txt
ECHO.                                                                     >>%mLog%

ECHO ==================================================================== >>%mLog%
ECHO MAINT: Clean Temp folder...                                        >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
DEL /F /Q /S "%mTempDir%\*.*" >>%mLog% 2>&1
DIR %mTempDir% /AD-H /B >%mLogFileFolder%\TempDir.txt
TYPE %mLogFileFolder%\TempDir.txt                                         >>%mLog%
FOR /F "tokens=1-6" %%a IN (%mLogFileFolder%\TempDir.txt) DO RD /S /Q "%mTempDir%\%%a"
SLEEP 15
DEL /F /Q %mLogFileFolder%\TempDir.txt
ECHO.                                                                     >>%mLog%

ECHO ==================================================================== >>%mLog%
ECHO MAINT: Clean Temp folder...                                        >>%mLog%
ECHO -------------------------------------------------------------------- >>%mLog%
DEL /F /Q /S "%mCTempDir%\*.*" >>%mLog% 2>&1
DIR %mCTempDir% /AD-H /B >%mLogFileFolder%\CTempDir.txt
TYPE %mLogFileFolder%\CTempDir.txt                                         >>%mLog%
FOR /F "tokens=1-6" %%a IN (%mLogFileFolder%\CTempDir.txt) DO RD /S /Q "%CTempDir%\%%a"
SLEEP 15
DEL /F /Q %mLogFileFolder%\CTempDir.txt
ECHO.  

:: --- Finishing...
ECHO ==================================================================== >>%mLog%
SET mDate=%DATE%
SET mTime=%TIME%
ECHO The server is shutting down for a reboot                             >>%mLog%
ECHO Date and Time : %mDate% @ %mTime%                                    >>%mLog%
ECHO.                                                                     >>%mLog%
ECHO End of Logfile ----------------------------------------------------- >>%mLog%

:FINISHED
CHANGE LOGON /Enable
TSSHUTDN 0 /reboot /delay:0
EXIT

:: --- Script Functions, keep at EOF ------------------------------------------
:DELPROFS
:: --- Delete Stuck Profiles
ECHO Trying to Delete %mProfDir%\"%*"                                     >>%mLog%
DEL /F /Q /S %mProfDir%\"%*" >>%mLog% 2>&1
RD /Q /S %mProfDir%\"%*" >>%mLog% 2>&1
GOTO :RETURN
:: --- end function

:: --- Use this to return to caller
:RETURN

Author

Commented:
Vishva, I dont want to send notification to users, we need to wait users to log off from citrix server and once there are no users logged on, then the server will have to reboot itself. We are not sure what exactly user doing. Some time uesr with customers in that case forcing user log off is not a good option.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Basraj, I tried the script which you suggested, after all the user logged no one logged in server didnt rebooted.

- Server disabled the login
- no users logged in
- only port listining is active, but server not rebooted.
- finally I logged to server console and restated the server then enabled same in xitrix server.

ENV - Windows 2003 Server, PS 4.5 & Xenapp.
ss.jpg

Author

Commented:
log.txt output

Tue 05/17/2011
Starting shutdown script
Aborted - Shutdown script timed out.
exited

Author

Commented:
Today I downloaded script from http://www.dabcc.com/miab/reboots/smartreboot.zip and tested, its working fine, but when users are working it sends the notification and restarted the server.

we need to wait users to log off from citrix server and once there are no users logged on, then the server will have to reboot itself. Please help me to get same in script
sc.jpg
Citrix Technology Professional - Fellow
Top Expert 2010
Commented:
Top Expert 2010
Commented:

Author

Commented:
Thanks Carl & Basraj, will check and update.
Top Expert 2010

Commented:
Also check this Centinel tool which will reboot only after the users are off.

http://www.dabcc.com/downloadfile.aspx?id=271

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial