0

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?

Joel G Mathew
  • 890
  • 1
  • 9
  • 19

3 Answers3

2

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 of bash something like ...

$ ./30s &
[1] 32135

$ wait %1 && echo hello
[1]+  Done                    ./30s
hello

... 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.

Paul Haldane
  • 4,517
  • 1
  • 21
  • 32
1

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 run ps 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 a while loop to fire sleeps while the process is active and after process stopped while statement ends and shell will execute your sendmail command.

Full code goes as:

while ps -p YOUR-PID > /dev/null; do sleep 1; done && echo "Subject: WGET DONE!" | sendmail user@domain.com

summery of above code: while your selected PID process runs while condition becomes true and loop keeps executing sleep 1 and it checks for it's condition every one second. However if the process ends while condition becomes false and the while loop will end. And the shell will execute the command after && which is sendmail

inckka
  • 201
  • 1
  • 10
0

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.

user2563336
  • 116
  • 4