How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Run a Cron Job as a Non-Root User
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Run a Cron Job as a Non-Root User
Server Management

How to Run a Cron Job as a Non-Root User

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Run a Linux cron job as a non-root user
SHARE

To run a cron job as a non-root user, edit that user’s own crontab — every job in a per-user crontab runs as the user who owns it, no sudo needed. The recipe is sudo crontab -u username -e (as root) or crontab -e (logged in as the user).

Contents
  • Method 1 — log in as the user
  • Method 2 — root edits another user’s crontab
  • Method 3 — system crontab in /etc/cron.d/
  • Don’t put sudo in the crontab line
  • Set the environment explicitly
  • Verify it ran
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on Ubuntu 22.04 and AlmaLinux 9. Originally published 2023-08-17, rewritten and updated 2026-05-17.

Method 1 — log in as the user

# SSH in as the target user, or
sudo -i -u username

# Then edit their crontab
crontab -e

Anything you add runs as that user automatically. The crontab file lives at /var/spool/cron/username (RHEL-family) or /var/spool/cron/crontabs/username (Debian-family).

Cron as non-root — crontab -e per user, crontab -u from root, /etc/cron.d with username field

Method 2 — root edits another user’s crontab

sudo crontab -u www-data -e

-u username tells crontab which user’s file to edit. Useful for system service accounts that don’t have an interactive shell (www-data, nobody, postgres).

Method 3 — system crontab in /etc/cron.d/

sudo tee /etc/cron.d/myapp <<'CRON'
PATH=/usr/local/bin:/usr/bin:/bin

* * * * * www-data /usr/bin/php /var/www/html/app/bin/legacy -C cron >> /var/log/myapp.log 2>&1
CRON

Files in /etc/cron.d/ follow the system-crontab format with a username field between the schedule and the command. Good fit for application-deployed cron jobs because the file lives with your app’s config, not buried in /var/spool/.

Don’t put sudo in the crontab line

You’ll occasionally see * * * * * sudo username command suggestions — skip those. sudo from a non-interactive cron job either prompts for a password (which can’t be entered) or requires a NOPASSWD sudoers entry. Both add friction for no benefit when the per-user crontab or /etc/cron.d/ already runs jobs as the desired user.

Set the environment explicitly

# Inside crontab -e
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
HOME=/home/myuser

* * * * * php /var/www/html/app/bin/legacy -C cron >> /var/log/myapp.log 2>&1

Cron’s environment is minimal — set PATH, HOME, and anything your job depends on at the top. Variables defined here apply to every job in the file below.

Verify it ran

# cron's own log
sudo grep CRON /var/log/syslog        # Ubuntu/Debian
sudo grep CROND /var/log/cron         # RHEL/Alma/Rocky

# List the user's crontab
sudo crontab -u username -l

Frequently asked questions

Where is each user’s crontab stored?

Per-user crontabs live under /var/spool/cron/ (CentOS/RHEL) or /var/spool/cron/crontabs/ (Debian/Ubuntu) — one file per user, named after the username. Edit them only via crontab -e, never by hand: the file is parsed and reloaded automatically when you save through the command, and direct edits can be lost.

What’s the difference between /etc/crontab and crontab -e?

/etc/crontab is the system crontab — root-owned, every line has a username field. crontab -e edits a per-user crontab where every job runs as that user automatically — no username field, no sudo. For one-off user-specific jobs, prefer the per-user crontab; reserve /etc/crontab and /etc/cron.d/ for system-wide tasks that need explicit user assignments.

Why doesn’t my cron job find binaries like php or node?

Cron runs with a minimal PATH (/usr/bin:/bin on most systems) and a near-empty environment. If your binary lives in /usr/local/bin or ~/.local/bin, give the full absolute path or set PATH= at the top of the crontab. Same for NODE_ENV, LANG, etc. — set them explicitly in the crontab, don’t rely on your login shell’s profile.

How do I capture cron job output instead of discarding it?

Replace >/dev/null 2>&1 with a real log file: * * * * * php /path/script.php >> /var/log/myapp.log 2>&1. >> appends so you keep history; 2>&1 sends stderr into the same file. Rotate it with logrotate to keep the file from growing forever.

Related guides

  • How to Stop Cron Job Output
  • How to Set Up a System-Based Cron Job for WordPress
  • How to Check the Linux OS Name and Version from the Command Line

References

man 5 crontab, man 1 crontab on any Linux. systemd timers (modern alternative): freedesktop.org/software/systemd/man/systemd.timer.html.

TAGGED:BashconfigurationcronLinux

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Reset the LiteSpeed WebAdmin Console password How to Reset the LiteSpeed WebAdmin Console Password
Next Article Run a Node.js application from a Windows .bat file How to Run a Node.js Application from a Windows .bat File
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
Server Management

How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth

13 Min Read
Fix Ubuntu terminal not opening on VirtualBox
OS

Fix: Terminal Won’t Open in Ubuntu on VirtualBox

4 Min Read
CyberPanel too many redirects Laravel error fixed by restarting LiteSpeed
Server Management

Fix CyberPanel Too Many Redirects (ERR_TOO_MANY_REDIRECTS) for Laravel Subdomain

7 Min Read
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
Server Management

How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)

9 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?