Stop fighting with file ownership after cron jobs run as root

Permissions & Security (Chmod/Chown/ACLs/SELinux/AppArmor)

Stop fighting with file ownership after cron jobs run as root

🧩 The Challenge

You’ve probably had a backup script or a log rotator run as root, leave a bunch of files behind, and suddenly your app can’t read its own data anymore. It’s an absolute headache to manually chown everything back just to get the service running again.

💡 The Fix

Use the –reference flag with chown to automatically grab the ownership settings from a good file and apply them to the new mess. It saves you from guessing who should actually own those files.

chown --reference=/var/www/html/data /var/www/html/data/backup-file.log

⚙️ Why It Works

Passing a reference file tells the utility to clone the user and group IDs directly from an existing template, which keeps your scripts idempotent. You don’t have to look up the UID or GID manually anymore.

🚀 Pro-Tip: Stick this in a post-exec block of your script and you’ll never touch chown manually again.

Linux Tips & Tricks | © ngelinux.com | 8/29/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted