Tools fcron and hcron Lead image: Lead Image © stillfx, 123RF.com
Lead Image © stillfx, 123RF.com
 

Cron alternatives fcron and hcron

Task Schedulers

The fcron and hcron scheduler alternatives to cron are suitable for computers that do not run around the clock, and each come with specific benefits. By Anzela Minosi

Most computers don't run nonstop, not least with a view to saving electricity. However, you might still want to run some tasks automatically when you are not sitting in front of your computer: In the long run, launching a backup program manually all the time can be annoying. The classic cron scheduler lets you handle these tasks automatically, but if your PC is not running at the specified time, automatic execution is simply skipped.

Therefore, it makes more sense to assign regular tasks to a scheduler that performs routine tasks as soon as the computer is switched back on. I tested the fcron [1] and hcron [2] schedulers on Fedora 29 with the Backup Manager [3] program as the test application.

Installation

You can pick up RPM and DEB packages for hcron on the project website and easily install these on your system with the package manager after downloading.

Fcron can only be installed manually (Listing 1) and requires the GCC compiler and Sendmail. The FcronQ [4] graphical front end uses the Qt GUI framework. Therefore, you have to set up the Qt library for Python 3, as well as Python 3 (Listing 2, first line). For FcronQ, you only need to download, unpack, and install the source files (Listing 2, following lines).

Listing 1: Installing fcron

# dnf install gcc sendmail
[...]
$ wget http://fcron.free.fr/archives/fcron-3.2.1.src.tar.gz
$ tar xvf fcron-3.2.1.src.tar.gz
$ cd fcron-3.2.1
$ ./configure
$ gmake
[...]
# gmake install

Listing 2: Installing FcronQ

# dnf install python3 python3-PyQt5 python3-PyQt4
[...]
$ wget https://downloads.sourceforge.net/project/fcronq/FcronQ/0.5.0/FcronQ-0.5.0.tar.bz2
$ tar xvf FcronQ-0.5.0.tar.bz2
$ cd FcronQ/Build/
$ make all
$ make install-user
[... set up for root ...]
# make install

Once you have set up one of the two schedulers, you should become root, enable the service, and launch the program in the background:

# systemctl enable fcron
# systemctl start fcron

In the case of hcron, just change the service name in the above commands from fcron to hcron.

fcron

In the /etc/fcron.conf configuration file, you can set the fcron file paths and the paths to some programs. If you want to edit the tasks in the future with your favorite editor, also adjust the editor variable. You can also specify who can and cannot use fcron with /etc/fcron.allow and /etc/fcron.deny. By default, all users have access.

At the command line, the

fcrontab -e

command creates a task. Fcron then opens a file in the configured editor in which you can edit, comment out, create, or delete tasks. The structure of tasks is

<options> <frequency> <command>

Table 1 summarizes important fcrontab file options and how to use them. If an option is preceded by an exclamation mark (!) on a command line, it applies to all subsequent lines or until the next !reset. A leading %, @, or &, on the other hand, indicates that it only takes effect within the line.

Tabelle 1: fcrontab Options

Option

Example

Function

bootrun(<bool>)

bootrun(true)

Runs a job after starting fcron or booting the system.

jitter(<seconds>)

jitter(10)

Delay job by up to 255 seconds if multiple jobs start at the same time.

mail(<bool>)

mail(true)

Inform users about the job result by email.

mailfrom(<email address>)

mailfrom(root)

Adds an address or username plus the domain to the From field.

mailto(<email address>)

mailto(root)

Adds an address or username plus the domain to the To field.

nice(<value>)

nice(5)

Job priority; varies from -20 (highest) to 19 (lowest).

nolog(<bool>)

nolog(true)

Write error messages only to the log.

noticenotrun(<bool>)

noticenotrun(true)

Notifies the user by email if a job has not been processed.

random(<bool>)

random(true)

Like jitter, but with a random number.

reset

reset

Reset option settings.

runas(<user>)

runas(root)

Process job with the rights of the specified user.

runatreboot(<Bool>)

runatreboot

Start job immediately after system boot.

runonce(<bool>)

runonce(true)

Do not reschedule the job once it has been executed.

stdout(<bool>)

stdout(true)

Output to the console if fcron is running in the foreground.

timezone(<timezone>)

timezone(Europe/Berlin)

Job specifications are valid for the specified time zone.

You define how often a command is processed by specifying a time in <Minute> <Hour> <Day> <Month> <Weekday> notation. The supported value ranges can be found in Table 2; the asterisk is used as a wildcard. To enter a command, simply type what you would normally type at the command line. Listing 3 shows some examples.

Tabelle 2: Time Under fcron

Field

Value

Minute

0-59

Hour

0-23

Day

1-31

Month

1-12

Weekday

0-7, where 0 = 7 = Sunday

Listing 3: fcron Examples

## Execute the echo command at 10:05am on Sundays and on the 31st.
5 10 31 * 7 echo '''
## Run mycommand every day at 5:05, 5:35, 6:05, 6:35, 7:05, and 7:35.
& 05,35 5-7 * * * mycommand -u me -o file
## Pick up email daily at 20, 21, 22 and 24 minutes past the hour.
20-24~23 * * * * getmail
## Pick up email every 30 minutes.
@ 30 getmails -all
## Run the Save command with low priority at 3:45 daily except Sundays
## and inform user 'jim' about it. Process job after startup
## if the PC was not running at the scheduled time.
&nice(10),mailto(jim),bootrun 45 03 * * *~0 "save --our work"

After saving the fcrontab file, you can use the command

fcrontab -l

to look up the tasks created by the current user.

FcronQ

Instead of using the command line, you can also configure fcron in the FcronQ graphical front end (Figure 1), which you call as FcronQ.pyw. To avoid an error message on launch, add your account with administrative rights to the fcron group:

# usermod -a -G fcron <user>
In principle, FcronQ is more like a text editor, because the parameters do not have any special input masks.
Figure 1: In principle, FcronQ is more like a text editor, because the parameters do not have any special input masks.

At bottom left in the GUI, you can see whether the fcron daemon is currently running. Next to that, select the user for whom you want to create new tasks. After a right-click on the input area, create a new task by selecting Append. In the Status column at the top, activate or deactivate individual tasks by checking or unchecking the boxes. You can also add a comment to the task.

You can double-click to add an option in the Variables or Options column. The Frequency column contains the time variables, and the Command column shows the command to be processed. To test a command, right-click the respective task and select Run. If everything works, save the task from the menubar with Action | Save.

hcron

Hcron uses key-value pairs (Table 3) for configuration, so you don't need to know where to enter minutes, hours, days, and so on in the call syntax. As with fcron, you can use asterisks for arbitrary information. In hcron, the individual tasks are referred to as events and can be stored in separate files to keep things tidy.

Tabelle 3: hcron Events

Key

Function (Values)

Mandatory Data

host

Hostname

command

Command to be executed

notify_email

Email address for notification

notify_message

Notification text

when_month

Execution: month (1-12)

when_day

Execution: day of the month (1-31)

when_hour

Execution: hour (0-23)

when_minute

Execution: minute (0-59)

when_dow

Execution: weekday (0-6, 0=Sunday)

Optional Data

as_user

User account for executing the command

failover_event

Alternative event in case of failure

next_event

Next event to execute after successfully starting an event

notify_subject

Email notification subject

when_year

Version: year (2000-2050)

The Scheduler configuration files can be found in the /etc/hcron/ folder. In the hcron.allow file, enter a username for anyone allowed to execute commands from an event file. You also need a directory for this user's events. It must be located in a subfolder that uses the fully qualified domain name (FQDN):

$ hcron-info --fqdn
[FQDN]

Next, create the corresponding events directory, and when you get there, use the hcron-event command to create an empty event file that already contains the most important keys:

$ mkdir -p ~/.hcron/<FQDN>/events
$ hcron-event ~/.hcron/<FQDN>/events/event-backup.txt
[... Fill event file ...]

In this example, the event file is named event-backup.txt.

To create an hcron event for Backup Manager that performs a daily backup run at 9:10pm, enter the keys and values shown in Listing 4 in the event-backup.txt file. After saving the file, hcron prompts you to restart the scheduler, which you can confirm by pressing Y. If you later edit an existing event file, you only need to restart the hcron scheduler with hcron-reload to notify hcron of the changes.

Listing 4: hcron Events

# event-backup.txt
as_user=<User>
host=<FQDN>
command=sudo backup-manager
notify_email=<User>@<FQDN>
notify_message=done
when_month=*
when_day=*
when_hour=21
when_minute=10
when_dow=*

Last but not least, use the calls

$ hcron-info allowed
yes
$ hcron-info es
accepted::/event-backup.txt

to check whether the current user is allowed to execute hcron events and, if so, where they reside.

Alternatives

The established vixie-cron scheduler and its fork cronie are used by default on several distributions. In contrast to fcron, neither cron nor vixie-cron resume operation if the computer was not running at the time of the scheduled event; however, another scheduler alternative named anacron does [5].

Cron also has graphical front ends, such as KCron [6], and you can even find websites where you can create a cronjob online [7]. The syntax of a standard job is the same for cron and fcron, so you can simply copy cronjobs to the corresponding fcrontab file, usually located in the /usr/var/spool/fcron/ directory. Additionally, the fcron syntax accommodates shortcuts from vixie-cron, so switching from vixie-cron to fcron is easy.

In addition to a comprehensive online tutorial [8] with several examples, fcron offers extended functions that are useful for load tests, among other things. A script can be executed several times a minute [9], which is only possible with the other tools by using the sleep command. Table 4 highlights further differences.

Tabelle 4: Scheduler Differences

fcron

cron/anacron

24/7 operation not required

Interrupted operation requires anacron

Demon

cron: demon; anacron: no demon

Tasks executable several times daily

cron: several times daily; anacron: daily only

Extensive tutorial

Several tutorials online

Manual installation

Installation by package manager

Only one GUI available

Multiple GUIs to choose from

Advanced syntax

Fewer functions

Conclusions

Fcron, like cron, is fairly clumsy to use, whereas hcron is easier to manage because it uses key-value pairs in an event file. Moreover, hcron creates a template with all obligatory keys on request: You only have to enter the corresponding values. Additionally, hcron is easier to install and provides better logging. Ultimately, you have to decide for yourself which scheduler best suits you, according to your specific deployment requirements.