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/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
- There are 2 common scheduler for queue worker
- You can apply following command for queue work
php artisan queue:work --queue=whatsapp-messages --stop-when-empty --sleep=3 --tries=3 --timeout=60 --backoff=5 --max-time=3600 --max-jobs=100
Core Parameters
- queue: The base command that starts a queue worker process to process jobs
- --queue=whatsapp-messages: Specifies which queue to process. Your application will only process jobs from the "whatsapp-messages" queue, ignoring jobs in other queues.
Execution Control Parameters
- --stop-when-empty: Instructs the worker to exit when there are no more jobs in the queue. Without this flag, the worker would run continuously, even when idle.
- --sleep=3: Sets the number of seconds to wait (sleep) when no jobs are available. This reduces CPU usage during idle periods.
- --max-time=3600: Limits the maximum runtime to 3600 seconds (1 hour). The worker will gracefully exit after this time, even if jobs remain. This prevents memory leaks and ensures workers don't run indefinitely.
- --max-jobs=100: Limits processing to 100 jobs per run. After processing 100 jobs, the worker will exit gracefully. This helps manage memory usage and ensures regular restarts.
Job Handling Parameters
- --tries=3: Sets the maximum number of attempts for a job before it's considered failed. If a job throws an exception, it will be retried up to 3 times.
- --timeout=60: Sets the maximum execution time for a single job to 60 seconds. If a job runs longer, it will be terminated and considered failed. This prevents stuck jobs from blocking the queue.
- --backoff=5: Sets the delay in seconds before retrying a failed job. Here, it will wait 5 seconds before the first retry. For multiple retry levels, you can use comma-separated values like --backoff=5,15,30