Cron Job - Knowledge Base Archives - Hivelocity Hosting https://www.hivelocity.net/kb/tag/cron-job/ Dedicated Servers, Private Cloud & Colocation Fri, 05 Jan 2024 19:27:09 +0000 en-US hourly 1 https://wordpress.org/?v=6.6 How to Schedule System Updates and Tasks Using Cron Jobs https://www.hivelocity.net/kb/how-to-schedule-system-updates-and-tasks-using-cron-jobs/ Mon, 17 Jul 2023 16:31:11 +0000 https://www.hivelocity.net/?post_type=hv_knowledgebase&p=27673 Updating a linux server is a good practice that can benefit your server’s security, functionality, and flexibility. This is important for several reasons: First, updating a linux server ensures that the server has the latest security patches and bug fixes, which can prevent potential attacks or errors from compromising the server’s performance or data. Second, updating …

How to Schedule System Updates and Tasks Using Cron Jobs Read More »

The post How to Schedule System Updates and Tasks Using Cron Jobs appeared first on Hivelocity Hosting.

]]>
Updating a linux server is a good practice that can benefit your server’s security, functionality, and flexibility. This is important for several reasons:

  1. First, updating a linux server ensures that the server has the latest security patches and bug fixes, which can prevent potential attacks or errors from compromising the server’s performance or data.
  2. Second, updating a linux server can also improve the server’s functionality and compatibility with new software or hardware, which can enhance the server’s efficiency and usability.
  3. Third, updating a linux server can also provide access to new features or options that can make the server more customizable and adaptable to different needs or preferences.

But what’s the best way to ensure that your server remains up-to-date?

Cron jobs are tasks scheduled to run periodically on a computer system. They are often used for performing maintenance, backups, or other routine operations. Cron jobs are defined by a file called crontab, which specifies the commands to execute and the time intervals to do so. The crontab file can be edited using the crontab command or by using a text editor, and consists of six fields: minute, hour, day of month, month, day of week, and command.

A Cron job’s Structure

A cron job consists of two parts: a schedule and a command. The schedule defines when and how often the task should run, using a special format of minute, hour, day of month, month, day of week, and optional year. The command defines what action should be performed by the task, such as executing a script or sending an email. For example, a cronjob that runs every Monday at 10:00 am and prints “Hello World” to the standard output would look like this:

0 10 * * 1 echo “Hello World”

The structure of each section of the above line can be found in the table below.

* * * * * Command
Minute (0-59) Hour (0-23) Day of the month (1-31)

Month of the year (1-12)

Day of the week (0-6) Command to be executed

Viewing All Cron jobs

Using the command below, one can view all existing Cron jobs. The command display jobs for the current user you are logged in as, which in this example is root.

crontab -l

Screenshot showing the result of the crontab -l
 command.

To view other user Cron jobs, use the command crontab -u USERNAME -l while ensuring to change the “USERNAME” part with the actual username.

Editing and Adding Cron jobs 

To add and schedule a cron job, use the command crontab -e to open the editor and begin entering a task.

Depending on your editor, the file will open for editing as shown below.

Screenshot showing the crontab editor.

Now you can enter a new entry. For example, to have the system check for updates and update all available packages automatically every Wednesday at 2AM, then the following command would be used.

* 2 * * 3 sudo yum update -y > /dev/null 2>&1

screenshot showing the result of the command: * 2 * * 3 sudo yum update -y > /dev/null 2>&1
.

Redirecting the Scheduled Task’s Output to a File

You can enter and schedule most commands and tasks including sending the output to a file using > to overwrite a file each time or append to it using >> . For example,

* 2 * * 3 sudo yum update -y > /home/hivelocity/update_status.txt – this command will send the output to the file we listed while overwriting the file each time the task runs.

* 2 * * 3 sudo yum update -y >> /home/hivelocity/update_status.txt – this command will send the output to the file we listed while adding the output to the existing file each time the task runs, which will have a file with all the outputs from every instance that the file ran.

Redirecting the Scheduled Task’s Output to an Email Address

To have the output of all the commands being run in the crontab file sent to an email address, ensure that at the top of the file the following line is added.

MAILTO=”your@emailaddress.com”

Note, make sure to change “your@emailaddress.com” to your email address within the double quotes, for example “pascal@hivelocity.net”.

Common cron job time structures

The listed items below can assist with the most common times used to run various tasks.

  • * * * * * – This runs the command every minute.
  • 0 * * * * – This runs the command every hour.
  • 0 0 * * * – This runs the command every day at midnight.
  • 0 0 * * 0 – This runs the command every Sunday at midnight.
  • 0 0 1 * * – This runs the command on the first day of every month at midnight.

As well as the following,

  • @reboot – Run once, at startup.
  • @yearly – Run once a year
  • @annually – (same as @yearly)
  • @monthly – Run once a month
  • @weekly – Run once a week
  • @daily – Run once a day
  • @midnight – (same as @daily)
  • @hourly – Run once an hour

Useful Resources

Creating a task can be complicated at first, so to make things easier, there are multiple sources online that will generate crontab lines based on your needs.

Two common generators are listed below.

 

– written by Pascal Suissa

The post How to Schedule System Updates and Tasks Using Cron Jobs appeared first on Hivelocity Hosting.

]]>
What is Cron Job? https://www.hivelocity.net/kb/what-is-cron-job/ Wed, 23 May 2012 18:42:54 +0000 https://kb.hivelocity.net/?p=10340 A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This is normally used to schedule a job that is executed periodically – for example, to send out a notice every morning. Some scripts, such as Drupal and WHMCS may require you to set up cron jobs …

What is Cron Job? Read More »

The post What is Cron Job? appeared first on Hivelocity Hosting.

]]>
A cron job is a Linux command used for scheduling tasks to be executed sometime in the future. This is normally used to schedule a job that is executed periodically – for example, to send out a notice every morning. Some scripts, such as Drupal and WHMCS may require you to set up cron jobs to perform certain functions.

For most cron jobs, there are three components present:

  1. The script that is to be called or executed.
  2. The command that executes the script on a reoccurring basis. This is typically set in cPanel.
  3. The action or output of the script, which depends on what the script being called does. Frequently, scripts called as cron jobs modify files or databases. However, they can perform other tasks that do not modify data on the server as well, such as sending out email notifications.

Most scripts that require the use of a cron job will give you specific instructions on what needs to be setup, frequently giving examples.

 

Using cPanel to Set a Cron Job

To add a cron job in cPanel, follow these steps:

  1. First, you’ll need to log in to cPanel
  2. From the dashboard, either search for Cron Job or navigate to the Advanced menu and click on the icon for Cron Jobs

    cPanel Advanced tab showing the Cron Jobs icon

  3. Next, you’ll need to select an email where you’d like to receive cron output. If the email address listed beside Current Email is valid, simply leave it. If you’d like to use a different email address, enter it into the form field next to Email and click Update Email once you’ve finished

    Window showing current email address as well as the option to update to a different email address for cron job output

  4. In the section labelled Add New Cron Job, you can specify what command you’d like to run as well as its frequency. Start by entering your desired command in the bottom form field labelled Command. You can list any Linux command here or, if you have a file you’d like to run at a certain time, simply provide the path to the file you’d like to run

    Add New Cron Job window highlighting the "Command" form field

  5. Next, move back up to the form field labelled Common Settings and select which frequency you’d like the command to run at from the available options listed in the dropdown menu. This will automatically update the other time settings according to your choice

    Add New Cron Job window highlighting the "Common Settings" for recurring commands

  6. Alternatively, you can choose to enter your recurring frequency manually by adjusting the individual time settings. Minute, Hour, Day, Month, and Weekday can all be modified to achieve your intended results

    Add New Cron Job window highlighting the individual time settings for recurring commands

  7. When you’ve finished making your selections, simply click the Add New Cron Job button to finalize the process

    Window highlighting the "Add New Cron Job" button

And now you’ve successfully scheduled a command using the Cron Jobs tool within cPanel. If you have other commands that need to be set, simply repeat this process for any other commands.

Not a cPanel user? No problem! Read on to learn how to schedule cron jobs using the Plesk control panel as well.

 

Using Plesk to Set a Cron Job

to add a cron job using Plesk, just follow these steps:

  1. First, log in to your Plesk control panel
  2. Once you’re logged in, on the left side of the dashboard under Server Management, click the option for Tools & Settings

    Plesk control panel dashboard highlighting "Server Management" and the "Tools & Settings" option
  3. Now, on the right side panel, under Tools & Resources, click on Scheduled Tasks

    Tools & Resources options highlighting "Scheduled Tasks"
  4. On the next screen, click on Plesk Administrator User

    System User tab with the "Plesk Administrator" option highlighted
  5. On the Scheduled Tasks screen, under Tools, click on Add New Task

    Scheduled Tasks window highlight the option to "Add New Task"
  6. On the following screen, provide all requested information as shown in the image below

    Task information window showing a sample description, scheduler notification, file path, and task priority

  7. Now, scroll down a bit and in the section beneath Task priority, you’ll see a calendar. Here, you can select the frequency with which your command runs. For this example, we’ve selected Every day, Every month, and under Days of the week we’ve also selected Every day

    Sample calendar showing highlighting the option to schedule every day of every month
  8. If you want to schedule a different frequency with which your command will run, simply select the corresponding check boxes. For instance, if you wanted a scheduled task to run every minute of every hour, or every day, you’re calendar would look like this:

    Sample calendar window highlighting the option to schedule for every minute of every hour of every day

  9. Once you’ve finished scheduling your task per your requirements, simply click OK to finalize your selections

And now you’ve successfully schedule a cron job using the scheduled tasks tool within Plesk.

Now that you know how to schedule tasks using both cPanel and Plesk, you should be ready to start scheduling cron jobs as needed. Just remember, if you encounter any problems, feel free to issue a support ticket and our support team will be happy to help.

 

Popular Links

Looking for more information on Cron Jobs? Search our Knowledge Base!

Interested in more articles about Web Hosting? Navigate to our Categories page using the bar on the left or check out these popular articles:

Popular tags within this category include: DNS, FTP, IIS, MX Records, and more.

Don’t see what you’re looking for? Use the search bar at the top to search our entire Knowledge Base.

 

The Hivelocity Difference

Seeking a better Dedicated Server solution? In the market for Private Cloud or Colocation services? Check out Hivelocity’s extensive list of products for great deals and offers.

With best-in-class customer service, affordable pricing, a wide-range of fully-customizable options, and a network like no other, Hivelocity is the hosting solution you’ve been waiting for.

Unsure which of our services is best for your particular needs? Call or live chat with one of our sales agents today and see the difference Hivelocity can make for you.

The post What is Cron Job? appeared first on Hivelocity Hosting.

]]>
Make diskuage, bandwidth and webalizer stats update more frequently https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently-2/ https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently-2/#respond Sun, 14 Nov 2010 09:43:48 +0000 https://kb.hivelocity.net/?p=2286 Cron is the tool used to control the update frequency of the stats. You can easily change the frequency by modifying the cron job. Cron file for RedHat: /etc/cron.d/directadmin_cron Cron file for FreeBSD: /etc/crontab Edit the cron file and locate the line that looks like the following: 10 0 * * * root echo ‘action=tally&value=all’ …

Make diskuage, bandwidth and webalizer stats update more frequently Read More »

The post Make diskuage, bandwidth and webalizer stats update more frequently appeared first on Hivelocity Hosting.

]]>
Cron is the tool used to control the update frequency of the stats. You can easily change the frequency by modifying the cron job.

Cron file for RedHat: /etc/cron.d/directadmin_cron
Cron file for FreeBSD: /etc/crontab

Edit the cron file and locate the line that looks like the following:

10 0 * * * root echo ‘action=tally&value=all’ >> /usr/local/directadmin/data/task.queue
That means that the tally will be run 10 minutes after midnight every day.

If you’d like to (for example) run the tally every 4 hours, you’d change it to:

10 */4 * * * root echo ‘action=tally&value=all’ >> /usr/local/directadmin/data/task.queue

One important thing to remember is that for very large servers, with upwards of 1000 domains, the tally can take a significantly long time to run (up to 2 hours), so if you run a large server, you may not want to run the tally too often.

Once you’ve set the value you want, save the file and reload the cron daemon by runnin the command:

killall -HUP crond

The post Make diskuage, bandwidth and webalizer stats update more frequently appeared first on Hivelocity Hosting.

]]>
https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently-2/feed/ 0
How do I schedule crontab tasks? https://www.hivelocity.net/kb/how-do-i-schedule-crontab-tasks/ https://www.hivelocity.net/kb/how-do-i-schedule-crontab-tasks/#respond Sat, 13 Nov 2010 18:34:32 +0000 https://kb.hivelocity.net/?p=1663 To access the crontab management functions, click the Crontab Manager icon on the Domain administration page. The Crontab management page will open: On this page, you can view scheduled tasks of various system users, set the e-mail address for the crontab messages to be sent, schedule new tasks and remove them. The Show Crontab of: …

How do I schedule crontab tasks? Read More »

The post How do I schedule crontab tasks? appeared first on Hivelocity Hosting.

]]>
To access the crontab management functions, click the Crontab Manager icon on the Domain administration page. The Crontab management page will open:

On this page, you can view scheduled tasks of various system users, set the e-mail address for the crontab messages to be sent, schedule new tasks and remove them.

The Show Crontab of: drop-down box indicates the system user, whose scheduled tasks are currently displayed. It also allows to select another system user to view and/or manage scheduled tasks that belong to that user.
Each line in the Crontab task list represents a single task. The Status (S) column shows whether the selected task is enabled or disabled (the disabled tasks are not executed). The Command column contains the command that is executed within the selected task and serves as a link to the page that allows editing the selected scheduled task properties.

The task list can be sorted by its parameters in ascending or descending order. To sort the task list, click on the name of the sor

The post How do I schedule crontab tasks? appeared first on Hivelocity Hosting.

]]>
https://www.hivelocity.net/kb/how-do-i-schedule-crontab-tasks/feed/ 0
Why is the task.queue not being processed? https://www.hivelocity.net/kb/why-is-the-task-queue-not-being-processed/ https://www.hivelocity.net/kb/why-is-the-task-queue-not-being-processed/#respond Sat, 13 Nov 2010 14:18:06 +0000 https://kb.hivelocity.net/?p=1606 check for /usr/local/directadmin/data/task.queue It should be deleted every minute. If it exists, check it’s contents. If there are more than just 1 or 2 lines, then the dataskq is not running. check /var/log/cron. See if the dataskq is running every minute (logged every minute). If not, try running chmod 644 /etc/cron.d/directadmin_cron /sbin/service crond restart make …

Why is the task.queue not being processed? Read More »

The post Why is the task.queue not being processed? appeared first on Hivelocity Hosting.

]]>
check for /usr/local/directadmin/data/task.queue
It should be deleted every minute. If it exists, check it’s contents. If there are more than just 1 or 2 lines, then the dataskq is not running.

check /var/log/cron. See if the dataskq is running every minute (logged every minute). If not, try running

chmod 644 /etc/cron.d/directadmin_cron
/sbin/service crond restart

make sure crond is running :
ps ax | grep cron

try restarting cron:
/sbin/service crond restart
Check /var/log/cron and /var/log/messages for possible problems with the /etc/cron.d/directadmin_cron file (/etc/crontab on FreeBSD).

If all of the above checkout but the task.queue is still not being processed, try running it manually:

/usr/local/directadmin/dataskq d

The post Why is the task.queue not being processed? appeared first on Hivelocity Hosting.

]]>
https://www.hivelocity.net/kb/why-is-the-task-queue-not-being-processed/feed/ 0
How do I make diskuage, and webalizer stats update more? https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently/ https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently/#respond Sat, 13 Nov 2010 14:16:18 +0000 https://kb.hivelocity.net/?p=1602 Cron is the tool used to control the update frequency of the stats. You can easily change the frequency by modifying the cron job. Cron file for RedHat: /etc/cron.d/directadmin_cron Cron file for FreeBSD: /etc/crontab Edit the cron file and locate the line that looks like the following: 10 0 * * * root echo 'action=tally&value=all' …

How do I make diskuage, and webalizer stats update more? Read More »

The post How do I make diskuage, and webalizer stats update more? appeared first on Hivelocity Hosting.

]]>
Cron is the tool used to control the update frequency of the stats. You can easily change the frequency by modifying the cron job.

Cron file for RedHat: /etc/cron.d/directadmin_cron
Cron file for FreeBSD: /etc/crontab

Edit the cron file and locate the line that looks like the following:

10 0 * * * root echo 'action=tally&value=all' >> /usr/local/directadmin/data/task.queue
That means that the tally will be run 10 minutes after midnight every day.

If you’d like to (for example) run the tally every 4 hours, you’d change it to:

10 */4 * * * root echo 'action=tally&value=all' >> /usr/local/directadmin/data/task.queue

One important thing to remember is that for very large servers, with upwards of 1000 domains, the tally can take a significantly long time to run (up to 2 hours), so if you run a large server, you may not want to run the tally too often.

Once you’ve set the value you want, save the file and reload the cron daemon by runnin the command:

killall -HUP crond

The post How do I make diskuage, and webalizer stats update more? appeared first on Hivelocity Hosting.

]]>
https://www.hivelocity.net/kb/how-do-i-make-diskuage-bandwidth-and-webalizer-stats-update-more-frequently/feed/ 0