question

MarioNeves-6886 avatar image
0 Votes"
MarioNeves-6886 asked RichMatheisen-8856 commented

PSScheduledJob always Running

Hi,

I have a simple Job Running everyday to Restart the server, but the state is always "Running", is this normal ? Just one is completed.

Id Name PSJobTypeName State HasMoreData Location Command


1 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force
2 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force
3 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force
4 Restart-Server PSScheduledJob Completed False localhost Restart-Computer -force
5 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force
6 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force
7 Restart-Server PSScheduledJob Running False localhost Restart-Computer -force


Thank you,
MN

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered LimitlessTechnology-2700 published

Hi there,

Yes, it is normal for the Process to run in the task scheduler .




If the reply is helpful, please Upvote and Accept it as an answer

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarioNeves-6886 avatar image
0 Votes"
MarioNeves-6886 answered

Hi,

thank you for your answer, my doubt is the process doesn't finish, it's always in the state of running, only one job in 10 is completed.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

You've shown us the status of the jobs, but not the code that the job runs. Can you post the code?

Is there anything in the code that would cause the job to not complete?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarioNeves-6886 avatar image
0 Votes"
MarioNeves-6886 answered MarioNeves-6886 edited

Hi,

thank you to take your time :).


the job cannot be more simple than this ....



$T = New-JobTrigger -Daily -At 6:01AM

Register-ScheduledJob -Name Restart-Server -FilePath "C:\restart-vm.ps1" -Trigger $T


My doubt is also quite simple, should not the job be finished (completed), sometimes the job may have problems, but after being executed should go to the state completed.


PS: "Restart-vm -force" is the command


Thank you,
Mario

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

So your assumption is that the Restart-VM always succeeds? The "-Force" parameter just says not to ask for confirmation before restarting. But a "stuck" VM isn't going to (or can't) accept the command at all.

Can you add a time limit to the scheduled job?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarioNeves-6886 avatar image
0 Votes"
MarioNeves-6886 answered MotoX80 commented

Hi,

So your assumption is that the Restart-VM always succeeds?

Not always, but most of the time, and more strange is the VM restart and the jobs continue in the Running state, like nothing happened.

Can you add a time limit to the scheduled job?

I can do that and force it to stop, I was just trying to understand the behavior.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Add a transcript to your script to see if an error is detected.

 Start-Transcript -Path c:\Logs\Restart-txt -Append"
 Restart-vm -force
 Stop-Transcript
0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

Perhaps it's because the Reset-VM is the equivalent of powering off a machine. That'll break the connection to the VM and prevent your job from receiving a result.

Maybe adding something like this to your job's script after the Reset-VM?

 while ((get-vm -name <vmname>).state -eq 'Off') {    # or maybe "-ne 'Running' instead of 'Off'??
     Start-Sleep -s 5
 }


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

I I restart the VM, I don't do reset, do you think it's the same ?

Tkx

0 Votes 0 ·

Yes, the Restart-VM also results in a "hard" reset.

0 Votes 0 ·