FreeBSD Cron Scheduling Process With Crontab Files

Crontab is a Unix command that creates a table or list of commands, each of which will be executed by the operating system (OS) at a time specified by the user and executed regularly according to script instructions created by the user. Crontab is used to create crontab files (lists) and then used to modify previously created crontab files.

Crontab, often called the cron table, is a file that contains a schedule of various cron entries that must be run at a predetermined time. Another way to describe crontab is as a utility that allows tasks to run automatically at regular intervals in the background by the cron daemon.

Cron is a standard utility of the FreeBSD operating system that allows task automation. This is a daemon, which is a process that usually starts at boot-up and runs in the background. The Cron daemon is a time-based scheduler that can run automatically. Meanwhile, Cron jobs are tasks carried out by Cron that occur during the boot-up or booting process, or during a time specified by the user.


1. Crontab and cron jobs

A cron job is a command that the cron daemon executes at regularly scheduled intervals. This is also known as a cron schedule, because it contains specific instructions about which commands to run and when they should start.

A user can submit a cron job by specifying the crontab command with the -e or options -e flag. The command calls an editing session so the user can create a crontab file and add entries for each cron job in the file. All entries must be in a form acceptable to the cron daemon.

Cron is usually used by system administrators to carry out periodic processes. Examples of processes that can be scheduled with Cron include:

  1. Back up important data and files.
  2. Updating software and packages.
  3. Disk cleaning and defragmentation.
  4. Send automatic emails on schedule.
  5. Sends email with IP address at boot-up
  6. Cleaning log files, etc
On FreeBSD systems, as on most other UNIX-type systems, scheduling is called cron. As a standalone daemon, Cron can run continuously and check its input files ( crontab ) every minute to see whether they have been modified or contain tasks that need to be running at that time.

One of the advantages of using Cron is that the cron process does not need to be restarted, the Cron file will automatically read changes every minute.


2. Crontab files

By default FreeBSD places the Crontab file in /etc/crontab. This file contains scheduled jobs run by the system, and a special /var/cron/tabs directory allows individual users to create their own crontab queue files.

If possible, it is better not to edit the /etc/crontab file, but to create a separate queue file. Each crontab file is placed in the /var/cron/tabs directory. The security mechanism will allow users to create and edit only their own files without interfering with other users' files. This mechanism is the crontab utility.

The crontab file contains entries for each cron job, with each entry separated from the next by a newline character. Additionally, each entry contains six fields/columns separated by spaces.
Look at the example of 6 columns from the crontab file below.

root@ns1:~ # ee /etc/crontab
#minute hour mday month wday who command

If we translate, the 6 fields of the crontab file are as follows.

root@ns1:~ # ee /etc/crontab
#menit jam haridalamsebulan bulan haridalamseminggu user perintah

How to fill in each column is as follows:
  1. Minutes: Range from 0 to 59.
  2. Hour: Range from 0 to 23.
  3. Day of the month: Range from 1 to 31.
  4. Months: Range from 1 to 12.
  5. Day of the week: Range from 0 to 6, where 0 = Sunday.
  6. who: root.
  7. Command: newsyslog.
Look at the example crontab file below.

root@ns1:~ # ee /etc/crontab
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD$
#
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
#
#minute hour mday month wday who command
#
# Save some entropy so that /dev/random can re-seed on boot.
*/11 * * * * operator /usr/libexec/save-entropy
#
# Rotate log files every hour, if necessary.
0 * * * * root newsyslog
#
# Perform daily/weekly/monthly maintenance.
1 3 * * * root periodic daily
15 4 * * 6 root periodic weekly
30 5 1 * * root periodic monthly
#
# Adjust the time zone if the CMOS clock keeps local time, as opposed to
# UTC time. See adjkerntz(8) for details.
1,31 0-5 * * * root adjkerntz -a


3. How to Use Crontab Files

To better understand the crontab command, here are some examples of writing /etc/crontab file commands.

a. Running the unbound program

59 12 * * * gunungrinjani /usr/local/bin/unbound

The script above explains that the Gunungrinjani user will run the unbound program every day at 12:59.

b. Run the redist-cli program

0 21 * * * root /usr/local/bin/redis-cli

The script above explains, the root user will run the redis-cli program every day at 21:00.

c. Restart the apache24 program

0 1 * * 2-7 root root /usr/local/etc/rc.d/apache24 restart

The description of the script above is that the root user will restart the apache24 program every Tuesday to Saturday at 01:00.

d. Turn off the apache24 program

30 07,09,13,15 * * * /usr/local/etc/rc.d/apache24 stop

The description of the script above is that it will turn off the apach24 program at 07:30, 09:30, 13:30 and 15:30.

e. Turn on the unbound program

*/10 * * * * /usr/local/etc/rc.d/unbound start

The script above will turn on the unbound program every 10 minutes.

In conclusion, Crontab can be a powerful tool for automating repetitive tasks, but it can also cause problems if not managed effectively. By following best practices for monitoring and managing cron jobs, developers can ensure their applications continue to run smoothly and efficiently.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post