In this article, we will explain how to watch a directory for new file creation in Linux.
To execute a command upon new file creation in a Linux folder, employ inotify-tools and a shell script. This combination enables the monitoring of file system events.
Here’s a fundamental guide to setting it up –
- Install inotify-tools.
First, install inotify-tools, You can install it through your package manager. For example, on Ubuntu or Debian-based systems, execute the following command to install:$ sudo apt update $ sudo apt install inotify-tools
- How to create a Shell Script-
After that, make a script using inotifywait (from inotify-tools) to watch for new files in the directory and trigger an API call when a new file is found.
Here’s a basic example of how the script could be:#!/bin/bash # Directory to monitor MONITOR_DIR="/path/to/your/directory" # Your custom command # CUSTOM_COMMAND="curl -X POST -d @newfile http://your.api.endpoint" # Monitor for new files and call the API inotifywait -m -e create --format '%w%f' "$MONITOR_DIR" | while read NEWFILE do echo "New file detected: $NEWFILE" # Uncomment to execute custom command #eval $CUSTOM_COMMAND done
In this script, you need to replace /path/to/your/directory with the path to the folder that you want to monitor and http://your.api.endpoint with the API endpoint you want to call. You can also modify the CALL_API_COMMAND to include any necessary headers, data, or options for your specific API call.
- Run the following Script-
Note: Make the script executable and run it using the following steps:- Save the script to a file, for example, monitor.sh.
- Make it executable: chmod +x monitor.sh
- Run the script: ./monitor.sh
- Execute the script in the background.
If you wish for the script to run continuously in the background, you need to consider using nohup or running it within a screen or tmux session.
For example:nohup ./monitor.sh &
This ensures the script continues running even after you log out of the session.
- Run the following script as Systemd Service.
Follow these steps, to run the script as a systemd service on a Linux system.- Make sure your script is correct and tested. Save it in a fitting directory, like /usr/local/bin/. For instance, let’s say your script is named monitor.sh.
$ sudo mv monitor.sh /usr/local/bin/
Make sure it is executable:
$ sudo chmod +x /usr/local/bin/monitor.sh
- Craft a new systemd service file with your chosen text editor—consider using nano, as shown below:
$ sudo nano /etc/systemd/system/monitor.service
Add the following content to the file:
[Unit] Description=File Monitor Service [Service] ExecStart=/usr/local/bin/monitor.sh Restart=always User=nobody Group=nogroup [Install] WantedBy=multi-user.target
- Description: Provide a concise overview of your service.
- ExecStart: Specify the complete path to your script.
- Restart: Configure restart behavior always ensures restart regardless of the exit code.
- User and Group: Designate the user and group for the service, potentially creating a dedicated user for security reasons. Save and close the file.
- Refresh the configuration of the systemd manager:
sudo systemctl daemon-reload
- Turn on the service for automatic startup during boot:
$ sudo systemctl enable monitor.service
- Start the Service:
$ sudo systemctl start monitor.service
- Verify your service’s status to ensure it is running correctly.
$ sudo systemctl status monitor.service
- Make sure your script is correct and tested. Save it in a fitting directory, like /usr/local/bin/. For instance, let’s say your script is named monitor.sh.
Notes:
- If your script generates logs, ensure it writes to a location with appropriate permissions. Alternatively, utilise systemd’s logging capabilities for effective log handling.
- This script serves as a fundamental example. Depending on your specific needs, adjustments may be necessary.
- For advanced monitoring or complex processing, opt for a more robust solution or programming language. Ensure your script is self-contained and doesn’t rely on interactive input, as Systemd services aren’t intended for interactive use.
- If your script generates logs, ensure it writes to a location with appropriate permissions. Alternatively, utilise systemd’s logging capabilities for effective log handling.
In conclusion, monitoring a directory for new file creation in Linux enhances real-time awareness. By implementing the discussed methods, you can efficiently track changes and stay informed. Explore more insights in our comprehensive guide.