Harness the power of the Prefect CLI to execute and schedule shell commands as Prefect flows. This guide shows how to use the watch and serve commands to showcase the CLI's versatility for automation tasks.
Here's what you'll learn:
Running a shell command as a Prefect flow on-demand with watch.
Scheduling a shell command as a recurring Prefect flow using serve.
The benefits of embedding these commands into your automation workflows.
The watch command wraps any shell command in a Prefect flow for instant execution, ideal for quick tasks or integrating shell scripts into your workflows.
When you need to run shell commands on a schedule, the serve command creates a Prefect [deployment](/concepts/deployments/ for regular execution. This is an extremely quick way to create a deployment that is served by Prefect.
To set up a daily weather report for Chicago at 9 AM, you can use the serve command as follows:
prefectshellserve"curl http://wttr.in/Chicago?format=3"--flow-name"Daily Chicago Weather Report"--cron-schedule"0 9 * * *"--deployment-name"Chicago Weather"
This command schedules a Prefect flow to fetch Chicago's weather conditions daily, providing consistent updates without manual intervention. Additionally, if you want to fetch the Chicago weather, you can manually create a run of your new deployment from the UI or the CLI.
To shut down your server and pause your scheduled runs, hit ctrl + c in the CLI.
With the watch and serve commands at your disposal, you're ready to incorporate shell command automation into your Prefect workflows. You can start with straightforward tasks like observing cron jobs and expand to more complex automation scenarios to enhance your workflows' efficiency and capabilities.
Check out the tutorial and explore other Prefect docs to learn how to gain more observability and orchestration capabilities in your workflows.