Cron job
- Setting Up a Cron Job in cPanel
- Setting Up a Cron Job Via SSH
- Verifying Cron Job Configuration
- Common Cron Job Commands
Cron is a Linux utility that schedules a command or script on your server to run automatically at a specified time and date. A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.
Every modern application with decent features must have cron job configured in order tasks to be executed in the background.
Whatsmark requires a properly configured cron job, follow the steps explained below to configure cron job for your installation.
NOTE
For the examples below, make sure to replace /path/to/whatsmark/
with the path to your installation.
On some shared hosting you may need to specify the full path to the PHP executable (for example, /usr/local/bin/php82
or /opt/alt/php82/usr/bin/php
instead of php
)
TIP
Did you know that if you are using our hosted solution, the cron job is automatically configured for you during installation?
Setting Up a Cron Job in cPanel
- When using cPanel, to ensure that scheduled tasks in Whatsmark run automatically, you need to set up a Cron Job:
1. Log In to cPanel:
- Access your hosting account's cPanel.
2. Find Cron Job:
- Use the cPanel search feature and type in Cron Job to locate the Cron Jobs section.
3. Add New Cron Job:
- In the Cron Jobs section, find the option to add a new cron job.
- Enter the following command in the command field:
/path/to/whatsmark-saas/artisan schedule:run >> /dev/null 2>&1
- Replace
/path/to/whatsmark
with the actual path to your Whatsmark installation.
Setting Up a Cron Job Via SSH
For server management through SSH, a Cron Job needs to be configured from the command line. It's important to set up the cron job under the user account that handles the web server processes, commonly the www-data user.
1. Open Crontab for the Web Server User:
sudo crontab -u www-data -e
2. Add the Cron Job: In the crontab file, enter the following line:
/usr/bin/php /home/cijagani-whatsmark/htdocs/whatsmark.cijagani.in/whatsmark-non-saas/artisan schedule:run >> /dev/null 2>&1
3.Replace /path/to/whatsmark
with the actual path to your Whatsmark installation.
NOTE ON ROOT USER
Avoid using the root user for configuring the cron job. It should be set under the user account that handles web server processes to ensure proper functionality.
Verifying Cron Job Configuration
- Ensuring that the cron job is correctly set up is crucial for the smooth operation of Whatsmark.
- Access System Info:
- In your Whatsmark dashboard, go to Settings -> System -> System Info.
- Check Last Cron Run:
- Look for the Last Cron Run row.
- Confirmation of Proper Setup:
- If the Last Cron Run value resets every minute, your cron job is configured correctly for your Whatsmark installation
Common Cron Job Commands
Below you can find common cron job commands that are confirmed to work for commonly used hosting providers, in most cases the commands are confirmed by our customers and if your hosting provider is listed below, they should work in your environment as well.
WARNING
The examples below are using path /path/to/whatsmark/ as an example, make sure to replace this path with the path to your installation.
TIP
Is your hosting Whatsmark on a commonly used hosting provider and you can confirm that the cron job command works perfectly fine? Send us the command via our support area so we can add it here and other customers can benefit from it.
4. Queue Worker For Cron Job
Queue workers are essential for processing background tasks like WhatsApp message sending, webhook processing, and other asynchronous operations in WhatsMarkSaaS.
Why Queue Workers Matter:
- Handle high-volume message processing without blocking the main application
- Ensure reliable delivery with retry mechanisms
- Prevent timeouts during peak usage
- Provide better error handling and monitoring
Two Common Approaches:
- Basic Cron Job Approach (Recommended for most installations)
- Dedicated Queue Worker Process (For high-volume installations)
Basic Queue Worker Command
For most WhatsMarkSaaS installations, use this optimized queue worker command:
php artisan queue:work --queue=whatsapp-messages --stop-when-empty --sleep=3 --tries=3 --timeout=60 --backoff=5 --max-time=3600 --max-jobs=100
Advanced Queue Worker Setup
For high-volume installations, consider setting up dedicated queue workers using Supervisor:
# Install Supervisor (Ubuntu/Debian)
sudo apt-get install supervisor
# Create worker configuration
sudo nano /etc/supervisor/conf.d/whatsmark-worker.conf
Supervisor Configuration:
[program:whatsmark-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/whatsmark-saas/artisan queue:work --queue=whatsapp-messages --sleep=3 --tries=3 --max-time=3600 --max-jobs=1000
directory=/path/to/whatsmark-saas
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/log/whatsmark-worker.log
stopwaitsecs=3600
Start the Workers:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start whatsmark-worker:*
Parameter Explanations
Core Parameters:
- queue:work - Base command that starts a queue worker process
- --queue=whatsapp-messages - Process only WhatsApp-related jobs, ignoring other queues
Execution Control:
- --stop-when-empty - Exit when no jobs remain (prevents idle processes)
- --sleep=3 - Wait 3 seconds when no jobs available (reduces CPU usage)
- --max-time=3600 - Exit after 1 hour to prevent memory leaks
- --max-jobs=100 - Process max 100 jobs before restarting (memory management)
Job Handling:
- --tries=3 - Retry failed jobs up to 3 times before marking as failed
- --timeout=60 - Kill jobs that run longer than 60 seconds
- --backoff=5 - Wait 5 seconds before retrying failed jobs
Queue Monitoring
Check Queue Status:
# View pending jobs
php artisan queue:monitor
# Check failed jobs
php artisan queue:failed
# Retry failed jobs
php artisan queue:retry all
# Clear failed jobs
php artisan queue:flush
Monitor Worker Performance:
# View active workers
php artisan queue:work --help
Troubleshooting Queue Workers
Common Issues:
Jobs Not Processing
- Check if cron job is running:
crontab -l
- Verify queue connection in
.env
file - Check for failed jobs:
php artisan queue:failed
- Check if cron job is running:
Memory Issues
- Reduce
--max-jobs
parameter - Decrease
--max-time
for more frequent restarts - Monitor with
php artisan queue:monitor
- Reduce
High CPU Usage
- Increase
--sleep
parameter - Use
--stop-when-empty
for cron-based workers - Consider dedicated worker processes
- Increase
Performance Optimization:
# For high-volume processing
php artisan queue:work --queue=whatsapp-messages,notifications --sleep=1 --tries=3 --max-jobs=500 --max-time=1800
# For low-volume, resource-conscious
php artisan queue:work --queue=whatsapp-messages --stop-when-empty --sleep=5 --tries=2 --max-jobs=50 --max-time=1800