Archives

Setting up Cronjob

How To Configure a Cron Job (cPanel)

Do you find yourself wasting hours on tedious and repetitive server administration tasks? If you answered yes, you’re going to love this article, which explores how cron and cPanel & WHM help you automate these boring but essential aspects of web hosting.

Cron is a time-based scheduler. It allows server administrators to run scripts known as cron jobs automatically at scheduled intervals. cPanel & WHM runs several essential server maintenance scripts in this way, and you or your users can schedule scripts too. You might, for example, configure a custom backup script to run at 7 a.m. each morning.

We’re going to look at some common uses of a cron, including how to schedule scripts, and how to add new jobs to automate server admin tasks.

Typical Uses of a Cron Job

Cron can run simple commands, shell scripts, and programs written in languages such as PHP, Python, and Perl. If a task can be completed on the command line or in a script, you can automate it with the task scheduler.

Cron job use-cases include:

  • Log rotation — rotate old logs to an archive at pre-set intervals to stop them from growing to an unmanageable size.
  • Emailing users — alert users when they’re approaching resource limits.
  • Triggering updates — schedule checks for security updates and apply them automatically.
  • Content management system and database backups — ensure you never forget to complete essential backups.

Configuring a Cron Job in cPanel

Cron uses a compact notation to represent times and days. A typical configuration line might look like this:

30 23 25 * * myscript.sh

The format has five white-space separated fields for time intervals followed by a command.

The schedule is stored in a “crontab” file, a list of jobs with additional directives. You can edit the file over SSH with the crontab command, but using cPanel & WHM is faster, easier, including two interfaces for scheduling jobs: one for cPanel’s maintenance scripts and one for user scripts, which we’ll take a look at in the next section.

Before we get to cPanel’s cron configuration interface, you should have a firm grasp of the interval notation.

The time fields represent the following, from left to right:

  • Minutes — a number between 0 and 59.
  • Hours — a number between 0 and 23. Cron uses the 24-hour format, so 4 p.m is 16, 11 p.m. is 23, and so on.
  • Days of the month — a number between 1 and 31.
  • Months — a number between 1 and 12.
  • Days of the week — a number between 0 and 7, with Sunday represented as either 0 or 7.

Fields with an asterisk indicate that the job will run at every relevant interval compatible with the other fields.

Our first example specified the time interval “30 23 25 * *”.  This is the 30th minute of the 23rd hour of the 25th day of the month. Or, to put it another way, 11:30 in the evening on the 25th of each month. Because the Month and Day of Week fields contain asterisks, the script runs on days and months for which the other fields are valid; in this case, every day and month.

Let’s look at some more complex examples. You have a monitoring script that you would like to run every hour, on the hour, between 9 a.m and 5 p.m., but only on weekdays. To achieve this, we have to introduce another piece of notation, the range.

00  9-17   *   *   1-5   myscript.sh

Ranges are values separated by a hyphen. The example breaks down as:

  • 00 — the first (or, more accurately, the zeroeth) minute of the hour.
  • 9-17 — each hour between 9 a.m and 5 p.m.
  • * — every day.
  • * — every month.
  • 1-5 — the first to the fifth day, so Monday to Friday.

What if, instead of every hour between 9 a.m. and 5 p.m, you wanted the script to run at 9 a.m, 12 p.m., 3 p.m., and 6 p.m on weekdays. For intervals of this type, you need one more piece of notation, the list.

00  9,12,15,18  *  *  1-5 myscript

This example is the same as the previous one, but with a list in the Hours field instead of a range.

The basics we have outlined here should cover almost every situation, but cron has an incredibly flexible notation with additional shortcuts and directives. If you want to learn more, take a look at this article or the man page on your CentOS server with the command “man 5 crontab.”

Configuring cPanel Cron Jobs in WHM

cPanel & WHM schedules several scripts, including the upcp update script, backup, and cpbackup. Server administrators can adjust when these scripts run in WHM.

Select Configure cPanel Cron Jobs in Server Configuration in the sidebar menu.

00-WHM-configure-cPanel-cron-jobs

 

For each script, you will see five boxes to enter the notation we described above. We have selected sensible defaults, but you can change them to your preferred times.

Configuring Cron Jobs in cPanel

