3

I use the alarm-clock-applet when doing work. When the timer ends, I have it run a script. The script does two things: 1) Give an on-screen notification and 2) Plays an audio clip.

The notification-daemon is not always running. So, if it is not running, I want to start it before trying to pass the on-screen notification.

To this end, I have the following:

#!/bin/sh

if ! pgrep -f "notification-daemon" > /dev/null ;
    then
    /usr/lib/notification-daemon/notification-daemon &
fi

notify-send "Take 5"
aplay /home/Me/Music/brubek-clip.wav

The trouble is that on the first run, the notification does not appear. The daemon has been started successfully and subsequent runs of the script function as expected.

When I put a sleep 0.5 after the call to the notification-daemon, the notification appears on the first run. But this seems rather crude.

Is there a way to check that the notification-daemon has started before continuing?

1

You could try using a while-loop to wait for the service to launch:

#!/bin/sh

# If the notification daemon isn't running then launch it
if ! pgrep -f "notification-daemon" > /dev/null; then
    /usr/lib/notification-daemon/notification-daemon &
fi

# Wait for the notification daemon to finish launching
while ! pgrep -f "notification-daemon" > /dev/null; do

    # Set optional delay
    sleep 0.1

done

# Play awesome song (do-doop da-doop doop-doop-doop...)
notify-send "Take 5"
aplay /home/Me/Music/brubek-clip.wav

This should guarantee that you don't continue until the daemon is running (e.g. if for some reason it takes longer than your 0.5 second sleep).

I did a little bit of web-searching for similar posts and found a few that seem relevant:

These all seem to follow the same basic approach - use a loop to wait until the desired condition is met.

UPDATE: It turns out that waiting for the process to start isn't sufficient. In this case you need to wait for the service to become available. You may want to look at the following posts:

  • Hi there! Your if clause ends with done instead of fi. – maulinglawns Dec 10 '17 at 17:37
  • You're welcome. Happens to all of us occasionally! – maulinglawns Dec 10 '17 at 17:41
  • 1
    It is not enough to check for the process; you have to check whether it already is accepting connections. You can check for a socket or for it being registered on dbus (depending on how the communication works). – Hauke Laging Dec 10 '17 at 19:49
  • I ran into the problem @HaukeLaging mentions; the loop would terminate because the PID had been created but the notification didn't appear because the application wasn't fully loaded. I'll investigate sockets etc. – Lorem Ipsum Dec 10 '17 at 20:39
  • @LoremIpsum I don't know very much about dbus but I found a few relevant posts, at least a couple of which seem to address the functionality that you're looking for - updated my post to include them. – igal Dec 11 '17 at 1:14

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.