Pause Php Process Make Update to Script Start Again With New Script

At that place are many reasons to interruption script execution. Perchance a script needs to wait for user input, slow downwards execution speed, or wait for some other process to conclude before moving forward. As with most tasks in Powershell, there is more than than one way to intermission a script. Depending on what you are trying to accomplish, the method of pausing you cull will have its strengths and weaknesses. Adding in another contraction to the available options is the new cross-platform power of PowerShell. Since PowerShell is often used as a way to "glue" dissimilar technologies together, simply like in Windows, the Linux command-line has its options for pausing execution which can be incorporated into PowerShell equally well.

Methods of PowerShell Pause

What are the different ways to pause script execution? In this article, we are going to intermission downward the power to interruption into either native and non-native commands. By native we refer to those commands that are PowerShell or .NET congenital-in functions or methods. Non-native methods are defined as programs that are specific to Windows and Linux that could be used in conjunction with PowerShell.

Native Methods

  • First-Sleep
  • [Organization.Threading.Thread]::Sleep()
  • Read-Host
  • [Panel]::ReadKey()
  • $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Not-Native Methods

Windows

  • break
  • timeout

Linux

  • sleep
  • read -p

Equally you can tell, there are a number of different options, each with their benefits and drawbacks. Which one you utilise will influence how execution is paused and what other effects that pause may include.

Native Methods

To start with, we volition begin outlining the native methods equally they are almost used in scripts.

Start-Slumber

The most normally used intermission control is past far, Showtime-Sleep. This command takes 2 simple inputs, which are -Seconds and -Milliseconds. Seconds tin be a Organization.Double number value while milliseconds takes only Organization.Int32 values. By combining the 2, precise command over the length of the pause can be achieved. Start-Slumber has an alias of slumber too.

            Start-Sleep -Seconds two -Milliseconds 300          

Before PowerShell vi.2.0, you would not exist able to write a fractional seconds value like, 2.3 which is the same length of fourth dimension we are pausing for in the in a higher place code. With 6.ii.0, fractional values are now supported, so instead of writing out the seconds and milliseconds independently, y'all can now state: -Seconds 2.3.

It's important to note that you can break out of a First-Sleep office by using Ctrl-C. This is not true for all methods!

Thread Sleep

A less commonly used method of pausing execution is the [System.Threading.Thread]::Sleep() .Net method. By passing in a System.Int32 or a TimeSpan value, this allows pausing of the current thread for a set amount of time. For example, to pause by that same corporeality of time, 2.iii seconds, every bit in our Start-Sleep example, we tin can write:

            [Arrangement.Threading.Thread]::Sleep(2300)          

To utilize a timespan construct with the Thread Sleep method, you tin can practice the following:

            [System.Threading.Thread]::Sleep([TimeSpan]::New(0, 0, 0, two, 300))          

To indefinitely pause a thread, you can use the [System.Threading.Timeout]::InfiniteTimeSpan but keep in mind this may lock up your process.

Different Start-Sleep, the Thread Slumber method does non allow you to break out of the sleep routine using Ctrl-C. Depending on your application, this may be preferable.

Besides important to know well-nigh thread sleep is that information technology is not an verbal time guarantee. All thread sleep has a guarantee that it will wait at least that many seconds. Since thread sleep counts coincide with clock ticks, then other programs may interrupt this pause part and force the expect to take longer.

While thread sleep has its uses, information technology is better to use one of the other built-in methods because of these issues.

Read-Host

The above methods describe ways to pause execution, but only for a ready amount of time. The other mode to intermission execution is by prompting for user input. With this method, a transmission action must be taken, but it can be very useful to crave a confirmation to go along and potentially include additional input.

Read-Host is one of the near mutual ways to prompt for user input. By prompting for user input, the resulting entry can exist saved for further utilise in the lawmaking, or to pause execution as necessary.

            Read-Host -Prompt "Press any cardinal to continue"          

Once text has been entered, information technology's necessary to printing the enter or return primal to move execution forth. Past doing and so, and without saving the input to a variable, this will output the same text entered to the panel.

Press Enter to Move Execution Along
Press Enter to Move Execution Along

If you wanted to salve the resulting text entered into a variable for later utilise, simply assign the Read-Host input to a variable.

            $Input = Read-Host -Prompt "Press any key to continue"          

