Create Scheduled Task to Restart BES Client hourly

Disclaimer: I’m a bit of a scripting novice (all forms) so please be patient with me.

I am trying to create a BigFix fixlet that will create a scheduled task on a bunch of servers that will stop and start the BES Client service hourly.

There’s a built-in fixlet in our BigFix environment called “Automatically Restart Stopped BES Clients Using TaskScheduler”. This contains two actions and second is called “Click here to schedule a BES Client restart task providing your own run schedule. (You will be prompted for the schedule time and interval).”

When I run this action, it prompts for the Schedule time (19:00) and the schedule Interval (M,T,W,Th,F,S,Su) but I need it to run even if the services is already started and fear this task only runs if the services is ‘stopped’.

When I input my parameters and setup the action this is the script that will be run (if it helps).

action parameter query “RunTime” with description “Please supply the schedule time. (Use 24 hour time in the format HH:MM)” with default “”

action parameter query “Interval” with description “Please specify the schedule interval as one or more days of the week (use the following abbreviations: M,T,W,Th,F,S,Su) or one or more days of the month (use the numbers 1 through 31). Use commas to separate multiple interval entries.” with default “”

if {(version of operating system < “6.2”)}
dos at {parameter “RunTime” of action} {if (parameter “Interval” of action equals “”) then ("") else ("/every:" & parameter “Interval” of action)} sc config BESClient start= delayed-auto

dos at {(first 5 of following text of position 17 of ((((first 17 of (now as string)) & parameter “RunTime” of action & (last 9 of (now as string))) as time + 1 * minute) as string))} {if (parameter “Interval” of action equals “”) then ("") else ("/every:" & parameter “Interval” of action)} “{pathname of system folder & “\cmd.exe”}” /c net start BESClient
else

parameter “scheduleMON” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “m”))) as integer >=1 ) then (“MON,”) else (”")}“
parameter “scheduleTUE” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “t”))) as integer >=1 ) then (“TUE,”) else (””)}“
parameter “scheduleWED” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “w”))) as integer >=1 ) then (“WED,”) else (””)}“
parameter “scheduleTHU” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “th”))) as integer >=1 ) then (“THU,”) else (””)}“
parameter “scheduleFRI” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “f”))) as integer >=1 ) then (“FRI,”) else (””)}“
parameter “scheduleSAT” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “s”))) as integer >=1 ) then (“SAT,”) else (””)}“
parameter “scheduleSUN” = “{if ((number of ((substrings separated by “,” of (parameter “Interval” of action as lowercase)) whose (it equals “su”))) as integer >=1 ) then (“SUN,”) else (””)}"

parameter “temp” = “{parameter “scheduleMON” & parameter “scheduleTUE” & parameter “scheduleWED” & parameter “scheduleTHU” & parameter “scheduleFRI” & parameter “scheduleSAT” & parameter “scheduleSUN”}“
parameter “scheduleALL” = “{if (parameter “temp” is not equal to “”) then (preceding text of last “,” of parameter “temp”) else (””)}”

dos schtasks /create /tn RestartStoppedBESClientsa21 /f /ru SYSTEM /st {parameter “RunTime” of action} {if (parameter “scheduleALL” equals “” AND parameter “Interval” of action is not equal to “”) then ("/sc monthly /d " & concatenation “-” of (substrings separated by “,” of parameter “Interval” of action)) else if (parameter “Interval” of action is not equal to “”) then ("/sc weekly /d " & parameter “scheduleALL”) else ("/sc weekly /d MON,TUE,WED,THU,FRI,SAT,SUN")} /tr “{pathname of system folder & “\sc.exe config BESClient start= delayed-auto”}”

dos schtasks /create /tn RestartStoppedBESClientsa22 /f /ru SYSTEM /st {(first 5 of following text of position 17 of ((((first 17 of (now as string)) & parameter “RunTime” of action & (last 9 of (now as string))) as time + 1 * minute) as string))} {if (parameter “scheduleALL” equals “” AND parameter “Interval” of action is not equal to “”) then ("/sc monthly /d " & concatenation “-” of (substrings separated by “,” of parameter “Interval” of action)) else if (parameter “Interval” of action is not equal to “”) then ("/sc weekly /d " & parameter “scheduleALL”) else ("/sc weekly /d MON,TUE,WED,THU,FRI,SAT,SUN")} /tr "{pathname of system folder & “\cmd.exe /c net start BESClient”}"
endif

First of all, it is not advised to restart the BESClient regularly. Instead, try to resolve the problem instead of repeatedly restarting the client. However, there may be a number of causes for this, in which case you can apply the following solution:

delete __appendfile
appendfile sc stop besclient
appendfile sc start besclient
move __appendfile "{parent folder of regapp "besclient.exe"}\RestartBESClient.bat"
dos schtasks /create /RU "SYSTEM" /SC MINUTE /MO 60 /TN RestartBESClient /tr "{parent folder of regapp "besclient.exe"}\RestartBESClient.bat" 

I have also encountered problems where BESClient stop service requests hang and the client fails to report. In these cases, I advise adding additional batch file checks to determine if the service has begun. If not, try to locate the problematic process and end it before attempting to restart the service.

1 Like

Thanks @vk.khurava - I think the last line of your script needs changing as when the job gets created the program/script/arguments are slightly incorrect.

It gets created like this which is not launching the ‘RestartBESClient.bat’ file.

broken

I need it to be created like this, please help?

working

ahh forgot about that, replace that with below-

dos schtasks /create /RU "SYSTEM" /SC MINUTE /MO 60 /TN RestartBESClient /tr '"{parent folder of regapp "besclient.exe"}\RestartBESClient.bat"'

Thanks, this slight change worked. Also, for some reason when the task ran it didn’t start (stopped fine). I just ended up changing the commands from sc stop/start to use net stop/start and worked fine.

One other question, at the moment the fixlet is applicable to every machine as the relevance is not filtered that much and making it available to any machine where OS contains Win.

Do you think there is a way to setup a relevance clause (or analysis, maybe this is better?) to track which machines have had the Scheduled Task created. i.e. An analysis that looks for Scheduled Task called ‘RestartBesClient’?

here you go -

not exists scheduled task whose (name of it = "RestartBESClient" and enabled of it)

Thanks, I’ve setup the analysis and will let it run over the weekend. Appreciate your assistance.

@vk.khurava - Thanks - This is all working as I needed. Appreciate your help here.

1 Like