My solution (using inotifywait)
This is based on linux's /proc filesystem.
My need was to start a 2nd (overall) backup, once containers backups is done. Containers backups is started by cron.
Watching for cron tasks
read -r wpid < <(ps -C backup.sh ho pid)
ls -l /proc/$wpid/fd
total 0
lr-x------ 1 user user 64 1 aoû 09:13 0 -> pipe:[455151052]
lrwx------ 1 user user 64 1 aoû 09:13 1 -> /tmp/#41418 (deleted)
lrwx------ 1 user user 64 1 aoû 09:13 2 -> /tmp/#41418 (deleted)
Where deleted entries was created by cron. But even if deleted, you could watch for file descriptor directly:
inotifywait /proc/$wpid/fd/1
/proc/511945/fd/1 CLOSE_WRITE,CLOSE
or
inotifywait /proc/$wpid/fd/0
/proc/511945/fd/0 CLOSE_NOWRITE,CLOSE
Note: My overall backup is run as root user! If no this could require sudo because command is run under cron session!
Same session
Just test: In a 1st window, hit:
sleep 0.42m <<<'' >/dev/null 2>&1
Then in another window:
read -r wpid < <(ps -C sleep wwho pid,cmd| sed 's/ sleep 0\.42m$//p;d')
ls -l /proc/$wpid/fd
total 0
lr-x------ 1 user user 64 1 aoû 09:38 0 -> pipe:[455288137]
l-wx------ 1 user user 64 1 aoû 09:38 1 -> /dev/null
l-wx------ 1 user user 64 1 aoû 09:38 2 -> /dev/null
Don't try to watch for 1 or 2! Because they point to /dev/null, any process acessing to them will trig inotifywait.
inotifywait /proc/$wpid/fd/0
/proc/531119/fd/0 CLOSE_NOWRITE,CLOSE
Elapsed time in seconds
1st window:
sleep 0.42m <<<'' >/dev/null 2>&1
2nd window:
read -r wpid < <(ps -C sleep wwho pid,cmd| sed 's/ sleep 0\.42m$//p;d')
startedAt=$(ps ho lstart $wpid | date -f - +%s)
inotifywait /proc/$wpid/fd/0;echo $((EPOCHSECONDS-startedAt))
/proc/533967/fd/0 CLOSE_NOWRITE,CLOSE
25
Conclusion.
Using inotifywait seem to be a good solution, mostly watching for command's standard input (fd/0). But this must be tested case by case.