Sometimes you may want to read in the input as a secure string. Thankfully, Read-Host makes that trivially easy.

            $SecureInput = Read-Host -Prompt "Enter your countersign" -AsSecureString          

Ctrl-C will allow you lot to break out of the Read-Host prompt.

Console ReadKey Method

Using the [Console]::ReadKey() method, you tin can hands read in central commands, forth with modifiers, while pausing the program execution. By default, the key commands sent are echoed back to the screen as a System.ConsoleKeyInfo object. This includes the Cardinal, KeyChar, and the Modifiers values, which can be very useful when checking for certain primal combinations.

Console ReadKey Command
Panel ReadKey Command

Often you lot may want to check for a sure key input earlier moving on continually. This tin hands be done using a Do-While loop like below.

            Exercise { 	$Key = [Console]::ReadKey($True) 	Write-Host $Key.Cardinal } While ( $Fundamental.Key -NE [ConsoleKey]::Escape )                      
Do-While Loop Key
Practise-While Loop Fundamental

Delight note that since we are looking for a specific key combination, Ctrl-C will not pause out of the input here.

Host RawUI ReadKey Method

Finally, we accept the $host.UI.RawUI.ReadKey() method. This is similar to the Console ReadKey method, but this method allows for passing in additional options into the ReadKey method to control beliefs and output further.

RawUI.ReadKey
RawUI.ReadKey

At that place are a number of ReadKeyOptions that can be passed in which may make the RawUI ReadKey method more than useful.

  • AllowCtrlC
  • IncludeKeyDown
  • IncludeKeyUp
  • NoEcho

This can be used in the following way which way include key down events and not repeat the text out.

            $host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')          

This method will let the employ of Ctrl-C to intermission out of the command, except if the AllowCtrlC choice is used.

Not-Native Methods

In this section, nosotros explore a few not-native methods of pausing script execution. These methods are intended for utilizing non-PowerShell console-specific utilities that can be integrated into PowerShell scripts depending on your needs.

Windows

Break

The interruption command is very simple, and volition display Press any fundamental to continue . . . and remain that way until a key is pressed to resume execution.

Pause Command
Pause Command

You may ask how to use this in a script, but just like you would any other command, insert the cmd /c 'suspension' command within your script to utilize the native functionality.

Cmd/C Pause Command
Cmd/C Pause Command

Timeout

Typically used in Windows batch files, the timeout command allows for pausing a specified number of seconds and optionally ignore any key commands. A user keystroke will typically resume execution even if the timeout period hasn't elapsed. Information technology is very merely used by passing in the number of seconds.

            # Pause for ii seconds timeout /t 2  # Break for v seconds merely disallow keystroke breaks timeout /t 5 /nobreak  # Intermission indefinitely until a primal is pressed timeout /t -one          
Timeout Command
Timeout Command

Just every bit with the pause command, insert the timeout command within the script and in the example beneath, we hitting the enter primal to go along.

Timeout Command Execution
Timeout Control Execution

Linux

Sleep

Similar the pause control in Windows, the slumber control in Linux allows for setting an capricious value for slumber. Dissimilar the break control, the slumber control has a few more options that allow for flexibility in slumber fourth dimension.

Sleep Command
Sleep Command

The slumber control likewise optionally has suffixes that tin allow for longer periods of time to exist defined. Examples:

  • sleep 1m – Sleep for one minute
  • slumber 2h – Slumber for 2 hours
  • sleep 1d – Sleep for ane 24-hour interval

Read -P

Finally, the other way that Linux tin can pause is by using the read application to pause for the enter key to exist hitting to continue execution. There is an optional timeout parameter that will continue execution if the enter cardinal is non hit by the specified time.

            read -p 'Press enter to go on' -t five          
Read Application to Pause
Read Application to Intermission

Conclusion

As you can see, there are many ways to pause execution within a script or on the command line. Depending on your needs, it could exist every bit simple equally a Kickoff-Sleep command or a more complex input-driven process. Combined with native commands, PowerShell pause allows for flexibility in virtually any surroundings and maximum script utility.

riceagettold.blogspot.com

Source: https://adamtheautomator.com/powershell-pause/

0 Response to "Pause Php Process Make Update to Script Start Again With New Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel