21

There are a few questions and answers on here with regard to being alerted when a process completes/exits(1, 2) – but these all assume that the user has issued said process themselves, and thus can script it with an alert built into the governing script, or pipe the process to some kind of alert.

My situation is that I would like to be alerted of the completion/exit of a process that my user is not initializing. Namely, I am bulk processing massive video files on a Ubuntu 12.04 LTS server. Certain operations on these files take a very long time, so I would like some kind of alert (email would be great) when a specific one finishes. They take so long, that doing this on a one-off basis, manually, based on PIT would be perfectly fine.

To provide more info – let's say I'm processing a particularly big file, and I see that it has progressed on to an FFMPEG script, the process itself being a python script (that is quite complex, and not written by myself, and something I do not wish to modify – though that would be the first logical approach). I imagine issuing a command or script with the PID of said running python script as an argument, and when the process with that PID is no longer running, the alert script does its thing.

Any ideas?

  • monit (mmonit.com/monit/documentation) seems like a potential option, but much more complex than what I need – hoping there may be a simpler, more direct, and less bloated way of doing only what I am asking, and no more. – dongle Sep 3 '14 at 14:05
9

I wrote process_watcher.py

process_watcher --pid 1234 --to me@gmail.com

Currently, email body looks like:

PID 18851: /usr/lib/libreoffice/program/soffice.bin --writer --splash-pipe=5
Started: Thu, Mar 10 18:33:37 Ended: Thu, Mar 10 18:34:26 (duration 0:00:49)
Memory (current/peak) - Resident: 155,280 / 155,304 kB Virtual: 1,166,968 / 1,188,216 kB

[+] indicates the argument may be specified multiple times, for example:
 process-watcher -p 1234 -p 4258 -c myapp -c "exec\d+" --to person1@domain.com --to person2@someplace.com

optional arguments:
  -h, --help            show this help message and exit
  -p PID, --pid PID     process ID(s) to watch [+]
  -c COMMAND_PATTERN, --command COMMAND_PATTERN
                        watch all processes matching the command name. (RegEx pattern) [+]
  -w, --watch-new       watch for new processes that match --command. (run forever)
  --to EMAIL_ADDRESS    email address to send to [+]
  -n, --notify          send DBUS Desktop notification
  -i SECONDS, --interval SECONDS
                        how often to check on processes. (default: 15.0 seconds)
  -q, --quiet           don't print anything to stdout

Ceate a GitHub issue if you need any improvements to it.

21

Something like this?

(while kill -0 $pid; do sleep 1; done) && echo "finished"

Replace $pid with the process id and echo "finished" with whatever you want to do when the process exited.

For example:

(while kill -0 $pid; do sleep 1; done) && mail ...
  • Phenomenal! Works like a charm. – dongle Sep 3 '14 at 14:19
  • 1
    Minor (and likely obvious) note – when said process is not owned by user, must sudo the kill. – dongle Sep 3 '14 at 14:25
  • 1
    @dongle, at least on my RHEL box, kill -0 reports No such process for non-existent pids and Operation not permitted for pids owned by a different user so perhaps interpreting the error message should obviate the need for sudo – iruvar Sep 3 '14 at 16:37
  • 1
    If you're echoing, you could always echo -en "\007 \007 \007", which will issue a terminal 'bell'. Whether that translates to an actual audible sound depends on your terminal and hardware (but typically does). – Morgon Sep 3 '14 at 16:50
5

I use && to execute a second command when the first is finished successfully. Here some examples:

  1. Delete a big folder and beep:

    rm -rv ~/tmp && beep
    
  2. Compile a kernel and send an email:

    make && mail -s "Compilation is done" user@yourmaildomain.com
    

Is this a good way to solve your problem? In another case, you can add an || and a third command in case of failure.

Also, an shell script could receive the exit status ($?) of your command, and with if/else and loops you can do whatever you want.

  • Thanks Willian, but as I said, the particular challenge in my use-case is that this is not a command that I am issuing, but an already running process. Thus && is not an option. Thanks anyway. – dongle Sep 3 '14 at 14:17
4

If you don't have the pid, this will run until all instances of ffmpeg are finished:

(while pidof ffmpeg | grep -q "[0-9]?"; do sleep 1; done) && echo "finished"
0

If you don't want to use a polling option or an external script, and you've already started the long-running process, you can suspend it with CTRL+Z (sends SIGTSTP signal) and then resume it with fg (moves process to the foreground).

For example:

my_command  # long running command
# press CTRL+Z to pause
fg ; notify-send 'finished'
  • Except ... "My situation is that I would like to be alerted of the completion/exit of a process that my user is not initializing. " – Jeff Schaller Feb 14 at 8:24
-1

I made an app that will send a notification to your Android phone when a process ends so you don't have to be tethered to your computer.

Install, then add ;notifier.sh to the end of your command.

For example, if your command was make install, you would make your command:

make install; notifier.sh

https://notifier.sh

https://play.google.com/store/apps/details?id=sh.notifier

  • “…but these all assume that the user has issued said process themselves, and thus can script it with an alert built into the governing script, or pipe the process to some kind of alert. / My situation is that I would like to be alerted of the completion/exit of a process that my user is not initializing.” – G-Man Jan 30 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.