NeuroAgent

How to Set Up a Cron Task Every 10 Seconds in CentOS 9 Stream

Complete guide to setting up cron tasks every 10 seconds in CentOS 9 Stream using systemd timers instead of the deprecated SLEEP method.

Question

How do I configure a cron task to run every 10 seconds in CentOS 9 Stream? On my old CentOS 7 server, this was solved using SLEEP, but in CentOS 9 Stream, cron doesn’t execute and doesn’t write logs. The following commands work in the console:

          • sleep 0 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
          • sleep 10 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
          • sleep 20 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
          • sleep 30 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
          • sleep 40 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
          • sleep 50 ; /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1

How do I properly configure script execution every 10 seconds in CentOS 9 Stream?

NeuroAgent

In CentOS 9 Stream, the standard cron doesn’t support executing tasks more frequently than once per minute, so the method with multiple sleep entries that worked in CentOS 7 no longer functions. To solve this problem, you should use systemd timers - a modern and more reliable mechanism for frequent recurring tasks.

Table of Contents

Why the sleep method doesn’t work in CentOS 9 Stream

CentOS 9 Stream uses a more modern version of cron that has strict restrictions on the minimum execution interval for tasks. As noted in the documentation, standard cron cannot run more frequently than once per minute.

The problem with your approach is that:

  • Cron executes all tasks exactly at the beginning of each minute
  • When multiple tasks are in the same minute, they run sequentially, not in parallel
  • If one task takes longer than the interval between executions, it can cause conflicts

Unlike CentOS 7, where such workarounds might have partially worked, in CentOS 9 Stream cron has become more strict and refuses to execute incorrect configurations without visible errors in the logs.


Solution through systemd timers

Systemd timers are the optimal solution for tasks that need to run more frequently than once per minute. This method is more efficient and reliable than using a while loop with sleep.

Step 1: Creating a systemd service file

Create a service file at /etc/systemd/system/cron_events.service:

ini
[Unit]
Description=Bitrix cron events service
After=network.target

[Service]
Type=simple
User=bitrix
Group=bitrix
ExecStart=/usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php
WorkingDirectory=/home/bitrix/ext_www/site.com
Restart=always
RestartSec=10
StandardOutput=file:/home/bitrix/.bx_temp/site.com/log/cron_events.log
StandardError=file:/home/bitrix/.bx_temp/site.com/log/cron_events.log

Step 2: Creating a systemd timer file

Create the corresponding timer at /etc/systemd/system/cron_events.timer:

ini
[Unit]
Description=Run cron events every 10 seconds
Requires=cron_events.service

[Timer]
OnUnitActiveSec=10s
OnBootSec=10s
AccuracySec=1ms

[Install]
WantedBy=timers.target

Step 3: Activation and startup

bash
# Reload systemd
systemctl daemon-reload

# Enable the timer
systemctl enable cron_events.timer

# Start the timer
systemctl start cron_events.timer

# Check status
systemctl status cron_events.timer

As noted in linuxconfig.org, the OnUnitActiveSec=10 directive in the timer unit guarantees that the task will run every 10 seconds.


Alternative methods for frequent tasks

Method 1: Script with while loop

If for some reason systemd is not suitable, you can create a separate script with an infinite loop:

bash
#!/bin/bash
# /usr/local/bin/cron_events_10sec.sh

while true; do
    /usr/bin/php -f /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php >> /home/bitrix/.bx_temp/site.com/log/cron_events.log 2>&1
    sleep 10
done

Then add to crontab to run this script every minute:

* * * * * /usr/local/bin/cron_events_10sec.sh

Method 2: Using anacron

For tasks that don’t require absolute precision, you can consider using anacron with frequent execution settings.

Method 3: Multiple cron jobs with different offsets

As mentioned in tecmint.com, you can create multiple cron jobs with different offsets, but this method is less reliable for intervals of less than a minute.


Verification and debugging of configuration

Checking systemd timer status

bash
# View timer status
systemctl status cron_events.timer

# View service logs
journalctl -u cron_events.service -f

# View all active timers
systemctl list-timers --all

Checking permissions

Make sure the bitrix user has the necessary rights:

bash
# Check execution rights for PHP script
ls -la /home/bitrix/ext_www/site.com/bitrix/php_interface/cron_events.php

# Check write permissions for log file
ls -la /home/bitrix/.bx_temp/site.com/log/

Test execution

For testing, you can temporarily reduce the interval:

bash
# Temporarily change interval to 30 seconds for testing
sed -i 's/OnUnitActiveSec=10s/OnUnitActiveSec=30s/' /etc/systemd/system/cron_events.timer
systemctl daemon-reload
systemctl restart cron_events.timer

Setting up permissions and security

When setting up frequent tasks, it’s important to consider security:

  1. Use a separate user: Create a special user for running cron tasks instead of using root.

  2. Restrict rights: Set the minimum necessary rights for executable files.

  3. Resource monitoring: Frequent tasks can load the system, use htop or top to monitor the load.

  4. Logging: Always set up logging to track errors and performance.

  5. Execution time limits: Set reasonable timeouts for tasks that may run for a long time.


Comparison cron vs systemd timers

Characteristic Cron Systemd Timers
Minimum interval 1 minute 1 millisecond
Precision Low High
Process management Basic Advanced
Dependencies Limited Flexible
Logging Basic Integrated with journald
Restart on failure No Yes
Resource efficiency Low for frequent tasks High

As experts from anteru.net note, systemd timers significantly outperform traditional cron in all aspects, especially for frequent and critical tasks.

Sources

  1. How to Execute Less Than 1 Minute Interval Jobs Using Cron Time-Based Scheduler
  2. systemd as a replacement for cron every 10 seconds
  3. How to schedule tasks with systemd timers in Linux
  4. Replacing cron with systemd-timers
  5. How To Run a Cron Job Every 10, 20, and 30 Seconds in Linux
  6. Use systemd timers instead of cronjobs
  7. Can I run a cron job more frequently than every minute?

Conclusion

For setting up script execution every 10 seconds in CentOS 9 Stream, it is recommended to use systemd timers instead of traditional cron. This approach:

  • Provides high timing accuracy (down to milliseconds)
  • Ensures reliable restart on failures
  • Integrates with the journald logging system
  • Is more efficient in terms of resource usage
  • Allows flexible management of dependencies and access rights

To solve your specific task with Bitrix cron_events.php, create systemd service and timer files as described above and activate them. This will ensure reliable and predictable operation of your script every 10 seconds without the problems associated with cron limitations.

If you need to run several different tasks with different intervals, create separate service and timer files for each of them.