Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to run long time process on Udev event
Udev is a device manager used by modern Linux systems to detect hardware changes and manage device nodes in the /dev directory. It provides a flexible way to run scripts or programs in response to device events, such as automounting drives, launching backup scripts, or running long-running processes. This article explains how to handle long-time processes triggered by Udev events.
Understanding Udev Rules
Udev rules are configuration files that specify how Udev should respond to device events. Each rule consists of match conditions that identify devices, actions to take when conditions are met, and optional attributes for fine-tuning behavior.
A basic Udev rule structure follows this pattern
# Match conditions, Action to take ACTION=="add", SUBSYSTEM=="block", KERNEL=="sda", RUN+="/usr/local/bin/myscript.sh"
This rule triggers when a block device named sda is added to the system, executing the specified script. The RUN directive runs the command asynchronously.
Running Long-Time Processes on Udev Events
When launching long-running processes from Udev events, you must ensure they don't block the Udev event processing. Here are three approaches
Background Process Execution
Use the & operator to run processes in the background
# /etc/udev/rules.d/11-longprocess.rules ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", RUN+="/usr/local/bin/longprocess.sh &"
This immediately returns control to Udev while the process continues running in the background.
Process with Wait
If you need Udev to wait for the process to complete
# /etc/udev/rules.d/12-waitprocess.rules ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", RUN+="/usr/local/bin/process.sh & wait"
The wait command ensures the Udev event doesn't complete until the background process finishes.
Using Systemd for Process Management
The recommended approach for long-running processes is to use systemd services, which provide better process management, logging, and restart capabilities.
Create a Systemd Service
# /etc/systemd/system/device-handler.service [Unit] Description=Device Handler Long Process After=multi-user.target [Service] Type=simple ExecStart=/usr/local/bin/device-handler.sh Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target
Trigger Service from Udev Rule
# /etc/udev/rules.d/13-systemd-trigger.rules ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", RUN+="/bin/systemctl start device-handler.service"
This approach provides automatic restarts on failure, proper logging through journald, and standardized service management.
Best Practices and Testing
Use udevadm to inspect device attributes and test rules
# View device attributes udevadm info -a -p /sys/class/block/sda # Test a rule without triggering actual events udevadm test /sys/class/block/sda # Monitor Udev events in real-time udevadm monitor
Always include descriptive comments in your rules for maintainability
# Launch backup process when external USB drive is connected
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd*", ATTRS{removable}=="1", RUN+="/usr/local/bin/backup.sh &"
Common Use Cases
| Scenario | Approach | Benefits |
|---|---|---|
| Quick scripts (<10s) | Direct RUN command | Simple, immediate execution |
| Long processes (>10s) | Background with & | Non-blocking, fast Udev response |
| Critical long processes | Systemd service | Auto-restart, logging, management |
Conclusion
Running long-time processes on Udev events requires careful consideration to avoid blocking device management. Use background execution for simple cases and systemd services for robust, manageable long-running processes. Proper testing with udevadm ensures reliable automation of hardware-triggered tasks in Linux systems.
