Get 50% Discount Offer 7 Days

NeuronVM

Contact Info

Chicago 12, Melborne City, USA

+88 01682648101

[email protected]

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
The Best Way to Configure Cron Jobs on Dedicated Server

After reading this post, you will learn how to configure cron jobs on a Dedicated server by using your shell user. This process includes entering the server through SSH to execute commands. Also, you can use these commands to edit an existing cron job that you created in the panel and continue editing it from the panel.

What is Cron Jobs?

Cron is a useful tool for users and can be used on any Unix-like operating system. The cron program is dedicated to scheduling commands at a specific time, and these commands and activities are known as cron jobs. This program is used to run monitor disk space, schedule backups and periodically deleted files that you don’t need anymore and etc.

Important Points Before Configuring Cron Jobs

There are some points you should be aware of before setting up. Crontab files are files that hold job lists and other instructions for cron daemons there. Each of the users is accessible by executing the commands here under the shell user:

crontab -e

Crontab files include simple text files that have a specific format. Every line of crontab files has five time and date fields which are followed by a command and then followed by a new line character (‘\n’). But you can’t add a newline so push enter/return at the end of your command.

Automatic word-wrap breaking up of the long line into multiple lines is another problem of Crontab, so make sure your text editor doesn’t do this.

Tip: In our example, the username is your shell user and the website is example.com.

How to Configure Cron Jobs on Dedicated Server

Here, we will show how to configure and use the Cron job on a dedicated server. We recommend you to visit the Cheap Dedicated Server plans offered by NeuronVM. By following our commands you can add a custom cron job in the user shell. These commands assume that you have not yet added a cron job to the panel, so the cron job file will be empty. Follow the instructions below.

First, You should enter your server using your shell user through ssh. Then after login, to open your crontab file you should use the following command:

crontab -e

It looks like you don't have a MAILTO line in your crontab file
For performance reasons we ask that you specify an address where
cronjob output will be delivered. If you do not wish to receive
cronjob output, simply press enter and cronjob output will not be
mailed to you.
For more information regarding this, please visit:
https://help.***.com/hc/en-us/articles/215088608
Where would you like cronjob output delivered? (leave blank to disable)
: [email protected]
cronjob output will be emailed to [email protected]
confirm? (y/N): y

Now you should add your email or leave it blank and after that, you should choose the proper text editor:

Select an editor. To change later, run ‘select-editor’.

1. /usr/bin/joe

2. /usr/bin/jstar

3. /usr/bin/jpico

4. /usr/bin/jmacs

5. /usr/bin/jed

6. /bin/nano <---- easiest

7. /usr/bin/vim.basic

8. /usr/bin/rjoe

9. /usr/bin/mcedit

10. /usr/bin/vim.tiny

11. /bin/elvis-tiny

12. /usr/bin/emacs25

13. /bin/ed
Choose 1-13 [3]: 6

As you see above, file number 6 is selected which is referred to as nano editor. Using a nano text editor is the easiest one.

Or you will see the following list:

Select an editor. To change later, run ‘select-editor’.

/bin/ed

/bin/nano <---- easiest

/usr/bin/emacs24

/usr/bin/vim.basic

/usr/bin/vim.tiny

Then you will have an empty crontab file. Now add the code to the cron job. In this example, we run a file named mail.php under the username of “username”. you should choose the name same as the one you logged in with, currently:

Custom cron job
MAILTO="[email protected]"
13 20 * * * php /home/username/mail.php

At last, save the file. you will see the final response below:

crontab: installing new crontab

The mission is completed now and your cron job should run every day at 8:13 pm.

Some Crontab Commands

For replacing your existing crontab with a custom crontab file:

crontab /home/username/filename

For editing your server’s crontab:

crontab -e

To view your crontab:

crontab -l

To remove your crontab:

crontab -r

An Explanation for The Date & Time Files

The five fields that you see at the beginning are the time and date fields which specify how much and when a command should be executed.

An Explanation for The Date & Time Files in cronjobs

We have another graphical way, as you can see here:

An Explanation for The Date & Time Files

You can use several ways of specifying multiple values in these fields:

_ The comma (,) specifies a list of values.

_ The dash (-) specifies a range of values and is equivalent to a comma (,)

_ The asterisk (*) or wildcard specifies all of the possible values for a field.

_ The slash (/) is used in conjunction with an asterisk, to skip a given number of values.

_ And at last, (/3) means you can skip to every third value, and in the hour field is equivalent to (0،3،6،9،12،15،18،21). you have to mention that (*) specifies every hour but /3 cause only 1,4,7 value are used.

One of these specific strings can be used in place of the date & time:

An Explanation for The Date & Time Files

Some Examples of Custom Cron Scripts

Here you can find some examples showing what to add to a new file in order to create a cron job:

1: This example runs a command at 5:10 pm PDT/PST, then will emails you the regular and error output to the destination specified by Mailto:

10 17 * * * perl /home/username/bin/yourscript.pl

2: This one runs a command at 1:00 am PDT/PST on Saturday and the only output is errors:

0 1 * * 6 sh /home/username/weekly/weekly-pruning.sh > /dev/null

3: This runs a PHP script which is calling cron.php at the top of every hour:

0 * * * * php /home/username/cron.php

Edit Dedicated Server

If you log in as a dedicated server admin user, you will be able to edit the crontab file directly. Follow the command below:

/var/spool/cron/crontabs/username

Then if you want to access that file, you will need to use sudo on your dedicated server.

Tip: If you are asking for sudo or admin access, you must upgrade to a dedicated server.

Here we have an example for opening the file with a vi text editor:

sudo vi /var/spool/cron/crontabs/username

Conclusion

Now you know how to configure and use cron jobs on a dedicated server. This tool is a useful assistant for you. It also makes the system recover automatically and work around the clock without human intervention. We hope you enjoy this article. If you have any suggestions, just leave a comment.

FAQ

What is the meaning of ***** in cron?

A cron schedule expression wildcard. It means your cron job should run every minute of every hour of every day of every month each day.

Rate this post
Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *