Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SPOSTARBUST_Widget_Class has a deprecated constructor in /home2/cptech/public_html/wp-content/plugins/spostarbust/index.php on line 386

Deprecated: Function create_function() is deprecated in /home2/cptech/public_html/wp-content/plugins/spostarbust/index.php on line 611

Notice: The called constructor method for WP_Widget in SPOSTARBUST_Widget_Class is deprecated since version 4.3.0! Use
__construct()
instead. in /home2/cptech/public_html/wp-includes/functions.php on line 4514
How to Schedule Tasks Using Cron | A cPanel Tech
How to Schedule Tasks Using Cron

How to Schedule Tasks Using Cron

Task scheduler is quite necessary a tool in any operating system. We may need to schedule upgrades, or scripts to run at regular intervals so that our application will be functioning as expected. Linux comes with a default task scheduler called Cron.

Crontab

Cron, the default task scheduler in Linux, will help up to run tasks from once in a year to once in a minute. It is driven by a Crontab (Cron table) file, a configuration file that specifies commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the Cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) which only system administrators can edit.

Via Crontab, you can execute commands and run scripts in the background at regular intervals which ranges from every minute to a year.

View or edit existing Cron entry

To view or edit the existing Cron entry, you need shell access. So, login to the server as the user and execute this command.

crontab -l

This will open an editor where you can add a Cron.

crontab -e

The Cron entries will be of this format

* * * * * /execute/this/command

You can see there are 5 stars. The stars represent different date parts in the following order:

  • minute (from 0 to 59)
  • hour (from 0 to 23)
  • day of month (from 1 to 31)
  • month (from 1 to 12)
  • day of week (from 0 to 6) (0=Sunday)

Examples of task scheduling

If you want to run script every minute

* * * * * /execute/this/command

If you want to execute it every 5 minutes

*/5 * * * * /execute/this/command

If you want to execute Nth minute of every hour

N * * * * /execute/this/command
Once in 2 hours
N */2 * * * /execute/this/command

Manipulating the output

Sometimes we may need the output of the Cron to do further processing.

1. Store it some where

The following Cron saves the output to a log file

* */2 * * * /execute/this/command > /var/log/output.log
2. Mail it

By default, Cron saves the output in the user’s mailbox on the local system. But you can also configure Crontab to forward all output to a real email address by starting your crontab with the following line:

MAILTO="you@yourmail.com"

Or you can pipe the output to an email ID

* */2 * * * /execute/this/command | mail -s " Cron Output" you@yourmail.com
3. Trash it
* */2 * * * /execute/this/command > /dev/null

Just pipe all the output to the null device, also known as the black hole. On Unix-like operating systems, /dev/null is a special file that discards all data written to it.

 

Do you like this article? Was it helpful? Let us know; drop a line in the comments 🙂

Close Menu