Visit Our UNIX and Linux User Community


Get status of dd running in background job


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get status of dd running in background job
# 1  
Old 10-08-2013
Get status of dd running in background job

Hello everyone

While working on TUI for scripts, there there came the idea to' add a command' for dd too.
That was, after 'wrapping' tar and wget either, to display their growing size and return the exit code with a textual-visual-feedback to the user.

Now displaying the filesize of a tarball or a file that is downoaded during a loop is quite easy.

What i'd like to get is the value of how many times BS (as in bytesize) has been written.

So it would look similar to (the console output):
Linux : [ Text User Interface ] : tui-download & tui-tar - YouTube
At around 0:40 of the video.

The full script is:
Note that tui-printf & tui-status can be replaced by 'printf' or 'echo'
tui-indi prints on each call the following of its previous -/|\

source tui loads in the configuration files/default variables not defined in this script.
Code:
#!/bin/sh
#    Author:     Simon A. Erat (sea)
#    Contact:    erat.simon@gmail.com
#
#    Variable defaults
#
    script_version=0.3
    ME="$(basename $0)"
    help_text="\r$ME ($script_version)
        \rUsage: $ME SOURCE TARGET
        \rWrites SOURCE to TARGET
        \rRequires root rights.
        \r"
    source tui
    FILE_CMD="$TUI_TEMP_DIR/bgjob"
    SOURCE="$1"
    TARGET="$2"
#
#    Variable handling
#
    if [[ "$1" = "-h" ]] || [[ -z $2 ]];then
    echo -e "$help_text" && exit $RET_HELP;fi
#
#    Display
#
    CMD="dd if=$SOURCE of=$TARGET bs=$DD_BS status=none > /dev/zero"
    echo "$CMD" > $FILE_CMD ; chmod +x $FILE_CMD ; $FILE_CMD &
    sleep 0.3
    
    while [[ ! "" = "$(ps|grep -v tui|grep dd)" ]]
    do    sleep 0.7
        tui-printf "Writing $(basename $SOURCE) to $TARGET"  " [  $(tui-indi)   ]"
    done
    
    tui-status $? "Written $(basename $SOURCE) to $TARGET"

Which works like it should.


According to the manpage, one's supposed to get job's info by:
Quote:
Originally Posted by man dd
Sending a USR1 signal to a running 'dd' process makes it print I/O statistics to standard
error and then resume copying.

$ dd if=/dev/zero of=/dev/null& pid=$!
$ kill -USR1 $pid; sleep 1; kill $pid
So i've changed accordingly:
(removed because outdated)

Problem now is:
Code:
# | Writing sea-AwesomeWM-F19.iso to /dev/sdb )                                                                                                                                            [  /   ] | #63+0 records in
63+0 records out
264241152 bytes (264 MB) copied, 4.22988 s, 62.5 MB/s
# | Writing sea-AwesomeWM-F19.iso to /dev/sdb )                                                                                                                                            [  -   ] | #65+0 records in
65+0 records out
272629760 bytes (273 MB) copied, 5.19806 s, 52.4 MB/s
# | Writing sea-AwesomeWM-F19.iso to /dev/sdb )                                                                                                                                            [  \   ] | #66+0 records in
66+0 records out
276824064 bytes (277 MB) copied, 6.16454 s, 44.9 MB/s
# | Writing sea-AwesomeWM-F19.iso to /dev/sdb )                                                                                                                                            [  |   ] | #
^C130 ~ $

I hoped redirecting the stderr to a variable would keep DD quite...
How could i achieve this?

Code:
...
CMD="dd if=$SOURCE of=$TARGET bs=$DD_BS"
...
while [[ ! "" = "$(ps|grep -v tui|grep dd)" ]]
    do    sleep 0.7
        pid=$(ps|grep -v tui|grep dd|awk '{print $1}')
        kill -USR1 $pid 2&> $TUI_TEMP_FILE
        tmp=$(grep "," $TUI_TEMP_FILE|awk '{print $3" "$4}')
        tui-printf "Writing $(basename $SOURCE) to $TARGET $tmp)"  " [  $(tui-indi)   ]"
    done

---------- Post updated at 16:39 ---------- Previous update was at 15:14 ----------

According to the manpages, this is supposed to work:
Quote:
Originally Posted by man bash
Note that the order of redirections is significant. For example, the command

ls > dirlist 2>&1

directs both standard output and standard error to the file dirlist, while the command

ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was dupli‐
cated from the standard output before the standard output was redirected to dirlist.
However, i have tried:
  1. kill -USR1 $pid 1&>2 > $TUI_TEMP_FILE
  2. kill -USR1 $pid 2&>1 > $TUI_TEMP_FILE
  3. kill -USR1 $pid > $TUI_TEMP_FILE > 2&>1
  4. CMD="dd if=$SOURCE of=$TARGET bs=$DD_BS > $TUI_TEMP_FILE > 2&>1" # status=none
In all caes $TUI_TEMP_FILE remained empty, while - if there were output - it was only shown to user but not written to tempfile.

It seems dd is either 'spamy' or dead-silent...
Any hints welcome. (or what am i understanding wrong?)

Last edited by sea; 10-08-2013 at 11:33 AM..
# 2  
Old 10-08-2013
I would suspect more kill than dd... ( Last time I tried a kill fancy option I finished going in the white room to restart to box I lost control on...)
# 3  
Old 10-08-2013
You cannot redirect a process after you start it. If you want dd's stderr to write into a file, you have to redirect it there in the first place.
# 4  
Old 10-08-2013
Quote:
Originally Posted by Corona688
You cannot redirect a process after you start it. If you want dd's stderr to write into a file, you have to redirect it there in the first place.
Guess i tried that with:
Code:
    CMD="dd if=$SOURCE of=$TARGET bs=$DD_BS "
    $CMD &  > $TUI_TEMP_FILE > 2&>1

