A cron job is a scheduled task that runs automatically on a Linux server at a specified time or interval.
Cron jobs are commonly used for:
-
Running PHP scripts
-
Scheduling Laravel commands
-
Performing application maintenance
-
Sending automated emails
-
Processing scheduled tasks
-
Running database backups
-
Executing system scripts
This guide explains how to create, manage, and troubleshoot cron jobs on an Ubuntu VPS server.
Requirements
Before creating a cron job, you will need:
-
An Ubuntu VPS server
-
SSH access to your server
-
A valid Linux user account
-
The script or command you want to execute
-
The correct path to the script or executable
Step 1: Connect to Your Ubuntu Server
Connect to your VPS using SSH.
Example:
ssh username@YOUR_SERVER_IP
Replace username with your Linux username and YOUR_SERVER_IP with your server's IP address.
If you are using Windows, you can connect through Windows Terminal, PowerShell, or an SSH client.
Step 2: Check Whether Cron Is Installed
Run the following command:
sudo systemctl status cron
If the cron service is installed and running, you should see an active status.
If it is not installed, install it using:
sudo apt update
sudo apt install cron -y
Enable and start the cron service:
sudo systemctl enable --now cron
Verify the service:
sudo systemctl status cron
Step 3: Open the Crontab Editor
To create a cron job for your current Linux user, run:
crontab -e
If prompted to select an editor, choose your preferred editor.
For example, you can select Nano.
This opens the user's cron configuration file.
Important: If your script needs to run as a specific application user, create the cron job under that user instead of using root unnecessarily.
Step 4: Configure the Cron Schedule
A cron job uses five fields to define when a command should run.
* * * * * command
The five fields represent:
| Field | Meaning |
|---|---|
| 1st | Minute |
| 2nd | Hour |
| 3rd | Day of month |
| 4th | Month |
| 5th | Day of week |
Common Cron Schedules
| Schedule | Cron expression |
|---|---|
| Every minute | * * * * * |
| Every 5 minutes | */5 * * * * |
| Every 15 minutes | */15 * * * * |
| Every 30 minutes | */30 * * * * |
| Every hour | 0 * * * * |
| Every day at midnight | 0 0 * * * |
| Every day at 2:00 AM | 0 2 * * * |
| Every Sunday at midnight | 0 0 * * 0 |
Use the schedule recommended by your application or script.
Step 5: Add a Cron Job
For example, if you want to execute a PHP script every five minutes:
*/5 * * * * /usr/bin/php /var/www/example.com/public_html/cron.php
Replace:
-
/usr/bin/phpwith the correct PHP executable path. -
/var/www/example.com/public_html/cron.phpwith the actual script path.
This is only an example. Your application may require a different command.
Example: Laravel Scheduler
For a Laravel application, the scheduler is commonly configured to run every minute:
* * * * * cd /var/www/example.com && /usr/bin/php artisan schedule:run >> /dev/null 2>&1
Replace /var/www/example.com with the actual Laravel project directory.
Example: CodeIgniter Application
For CodeIgniter, the correct command depends on the application and its scheduled tasks.
A CLI command may look like:
* * * * * cd /var/www/example.com && /usr/bin/php spark YOUR_COMMAND
Replace YOUR_COMMAND with the command supported by your application.
Do not use this example unless the application provides a valid Spark command.
Step 6: Save the Cron Job
If you are using Nano:
-
Press
CTRL + Oto save. -
Press
Enterto confirm. -
Press
CTRL + Xto exit.
The cron job will be saved automatically.
Step 7: Verify the Cron Job
To list the current user's cron jobs, run:
crontab -l
You should see the cron job you created.
To check the cron service:
sudo systemctl status cron
To view cron logs:
sudo grep CRON /var/log/syslog
You can also use:
sudo journalctl -u cron
Step 8: Remove or Edit a Cron Job
To edit existing cron jobs:
crontab -e
To remove all cron jobs for the current user:
crontab -r
Warning: The crontab -r command deletes all cron jobs for the current user. Use it carefully.
Troubleshooting
Cron job is not executing
Verify that:
-
The cron service is running.
-
The cron schedule is correct.
-
The script path is correct.
-
The command uses the correct PHP or executable path.
-
The user has permission to execute the script.
PHP script is not working
Check the PHP executable:
which php
Check the PHP version:
php -v
Your website may use a different PHP version than the command-line PHP version.
Script works manually but not through cron
Cron jobs run in a limited environment.
Use absolute paths and avoid relying on environment variables or the current working directory.
For example:
cd /var/www/example.com && /usr/bin/php script.php
Check cron logs
Run:
sudo grep CRON /var/log/syslog
If the script runs but produces an application error, check the application's own log files.
Important Notes
-
Use the correct Linux user for the application.
-
Use absolute paths in cron commands.
-
Avoid running scripts too frequently unless required.
-
Do not run application scripts as root unless necessary.
-
Ensure the script is compatible with the installed PHP version.
-
Always use the cron command recommended by your application developer.
Need Assistance?
If you need help configuring a cron job on your VPS, contact your hosting provider's support team.
Please provide:
-
The application or script name
-
The script location
-
The required execution frequency
-
The cron command, if available
-
Any error messages
Our support team will be happy to assist you.