Executing a script when a service starts/stops

What is the best way to control whether a service is running or not, so that when the state changes, the script is started?

Read pgrep every second? Or is it possible to run the script on an event?

Upd: I need a script that will run when the dropbear or uhttpd services start or stop, for control purposes.

ubus subscribe service

gives you events as line / JSON objects.

2 Likes
  1. See hotplug.d, if script brings up/down interfaces.
  2. Follow log in bash-script:
logread -f | while read line; do
 echo $line
done
1 Like

I've tried, but so far I can't get it to react to start/stop a particular service.

Unfortunately, the start/stop is not recorded in the logs.

I need a script that will run when the dropbear or uhttpd services start or stop, for control purposes.

How about

ubus subscribe service | jsonfilter -e '@["instance.start"].service' | while read service; do
  echo "$service just started"
done
1 Like

Thank you! I don't know what I did wrong yesterday, but today the ubus subscribe service is working.
I excluded jsonfilter because it doesn't read the incoming stream all the time, but did it through if and grep right in the while loop.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.