But doesnt work -- keep getting all info on each loop to stout, but not to the file

Last edited by sea; 10-08-2013 at 01:34 PM..
# 5  
Old 10-08-2013
You are blanking the output file every time you kill, too. Remove that pile of redirections from kill.
# 6  
Old 10-08-2013
Pure silence (fail)
Code:
dd if=/dev/random of=/dev/zero & 2&>1 > /tmp/tmp
pid=$(ps|grep dd|awk '{print $1}')
while [[ ! "" = "$(ps|grep dd)" ]]
do     #kill -USR1 $pid
    grep "," /tmp/tmp | grep -v "+"|awk '{$2" "$3}'
done

Full spam (fail):
Code:
dd if=/dev/random of=/dev/zero & 2&>1 > /tmp/tmp
pid=$(ps|grep dd|awk '{print $1}')
while [[ ! "" = "$(ps|grep dd)" ]]
do     kill -USR1 $pid
    grep "," /tmp/tmp | grep -v "+"|awk '{$2" "$3}'
done

Meaning:
Code:
0+5 records in
0+0 records out
0 bytes (0 B) copied0 bytes (0 B) copied, 20.7462 s, 0.0 kB/s
, 2.36932 s, 0.0 kB/s
0+5 records in
0+0 records out
0+0 records in
0+0 records out
0 bytes (0 B) copied, 2.38147 s, 0.0 kB/s
0 bytes (0 B) copied, 20.7584 s, 0.0 kB/s
0+20 records in
0+0 records out
0 bytes (0 B) copied, 49.2783 s, 0.0 kB/s

All i want to achieve is "NO 'plain-dd' output" as above, but only this part: "0 bytes (0 B) copied" so i can print it on 1 line, 'staying' that very 1 line to the user, without the console spammed.

As in $tmp contains only "20mb" -- "25mb" -- "50mb" ...
Once i could 'mute' the DD output to the user, but still retrieve the values, even with the 0+0 records, i could 'cut' it down to the value needed, but this redirection stuff drives me crazy!! SmilieSmilie

Last edited by sea; 10-08-2013 at 02:02 PM..
# 7  
Old 10-08-2013
One problem: dd if=/dev/random of=/dev/zero quits instantly on my system. /dev/zero isn't something you can write to.

Also: 2&>1 is garbled, you want 2>&1.
This User Gave Thanks to Corona688 For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #480
Difficulty: Easy
Memory addresses are represented using hexadecimal, which is a base-8 counting system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for continuously checking status of a another script running in background, and immedia

Hi, I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help.. i am using below command to run script nohup system_traps.sh & but in some... (9 Replies)
Discussion started by: ketanraut
9 Replies

2. Solaris

Is user cron job running in background?

Hi, Should the user jobs specified in crontab be running in background? Cron daemon is already running in background. So I am not sure whether should the jobs (output and error messages are redirected to file) ran by it be explicitly stated to be run in background (& at end of command) if one... (1 Reply)
Discussion started by: joe_x
1 Replies

3. Shell Programming and Scripting

Background Job

Hello Everyody, Having a doubt. sort file1 & when we sent a job to the background it returns Job Number PID again if we want to ... (1 Reply)
Discussion started by: knroy10
1 Replies

4. UNIX for Dummies Questions & Answers

suspend a *background* running job

Is there a way to suspend (TSTP?) a job that is running in the background, _without_ first bringing it to the foreground and inputting Ctrl-Z from the keyboard? IOW, something similar to issuing the shell's bg builtin command on a job ID to resume a job that is suspended in the background,... (2 Replies)
Discussion started by: uiop44
2 Replies

5. Shell Programming and Scripting

Capturing the exit status of the script running in background

Hi All, I have a scenario where I am executing some child shell scripts in background (using &)through a master parent script. Is there a way I can capture the exit status of each individual child script after the execution is completed. (2 Replies)
Discussion started by: paragkalra
2 Replies

6. UNIX for Dummies Questions & Answers

Job Status for running shell script

Hello, I am running a shell script whose execution often takes several hours to complete. Is there way I can get some kind of status update as the job is running? Something as simple as the start and the current time stamp. Thanks, Gussi (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. AIX

Question on background running job

Guys, We use AIX 5.3 at our work place. I only in my team have a strange problem of not able run jobs background. Other colleagues are able to run without any problem. Once I kick off background job using nohup and & command, It immediately stops. The following error I get when I run. ... (2 Replies)
Discussion started by: anandsbr
2 Replies

8. Shell Programming and Scripting

How to know the status of process running in background

I have run one shell script in background that contains a endless while loop. I am not able to know the status of that job . Please provide any command to know this. I have already used "ps -aef" , "jobs" to know it , but it didn't work. I am sure the process is running as it is generating a file... (8 Replies)
Discussion started by: sumanta
8 Replies

9. Shell Programming and Scripting

Hw to Know the status of running JoB

Hi all, I am running a job .. and i want to know the status tht it is runnig or not .. and how can i find the jobId of my job .. I have to get it to kill my running job Pls let me know da Unix commands to do it .. i m wrking on Hp UNIX (1 Reply)
Discussion started by: ravi.sadani19
1 Replies

10. UNIX for Dummies Questions & Answers

Background job

Hiya, Recently I've run a few scripts in the foreground, but have realised later they should of been better nohup'd and placed in the background. I understand how to change a foreground job into a background one, but how would put the job into the nohup state? Thanks (1 Reply)
Discussion started by: rdbooth
1 Replies

Featured Tech Videos