How to Use Claude Code /loop for Recurring Monitoring Tasks
Problem
I was deploying a critical update to staging and needed to monitor the CI/CD pipeline. But I didn’t want to:
- Keep switching to GitHub Actions in my browser
- Set up a cron job just for a 30-minute deployment
- Install another monitoring tool
I just wanted Claude to check every few minutes and tell me when something changed.
What I tried first
My initial approach was manual polling:
Me: Check the deployment statusClaude: The build is running... 3/5 steps complete.
[5 minutes later]Me: Check againClaude: Still running... 4/5 steps complete.
[3 minutes later]Me: Check deploymentClaude: Deployment succeeded! The staging environment is live.This worked but required me to remember to check. I missed an error window because I got distracted by a Slack notification.
Then I considered setting up a proper monitoring solution:
Option 1: cron job- Write a shell script- Configure cron- Parse JSON output- Send notifications- Remove when done
Option 2: CI/CD notifications- Configure GitHub Actions webhooks- Set up Slack integration- Or email notifications
Option 3: Monitoring service- Sign up for Datadog/PagerDuty- Configure dashboards- Pay for infrastructureAll of these felt like overkill for a temporary monitoring need during a deployment.
The solution: /loop command
Claude Code has a built-in /loop command that creates a session-level recurring task. Here’s what I tried:
/loop 5m check the deploy status and alert if there are errorsClaude responded:
[Loop started] Checking deployment status every 5 minutes...
Check 1 (12:00): Build in progress - 2/5 jobs completeCheck 2 (12:05): Build in progress - 4/5 jobs completeCheck 3 (12:10): Build succeeded! Staging deployment starting...Check 4 (12:15): Deployment complete. Application is live.
No errors detected. Continue monitoring? (loop will auto-cancel in 30 min of no activity)This was exactly what I needed. Claude checked automatically while I continued working in the same terminal.
How /loop works
The syntax is straightforward:
/loop [interval] [prompt]Valid intervals:
| Syntax | Meaning | Best For |
|---|---|---|
30s | 30 seconds | Quick log tailing |
1m | 1 minute | API health checks |
5m | 5 minutes | Deployment monitoring |
10m | 10 minutes | Periodic reports |
1h | 1 hour | Long-running checks |
Key behaviors I observed:
- Runs in background: Claude continues executing your loop while you work on other tasks
- Session-scoped: The loop stops when you close your Claude Code session
- Single active loop: Starting a new loop replaces the previous one
- Reports results: Each iteration shows you what it found
Practical examples I tested
CI/CD monitoring during deployment
# Start deploymentnpm run deploy:production
# Set up monitoring loop/loop 2m check if the CI build completed and report any failures
# Continue with other work...# Claude reports: "Build step 3/5 complete"# Claude reports: "Build step 5/5 complete - deploying to production"# Claude reports: "Deployment succeeded!"
# Cancel when done/loop stopLog file watching
I was debugging an intermittent error in production:
/loop 30s check logs/app.log for any ERROR entries in the last minute and summarize
# Claude reports every 30 seconds:# "No errors in last 60 seconds"# "No errors in last 60 seconds"# "Found 2 ERROR entries:# - NullPointerException at UserService.java:145# - ConnectionTimeout at DatabasePool.java:89"
/loop stopAPI health checks
During a database migration, I wanted to verify API availability:
/loop 1m curl https://api.example.com/health and report the status code and response time
# Claude reports:# "Status: 200, Response time: 45ms"# "Status: 200, Response time: 52ms"# "Status: 503, Response time: 5000ms - SERVICE DEGRADED"# "Status: 200, Response time: 48ms - Recovered"Code quality during development
While refactoring a large codebase:
/loop 5m run npm run lint and report if new errors appeared since last check
# Claude reports:# "15 errors (no change from last check)"# "18 errors - 3 new issues in utils.ts"# "12 errors - 6 errors fixed, 3 new issues in api.ts"Important limitations I discovered
Through trial and error, I learned these constraints:
1. Session-scoped execution
What I expected: Loop continues even if I close my laptop
What actually happens: Loop stops when Claude Code session endsThis makes /loop perfect for:
- Active development sessions
- Ongoing deployments
- Debugging sessions
But NOT for:
- Overnight monitoring
- Permanent health checks
- Production alerting
2. Single loop per session
/loop 5m check deployment status# (works fine)
/loop 1m check log errors# (this REPLACES the previous loop)You can only have one active loop. Starting a new one cancels the previous one.
3. Intervals are approximate
Claude processes each check before scheduling the next. A complex check might take 30 seconds, so a 1m interval might actually run every 90 seconds:
Scheduled: Check every 1 minute
Actual timeline:12:00:00 - Check started12:00:25 - Check completed (took 25s)12:00:25 - Next check scheduled for 12:01:2512:01:25 - Check started12:01:30 - Check completed (took 5s)12:01:30 - Next check scheduled for 12:02:30When to use /loop vs alternatives
I made a decision matrix:
| Scenario | /loop | Cowork Scheduled | cron |
|---|---|---|---|
| Monitor deployment during release | Yes | Overkill | No |
| Daily report generation | No | Yes | Yes |
| Temporary log watching | Yes | No | No |
| Permanent production monitoring | No | Yes | Yes |
| Debugging session with periodic checks | Yes | No | No |
For persistent scheduled tasks, the Reddit discussion mentioned Cowork’s scheduled tasks feature as a better alternative. But for temporary, session-based monitoring, /loop fills the gap perfectly.
Best practices I developed
1. Be specific in your prompts
# VAGUE - Claude might not know what to check/loop 5m watch the deploy
# SPECIFIC - Clear success/failure criteria/loop 5m check if deployment succeeded and report any error messages from the build log2. Set appropriate intervals
# TOO SHORT for complex operations/loop 10s run the full test suite# This might not finish before the next trigger
# BETTER/loop 5m run the full test suite and report results3. Include cleanup in your workflow
# Start monitoring/loop 2m check staging health endpoint
# ... do your work ...
# When deployment is verified, STOP the loop/loop stop
# Otherwise it continues until session ends4. Combine with notifications for critical ops
For critical deployments, I use both:
/loopfor in-terminal monitoring- Claude Channels for mobile alerts if something breaks
A complete workflow example
Here’s my full deployment monitoring workflow:
# 1. Start the deploymentgit push origin main
# 2. Set up monitoring immediately/loop 1m check the GitHub Actions workflow status at .github/workflows/deploy.yml
# 3. Claude reports periodically:# "Workflow started - job 'test' running"# "Job 'test' passed - job 'build' starting"# "Job 'build' passed - job 'deploy' starting"# "Job 'deploy' failed - Error: Cannot connect to Kubernetes cluster"
# 4. When I see an error, investigatekubectl get pods -n production
# 5. Fix the issue and re-rungit push origin main
# 6. Monitor again/loop 1m check the deployment workflow status
# 7. Success! Clean up/loop stopSummary
In this post, I showed how to use Claude Code’s /loop command for lightweight, session-level recurring monitoring. The key point is that /loop creates temporary monitoring without any infrastructure setup.
The command fills a specific gap: you need recurring monitoring for the next hour, not the next month. For deployments, debugging sessions, and temporary health checks, /loop eliminates the need for separate tooling.
Remember the limitations:
- Session-scoped (stops when Claude Code closes)
- Single loop per session
- Intervals are approximate
For persistent scheduled tasks, consider Cowork’s scheduled tasks feature or traditional cron. But for “I need to watch something while I work,” /loop is the simplest solution.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments