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?
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
- Solution through systemd timers
- Alternative methods for frequent tasks
- Verification and debugging of configuration
- Setting up permissions and security
- Comparison cron vs systemd timers
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:
[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:
[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
# 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:
#!/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
# 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:
# 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:
# 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:
-
Use a separate user: Create a special user for running cron tasks instead of using
root. -
Restrict rights: Set the minimum necessary rights for executable files.
-
Resource monitoring: Frequent tasks can load the system, use
htoportopto monitor the load. -
Logging: Always set up logging to track errors and performance.
-
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
- How to Execute Less Than 1 Minute Interval Jobs Using Cron Time-Based Scheduler
- systemd as a replacement for cron every 10 seconds
- How to schedule tasks with systemd timers in Linux
- Replacing cron with systemd-timers
- How To Run a Cron Job Every 10, 20, and 30 Seconds in Linux
- Use systemd timers instead of cronjobs
- 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.