Is it possible to edit the command line of a process sent to background, or to chain another command to execute after the first command has exited?
Eg:
I start mirroring a site with wget -m site.com
but later realize that I want to be notified by email when the job executed. The job is running in the background (bg
). How can I add a && sendmail
to the command, or execute it when the first command has completed execution?
If you've still got the session you started the original command from and you just want to know when it finishes then you can use
wait
which will wait until the specified child process completes. Assuming the use ofbash
something like ...... replacing the
echo
command with a more useful notification.This doesn't get you any of the output from the original command but gives a simple way to notify on completion.
If you don't still have the session you started the original command from then you'd need to knock together a script to watch for the process disappearing.
There's no any built in way to do this. However I'd run just a oneliner to achieve what you need.
However you need to know your process id of the bg process (in your case
wget
). You could runps aux | grep wget
Once you have the PID, You can use
wait
. However this doesn't work if you closed the previous shell or session.In that case I'd use
ps -p PID
with awhile
loop to fire sleeps while the process is active and after process stoppedwhile
statement ends and shell will execute yoursendmail
command.Full code goes as:
summery of above code: while your selected PID process runs
while
condition becomes true and loop keeps executingsleep 1
and it checks for it's condition every one second. However if the process endswhile
condition becomes false and the while loop will end. And the shell will execute the command after && which issendmail
Short answer: You can't.
Long answer: You could try to attach a debugger (strace, gdb) and get your output that way and then mail that information. But first you'd get a different output format and second this is all kinds of things but not very useful.