You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

需求:配置单次运行CronJob监控目录,实现tar.gz文件触发执行monitor.py

Optimize Shell Script & CronJob: Run monitor.py Only When New .tar.gz Files Are Added

Great question! Your original cron-based setup has two key pain points: it runs monitor.py repeatedly as long as any .tar.gz files exist in the directory, and it can miss file changes between cron intervals. Let's fix this with a real-time monitoring approach that triggers the script only when a new .tar.gz file is added, plus options to run the monitor persistently or as a one-time task, aligning exactly with your needs.

First: Ditch Cron Polling for Real-Time Monitoring

Instead of checking the directory on a fixed schedule, use inotifywait (part of the inotify-tools package) to watch for actual file creation events. This is far more efficient and ensures you only trigger monitor.py when a new .tar.gz lands in /home/cassandra.

Step 1: Install inotify-tools (if not already present)

# Debian/Ubuntu-based systems
sudo apt-get install inotify-tools

# RHEL/CentOS/Rocky Linux-based systems
sudo yum install inotify-tools

Step 2: Write the Optimized Monitoring Script

Create a script named tarball_monitor.sh with this content:

#!/bin/bash
MONITOR_DIR="/home/cassandra"

# Continuously watch for new files, filter for .tar.gz, and run monitor.py once per new file
inotifywait -m -e create --format '%f' "$MONITOR_DIR" | while read FILENAME; do
    if [[ "$FILENAME" == *.tar.gz ]]; then
        echo "New .tar.gz file detected: $FILENAME. Running monitor.py..."
        python monitor.py
    fi
done

Here's what the key flags do:

  • -m: Keep monitoring indefinitely (don't exit after the first event)
  • -e create: Only listen for file creation events (excludes edits or deletes)
  • --format '%f': Output only the filename to simplify our file type check

Make the script executable:

chmod +x tarball_monitor.sh

Step 3: Run the Monitor Persistently (Or As a One-Time Task)

You mentioned wanting a "one-time CronJob to monitor the directory"—but if you need ongoing monitoring, a cron job isn't the right fit. Instead, use one of these methods to keep the monitor running reliably:

Option A: Run in the Background Temporarily (For Testing)

Use nohup to run the script in the background even after you log out:

nohup ./tarball_monitor.sh > /var/log/tarball_monitor.log 2>&1 &

Logs will go to /var/log/tarball_monitor.log so you can debug if needed.

To make the monitor start automatically on boot and restart if it crashes, create a systemd service:

  1. Create /etc/systemd/system/tarball_monitor.service with this content:
[Unit]
Description=Monitors /home/cassandra for new .tar.gz files
After=network.target

[Service]
User=your_username  # Replace with the user that should run the script (e.g., cassandra)
WorkingDirectory=/path/to/your/scripts  # Replace with where your script is stored
ExecStart=/path/to/your/scripts/tarball_monitor.sh
Restart=always  # Auto-restart if the script crashes unexpectedly

[Install]
WantedBy=multi-user.target
  1. Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl start tarball_monitor.service
sudo systemctl enable tarball_monitor.service

If You Really Need a One-Time Check

If you only want to check for new .tar.gz files once (not ongoing), we can adjust the script to compare timestamps and run monitor.py only if new files exist since the last check:

Create one_time_tarball_check.sh:

#!/bin/bash
MONITOR_DIR="/home/cassandra"
LAST_CHECK="/tmp/tarball_last_run.txt"

# Initialize the last check timestamp if it doesn't exist
[[ ! -f "$LAST_CHECK" ]] && touch "$LAST_CHECK"

# Find .tar.gz files added since the last check
NEW_FILES=$(find "$MONITOR_DIR" -type f -name "*.tar.gz" -newer "$LAST_CHECK")

if [[ -n "$NEW_FILES" ]]; then
    echo "New .tar.gz files found. Running monitor.py..."
    python monitor.py
    # Update the last check timestamp to now
    touch "$LAST_CHECK"
fi

Run it once immediately with at (better than cron for one-time tasks):

echo "/path/to/one_time_tarball_check.sh" | at now

Or schedule it for a specific time with cron (then delete the cron entry after it runs):

# Add to crontab (example: runs at 10 AM tomorrow)
crontab -e
# Add this line:
0 10 * * * /path/to/one_time_tarball_check.sh

内容的提问来源于stack exchange,提问作者Ajay Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:52:46