Linux下按进程名限制Core Dumps数量的标准方法问询
Great question! Linux doesn’t offer a native one-liner to directly cap core dumps per process out of the box, but there are several mature, widely accepted workarounds that align with what you’re looking for—either a system-wide rule for all apps or per-process controls.
Global Limit: Core Naming + Auto-Cleanup Script
If you want a universal rule that caps every application to X core dumps, here’s a straightforward approach:
1. Configure Structured Core Dump Naming
First, make sure each process’s core dumps are uniquely identifiable by name/ID. Edit /etc/sysctl.conf to set a structured core pattern:
kernel.core_pattern = /var/core/core_%e_%p_%t
%e= Process name%p= Process ID%t= Timestamp
Apply the change immediately with:
sudo sysctl -p
2. Auto-Clean Old Core Files
Create a simple bash script to prune old core dumps per app. Save this as /usr/local/bin/clean_cores.sh:
#!/bin/bash CORE_DIR="/var/core" MAX_CORES_PER_APP=3 # Adjust this number to your desired limit cd "$CORE_DIR" || exit # Group core files by app name, keep only the newest N, delete the rest for app in $(ls core_* 2>/dev/null | awk -F'_' '{print $2}' | sort -u); do ls -t core_"$app"_* | tail -n +$((MAX_CORES_PER_APP + 1)) | xargs rm -f 2>/dev/null done
Make it executable:
sudo chmod +x /usr/local/bin/clean_cores.sh
Run it automatically with cron (e.g., every minute to keep core counts in check):
sudo crontab -e # Add this line to the crontab: * * * * * /usr/local/bin/clean_cores.sh
For more real-time cleanup, use inotifywait to trigger the script whenever a new core file is added to /var/core.
Per-Process Limit
If you prefer to set custom limits for individual applications, try either of these methods:
1. Wrap the App in a Startup Script
Create a wrapper script that manages core dumps for your specific app. For example, for an app named my_app:
#!/bin/bash APP_NAME="my_app" MAX_CORES=2 CORE_DIR="/var/core/$APP_NAME" mkdir -p "$CORE_DIR" # Clean old cores before launching the app ls -t "$CORE_DIR"/core_* 2>/dev/null | tail -n +$((MAX_CORES + 1)) | xargs rm -f 2>/dev/null # Set core dump path for this process and launch the app export CORE_PATTERN="$CORE_DIR/core_%p_%t" exec "$APP_NAME" "$@"
Use this script to launch your app instead of running it directly—every time it starts, it’ll ensure only the newest 2 core dumps are retained.
2. Indirect Limit via Cgroup Disk Quotas
While cgroups don’t directly limit core dump counts, you can assign a disk quota to the app’s cgroup that only fits X core dumps. When the quota is hit, subsequent core dumps will fail. This is less precise than the script method, but works well if you’re already using cgroups for resource management.
Quick Notes
- Ensure
/var/corehas proper write permissions for your processes:sudo chmod 777 /var/core(tighten this to match your security policies if needed). - If your system uses
systemd-coredump(common on modern distros), check/etc/systemd/coredump.conffor global disk space limits (MaxUse,KeepFree), but these aren’t per-process count limits—pair them with the cleanup script for count-based control.
内容的提问来源于stack exchange,提问作者JimPri

