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:


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

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 Answer 1


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


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

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

    # Set optional delay
    sleep 0.1


# 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. Dec 10, 2017 at 17:37
  • You're welcome. Happens to all of us occasionally! Dec 10, 2017 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). Dec 10, 2017 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. Dec 10, 2017 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, 2017 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .