SHELL TASK WRAPPER
What is a Cron Job and a Crontab File?
A Cron Job is a time-based task scheduler in Unix-like computer operating systems (such as Linux and macOS). It enables system administrators and developers to automate repetitive background tasks, such as executing database backups, sending daily notification emails, running script updates, or clearing temporary server caches, at specified intervals.
A Crontab (Cron Table) is a configuration file that contains the list of commands scheduled to run. Each line in a crontab represents a specific task, defined by a schedule expression followed by the command to execute.
Understanding the 5-Field Cron Schedule Syntax
A standard cron expression consists of five space-separated fields representing the time interval of execution. The syntax is formatted as follows:
* * * * * | | | | | | | | | +----- Day of Week (0 - 6) (Sunday to Saturday) | | | +------- Month (1 - 12) | | +--------- Day of Month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)
To write complex schedules, developers use special characters:
- Asterisk (
*): Matches all values for that field (e.g.*in the hour field means “every hour”). - Comma (
,): Defines a list of specific values (e.g.1,3,5in the weekday field means “Monday, Wednesday, and Friday”). - Hyphen (
-): Specifies a range of values (e.g.9-17in the hour field means “from 9 AM to 5 PM”). - Slash (
/): Denotes step values / intervals (e.g.*/15in the minute field means “every 15 minutes”).
Best Practices for Writing Cron Commands
To avoid common configuration mistakes that prevent cron tasks from running successfully, follow these best practices:
- Always Use Absolute Paths: The cron daemon runs commands inside a very limited environment shell that lacks standard user path definitions. Always specify the full absolute path to scripts and executables (e.g., use
/usr/bin/phpinstead ofphp, and/var/www/html/script.phpinstead ofscript.php). - Prevent Overlapping Executions: If a scheduled task takes longer to run than its execution frequency (e.g., a backup running every 5 minutes but taking 7 minutes to complete), multiple instances of the script will run simultaneously. Use the
flockcommand lock-wrapper generated by this tool to prevent overlapping runs. - Redirect Output Correctly: By default, cron attempts to email stdout/stderr logs to the system owner. Redirect logs to log files using output append directives (
>> /var/log/myjob.log 2>&1) or discard output entirely using> /dev/null 2>&1to keep system storage clean.
Frequently Asked Questions (FAQ)
Q: Why is my cron job not running?
A: This is usually caused by environment shell mismatches. The cron daemon runs in a restricted shell (often /bin/sh). If your script depends on standard user profile paths (like node/npm binaries or specific python packages), it will fail. Define explicit paths or wrap your scripts in a bash shell redirect block.
Q: What timezone do cron jobs run in?
A: Cron jobs run in the timezone of the server hosting the database/application (usually UTC). If your server is in a different timezone than your target audience, adjust your cron schedule hours accordingly.
Q: How do I view existing cron jobs on my Linux server?
A: Run the terminal command crontab -l to list all active cron schedules for your current user. To edit the file, execute crontab -e.
![]()