I want to ask about how to get notified if a program starts running in a shell script without checking it in a loop with 'pidof' for example, which consume CPU resources.
Asked
Active
Viewed 125 times
0
-
You somehow need some way by which your process convey that it is running. This is usually done by checking pid file for that process, which usually sits at /var/run – Niraj Nandane Sep 13 '22 at 12:20
-
What do you mean by "get notified"? Print something? Run a command? – stark Sep 13 '22 at 12:39
-
@stark i mean instead of executing the command pidof in a loop in order to check if the program starts is there any other efficient method which does not consume CPU like interrupt or signal that could be triggered ? – kakachi_hatake Sep 13 '22 at 13:13
-
Regarding the program you want to detect, is it a script or a binary? Is it a program whose source you control? – Mark Setchell Sep 13 '22 at 13:46
-
Here is a hideous hack if you like such things... https://stackoverflow.com/a/24202568/2836621 – Mark Setchell Sep 13 '22 at 13:49
-
@MarkSetchell it's a binary whose source i can't control; and about the wrapping method i guess it will not solve my problem because i wanna reduce CPU usage instead of checking when my program starts for example (while [!$(pidof program)] ==> instead am searching for an alternative way to not call pidof in loop – kakachi_hatake Sep 13 '22 at 14:09
-
I was implying having your monitoring program block (without consuming CPU) on a read from a fifo, or on a UDP read or on read from an MQTT subscription that the wrapped program issued and fulfilled. Try it with `mkfifo /tmp/fifo` then do a blocking read from it and check CPU usage `read line < /tmp/fifo` then notify the fifo `echo STARTED > /tmp/fifo` – Mark Setchell Sep 13 '22 at 14:20
-
@NirajNandane, I wouldn't call pidfiles "usual" practice -- modern best-practice process supervision doesn't use them; if the thing being watched runs as a service, one can configure another service to run when it does. (For that matter, pre-systemd best-practice process supervision didn't use pidfiles either -- look at DJB daemontools and its BSD-licensed replacement http://smarden.org/runit/, or https://skarnet.org/software/s6/, or even the old `/etc/inittab` approach _without_ `rc.d` extensions to same) – Charles Duffy Sep 13 '22 at 15:33
-
That said -- in a lot of circumstances, I'd just replace the program's executable shim that sends your notice and then starts the original/real executable. Make the shim setgid to a group that doesn't provide any other meaningful/special privileges and the real executable only readable to people in the group, and you can make it impossible for any user to run that executable _without_ going through the shim. – Charles Duffy Sep 13 '22 at 15:37
1 Answers
1
You can use the inotifywait
command
inotifywait -e open /usr/bin/ls
change ls
for your desired program.
See inotify-tools packages.

Germán
- 21
- 4
-
I think this will also trigger if someone opens the file for some purpose other than running it, e.g. `wc -c /bin/ls` or `strings /bin/ls` or `xxd /bin/ls` – Mark Setchell Sep 13 '22 at 15:30
-
...sure, but one can scan for what type of usage is active at that time (in the trigger, post-notification). This is still a good approach (but on a modern system, instead of inotifywait I would use a systemd path unit to do the same thing but with the init system itself doing the monitoring). – Charles Duffy Sep 13 '22 at 15:34
-
@Germán thank you for response, just this will trigger an event if the command will be executed, for my purpose is to check if a program start running using pidof it means it gets a pid so how it could be ? inotifywait -e modify /usr/bin/pidof "program" ?? because i want to get notified when the program get a pid and start running – kakachi_hatake Sep 13 '22 at 22:21
-
-
@kakachi_hatake. inotifywait -e open /usr/bin/less ; pidof less. With ls not work because ls end before pidof runs. Excuse me for my english – Germán Sep 14 '22 at 18:15