Search code examples
linuxprocessprogress

How to send process progress information from a long running linux command


I am planning to write a linux command which is going to process (with a custom logic) a very big file. Once this command is run, it will take hours to finish the task.

What is the best way to give eta output?

What about the idea of writing a 100 byte progress status file? In specific stages I can write bytes to the file. Say at 40% I can write 40 bytes to the file. Once the file size reaches 100 bytes size it means the process is finished. So to monitor the progress we have to check only the size of this file.

Is there any general way of handling such progress information? I don't want to include too much logic in my program for displaying the progress information. I am looking for a simple solution.

Any suggestions?


Solution

  • The normal way would be to set up a signal handler like dd does, and then you send the process a SIGUSR1 or somesuch using the kill command and it catches the signal and outputs a status message. Try

    man dd
    

    So, on my iMac, for example, dd uses SIGINFO, so

    # Move a whole load of nothing to nowhere in the background
    dd if=/dev/zero of=/dev/null &
    [1] 11232
    
    # No output, till I want some a few seconds later
    
    kill -SIGINFO 11232
    12875835+0 records in
    12875834+0 records out
    6592427520 bytes transferred in 9.380158 secs (702805581 bytes/sec)
    
    
    
    # No more output, till I want some a few more seconds later
    
    kill -SIGINFO 11232
    19163866+0 records in
    19163865+0 records out
    9811898880 bytes transferred in 14.015095 secs (700095068 bytes/sec)
    

    A signal handler is pretty easy to set up, even in a shell script. For example:

    #!/bin/bash
    
    trap "echo Progress report..." SIGINT
    echo "My process id is $$"
    
    while : ; do
        sleep 10    # Do something - admittedly little :-)
    done