Search code examples
rundeck

Rundeck monitor directory


Does anyone know if Rundeck has the functionality to trigger a job upon the arrival of a file in a directory?

I've searched a bit, and it doesn't seem like I've found this feature, either in the enterprise version or in the community version.


Solution

  • That is an appropriate circumstance for the Waitfor Rundeck Nixy Plugin.

    You may install it by going to the gear icon > plugins > locate plugins > in the "search for..." area, type "nixy" and then install the "*nixy waitfor Steps" plugin.

    Now, in your workflow, you add a step to validate that, called "*nixy / waitfor / file-exist". You can provide the file path, the wait interval in seconds, and the maximum number of tries.

    Check this job definition example:

    - defaultTab: nodes
      description: ''
      executionEnabled: true
      id: 447af8f3-fc99-4bda-bc2d-aa91ee9c5233
      loglevel: INFO
      name: ProjectEXAMPLE
      nodeFilterEditable: false
      plugins:
        ExecutionLifecycle: null
      scheduleEnabled: true
      sequence:
        commands:
        - configuration:
            file: /home/user/Downloads/myfile
            interval: '60'
            maxtry: '2'
          nodeStep: true
          type: nixy-waitfor-file-exists
        keepgoing: false
        strategy: node-first
      uuid: 447af8f3-fc99-4bda-bc2d-aa91ee9c5233
    

    This job waits 60 seconds for the "myfile" file on the /home/user/Downloads/ directory, if the file isn't there, it tries twice.

    Alternatively, you can put a bash script logic in a script step job like this script:

    #!/bin/bash
    
    # Define the directory to monitor
    directory_to_monitor="/home/user/Downloads"
    
    # Function to monitor the directory for new files
    monitor_directory() {
        # Initial list of files in the directory
        initial_files=$(ls "$directory_to_monitor")
    
        while true; do
            # List files in the directory
            current_files=$(ls "$directory_to_monitor")
    
            # Check for new files
            new_files=$(comm -13 <(echo "$initial_files") <(echo "$current_files"))
    
            # If new files are detected, print a message and exit
            if [ -n "$new_files" ]; then
                echo "New file detected: $new_files"
                exit 0
            fi
    
            # Sleep for a while before checking again
            sleep 5
        done
    }
    
    # Call the function to monitor the directory
    monitor_directory
    

    Now, the script working in a Rundeck job (the directory path is defined in an Rundeck Option).

    - defaultTab: nodes
      description: ''
      executionEnabled: true
      id: 685e175b-6554-402e-a511-7a0af8420bd7
      loglevel: INFO
      name: Check Directory
      nodeFilterEditable: false
      options:
      - name: directory
        required: true
        value: /home/user/Downloads/
      plugins:
        ExecutionLifecycle: {}
      scheduleEnabled: true
      sequence:
        commands:
        - configuration:
            adhocLocalString: |-
              #!/bin/bash
    
              # Define the directory to monitor
              [email protected]@
    
              # Function to monitor the directory for new files
              monitor_directory() {
                  # Initial list of files in the directory
                  initial_files=$(ls "$directory_to_monitor")
    
                  while true; do
                      # List files in the directory
                      current_files=$(ls "$directory_to_monitor")
    
                      # Check for new files
                      new_files=$(comm -13 <(echo "$initial_files") <(echo "$current_files"))
    
                      # If new files are detected, print a message and exit
                      if [ -n "$new_files" ]; then
                          echo "New file detected: $new_files"
                          exit 0
                      fi
    
                      # Sleep for a while before checking again
                      sleep 5
                  done
              }
    
              # Call the function to monitor the directory
              monitor_directory
            interpreterArgsQuoted: 'false'
          nodeStep: true
          type: script-inline
        keepgoing: false
        strategy: node-first
      uuid: 685e175b-6554-402e-a511-7a0af8420bd7