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
# 8  
Old 10-08-2013
Code:
( dd if=/dev/zero of=/dev/null 2>&1 |
        awk -F"[()]" '/copied/ {print $2}' ) &

PID=$(pgrep -u $USER dd) # Get the PID, since we don't get it in $!
trap "kill $PID" EXIT # Stop the background proc on accidental quit

while ps $PID >/dev/null 2>/dev/null
do
        sleep 1
        kill -USR1 $PID
done

# 9  
Old 10-09-2013
Sadly i get this on 'copy-paste':
Code:
...
> done
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
^C
130 /home/simon #

It cant find the $PID the way you did, and even when applying my pid retrieval, the output is hidden Smilie

EDIT: /* removed 3 appended posts as its working now.. */

This seems to be working: Smilie
Code:
    dd if=$SOURCE of=$TARGET 2> $TUI_TEMP_FILE &
    PID=$(ps|grep -v tui|grep dd|awk '{print $1}')
    while [[ ! "" = "$(ps|grep -v tui|grep dd|awk '{print $1}')" ]]
    do     kill -USR1 $PID 2>&1
        SIZE=($(grep "," $TUI_TEMP_FILE|awk '{print $3$4}'))
        [[ ${#SIZE[@]} -eq 1 ]] && max=0 || max=$[ ${#SIZE[@]} - 1 ]
        tui-printf "Writing $SOURCE to $TARGET" "${SIZE[$max]}" "[  $(tui-indi)   ]"
        sleep 0.7
    done

Thanks for your help!

Last edited by sea; 10-09-2013 at 11:10 AM..
# 10  
Old 10-09-2013
Just because i'm happy and bit proud of it, it looks now like this:
(had pressed enter to 'show progress')
Get status of dd running in background job-tui-dd-pressed-enter-illustrate-.jpg

Would actualy look like:
Code:
✔ ~ # ISO=/home/simon/net/dls/iso/sea-Awesome-WM.iso
+ ~ # tui-dd $ISO $(tui-str-usb)
# | Written sea-Awesome-WM.iso to /dev/sdb                                             [  ✔   ] | #
:) ~ #

Thanks again Smilie


/solved
Get status of dd running in background job-tui-dd-pressed-enter-illustrate-.jpg

Last edited by sea; 10-09-2013 at 11:39 AM.. Reason: Dont see no 'thread tools - mark as solved'
# 11  
Old 10-09-2013
You can use $! to get the PID of the last started background process.

You can also use jobs %1 to get the status of the job (testing it with $?), and kill %1 to kill it.
This User Gave Thanks to Scott For This Post:
# 12  
Old 10-09-2013
I droped the $! as the example line failed yesterday, but Corona's script did run successfully (but wrote a new line each loop).
Today its vice-versa (still copy-pasted), weird things happen on/to my laptop...
# 13  
Old 10-09-2013
Quote:
Originally Posted by sea
I droped the $! as the example line failed yesterday,
Oh, sorry I missed that Smilie
# 14  
Old 10-22-2013
You guys probably knew already, but me just figured out how to get the return value of job running/ran in background.
For some weird reason it didnt want to work with an exported variable.

This one will write the exit code to a temp file, so it can be easy retrieved after the loop is done.
Code:
	(sh "$JOB" && \
			echo 0 > /dev/stdout > $TEMP_FILE || \
			echo 1 > /dev/stdout > $TEMP_FILE
	) & PID=$!

Where $JOB is a scriptfile containg the command to execute and $TEMP_FILE is a pre-set var (like: /tmp/tmp.ret)

Now to use that retvar in a variable again is easy:
Code:
JOB_RET=$(cat $TEMP_FILE)

Et voila Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question