In cPanel, select Cron Jobs in the Advanced section of the Main Page menu.

02-cPanel-Cron-Job-Interface

To simplify configuration, we include a drop-down menu with common settings such as Once Per Week or Once Per Month.

03-cpanel-cron-job-common-settings

If you don’t find a setting that fits your needs in the main drop-down menu, enter a time interval in the boxes on the left or use the menus on the right to select from typical entries for each period.

Finally, enter the command or the path to your script in the Command field and hit the “Add New Cron Job” button to save. Cron will run the script automatically at the intervals you selected.

Cron is a simple but powerful tool for automating mundane, repetitive and easily forgotten server administration tasks. The interval format can be confusing at first, but a few minutes of study will save you many hours of work in the future, especially if you combine your knowledge with cPanel and WHM’s quick and easy configuration tools.

Configuring Amazon S3 for Cloud Storage

Before you start syncing files to S3, you need to setup con job on your server:

Please Go to Admin dashboard –> System Admin –> Tasks to get the command line that you need to setup cronjob (see screenshot). You can setup cron via cpanel or ssh. Contact your hosting if you don’t know how to do that. Normally, the command line will be “wget -qO- ‘https://domain.com/cron/task/run?key=3FSE@’ &> /dev/null . Send this command line to your hosting if you want to ask them to help you.

0711e61310dcbd53884091501fdcf7cd

Sync missing images, fonts, css and js to Amazon S3

If S3 is enabled and you find out some resources like CSS, font…are missing like the below screenshot. Please follow the below instructions to fix.

7e81c0e91d98a64c8483c4dee1583a6d

Go to admin dashboard –> System Admin –> Storage System –> Manage Storage Services. Click on “Synchronise webroot directory”, and you will see below popup.

Check Image, font, Cascading style sheets, Javascript then checking the folders under “Webroot” that you have not synced/transferred to S3 before.

For example, in the below picture you will transfer files (images, fonts, CSS and js) in “img” and “js” folder to S3

d7ed6bb137b3f6b30aa561db6b7a7018

Click OK to start Sync progress. You need to wait for a few minutes or may a few hours in case you have a lot of files that need to upload from selected folders to S3. The cronjob that you set up above will do this job so that you can close the web browser by clicking on “OK” button to sync.

Transfer data to S3: In the setting page you will see a button called “Transfer”, please click on this button if you enable S3 after the site has launched a long time ago to manually transfer all data such image, video… that uploaded by members before s3 is enabled to Amazon.

Clear caches: If you have any problems after s3 is enabled. Clear caches and then try again.

Mailing Template & SMTP Mail Settings

This article is about mooSocial’s Mailing template and how to configure the mailing system.

I/How to configure your mailing system:

e1f0e8a038b49991e7e96b1fe43dcc2d.jpg

1. Enter your From Name, ex: mooSocial

Enter your “From Address”, email address that you want the emails from the system to come from.

2. Check on Send Through SMTP server. (If you intended to use built-in mail, skip this step)

SMTP Host: enter your Host.

SMTP Username: enter your SMTP server username. Ex: http://mail.socialloft.com

SMTP Password: enter your SMTP server password.

SMTP Port: enter your SMTP server’s port. Ex: 25

3. Email Queue, 2 options, Yes to enable email queue or No to send emails immediately.

4. Choose between TLS & SSL or none of them.

5. Input numbers of emails you want to send each time Background Mail Task is running.

Then click on Save Changes button below to save all the settings.

II/ Manage Mailing Template:

You can configure Mails content that will send to users. There are many templates of emails for cases or roles.

7bf4203800d37f4f85b4464f9eed1fbb.jpg

#Mail templates manager layout.

Let’s take a look at the Templates list:

6bbeafcc77f18720771fcc2945a6db62.jpg

#Templates.

Now we take example on Welcome Mail:

First, select Welcome at Choose Email field to config, this email template is used for sending to new Members signed up.

8800231e0eb99da8c1aba9747b8451c7.jpg

#1: Select Language for the template.

#2: Emails Templates.

#3: Description of the template.

#4: Subject of the Email Template.

#5: Message to users.

#6 Save Changes button.

*In #5: Message to users[header] and [footer] are templates, they will display in your emails sent to users, you can config them by selecting Choose Emails field.