New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terminal-notifier hangs if user is not logged in with graphical shell #29

Closed
casaschi opened this Issue Sep 21, 2012 · 8 comments

Comments

Projects
None yet
5 participants
@casaschi

casaschi commented Sep 21, 2012

login on macOs as userOne
open a terminal and run "su - userTwo" to for a shell prompt as userTwo
try terminal-notifier -message Hello

at this point terminal-notifier hangs

I'd rather have the command fail (quickly) and the script to move on rather than the script hanging

@kmarty

This comment has been minimized.

Show comment
Hide comment
@kmarty

kmarty Sep 28, 2012

Same problem if terminal-notifier is called by bash script which is called by launchd (using /Library/LaunchDaemons/...plist). It simply hangs.

kmarty commented Sep 28, 2012

Same problem if terminal-notifier is called by bash script which is called by launchd (using /Library/LaunchDaemons/...plist). It simply hangs.

@casaschi

This comment has been minimized.

Show comment
Hide comment
@casaschi

casaschi Sep 29, 2012

I found a workaround that seems to work also when called by launchd, it would be nice to have a confirmation from other users. This is for calling terminal-notifier from a bash script, checking first if the user has a running instance of NotificationCenter, otherwise it's pointless to send the notification.

if [ -n "$(pgrep -u $(whoami) NotificationCenter)" ]; then
terminal-notifier -message "$*"
fi

While a fix in terminal-notifier would be welcome, this seems to avoid the hanging issue. Does it work for you also?

casaschi commented Sep 29, 2012

I found a workaround that seems to work also when called by launchd, it would be nice to have a confirmation from other users. This is for calling terminal-notifier from a bash script, checking first if the user has a running instance of NotificationCenter, otherwise it's pointless to send the notification.

if [ -n "$(pgrep -u $(whoami) NotificationCenter)" ]; then
terminal-notifier -message "$*"
fi

While a fix in terminal-notifier would be welcome, this seems to avoid the hanging issue. Does it work for you also?

@kmarty

This comment has been minimized.

Show comment
Hide comment
@kmarty

kmarty Oct 1, 2012

My bash script which is called by launchd is started as user 'root' and this user doesn't have running NotificationCenter ever.
The easiest thing ("workaround") is to remove notification at all because condition is false forever. Unfortunatelly this is not solution, becasue the main goal is to notify user (terminal-notifier was try to replace growl which is working in this case).

kmarty commented Oct 1, 2012

My bash script which is called by launchd is started as user 'root' and this user doesn't have running NotificationCenter ever.
The easiest thing ("workaround") is to remove notification at all because condition is false forever. Unfortunatelly this is not solution, becasue the main goal is to notify user (terminal-notifier was try to replace growl which is working in this case).

@casaschi

This comment has been minimized.

Show comment
Hide comment
@casaschi

casaschi Oct 1, 2012

Your issue is that terminal-notifier sends the message to the NotificationCenter of the current user, and root is not logged in with the graphical UI ever. I dont have the issue because I use a user specific launchd.

You could play with sudo so that the root user impersonates your user when sending the notification, that might work. Still check for the given user having a NotificationCenter process, otherwise the script will hang.
Something like this should send a notification to the given user if logged in, otherwise skip the notification. I typed this without testing it, so you might have to doublecheck/debug, but you get the idea.

targetUser=kmarty
if [ -n "$(pgrep -u $targetUser NotificationCenter)" ]; then
sudo -u $targetUser terminal-notifier -message "$*"
fi

casaschi commented Oct 1, 2012

Your issue is that terminal-notifier sends the message to the NotificationCenter of the current user, and root is not logged in with the graphical UI ever. I dont have the issue because I use a user specific launchd.

You could play with sudo so that the root user impersonates your user when sending the notification, that might work. Still check for the given user having a NotificationCenter process, otherwise the script will hang.
Something like this should send a notification to the given user if logged in, otherwise skip the notification. I typed this without testing it, so you might have to doublecheck/debug, but you get the idea.

targetUser=kmarty
if [ -n "$(pgrep -u $targetUser NotificationCenter)" ]; then
sudo -u $targetUser terminal-notifier -message "$*"
fi

@kmarty

This comment has been minimized.

Show comment
Hide comment
@kmarty

kmarty Oct 1, 2012

Nevertheless you brought me to this thought:
ps aux | grep NotificationCenter | grep -v grep | cut -d' ' -f1 |
while read JUZR; do
su - "$JUZR" -c "terminal-notifier -message "$1""
done

Because I need to tell it to anyone who is logged (a broadcast message :-)). And this works.

EDIT: Wow, I should refresh page before I'm trying to send a new post :-). You wrote the same.
BTW. 'sudo -u user ...' doesn't work. It freezes.

kmarty commented Oct 1, 2012

Nevertheless you brought me to this thought:
ps aux | grep NotificationCenter | grep -v grep | cut -d' ' -f1 |
while read JUZR; do
su - "$JUZR" -c "terminal-notifier -message "$1""
done

Because I need to tell it to anyone who is logged (a broadcast message :-)). And this works.

EDIT: Wow, I should refresh page before I'm trying to send a new post :-). You wrote the same.
BTW. 'sudo -u user ...' doesn't work. It freezes.

@src73

This comment has been minimized.

Show comment
Hide comment
@src73

src73 May 8, 2013

I thought that I'd write a conclusion to this thread for n00bs like me...

My shell script was working perfectly when I executed it as a user, but did not work when I ran it as root's launchd. My code was hanging (which for me meant that nothing was appearing in the logs because I had placed the terminal notification first). I fixed it by replacing terminal-notifier -message 'hi', with...

ps aux | grep NotificationCenter | grep -v grep | cut -d' ' -f1 | \
while read JUZR; do
    su - "$JUZR" -c "/path/to/terminal-notifier -message \"$your_message\" -title \"$your_title\""
done

...which will display the notification to all users who are logged in and have a notification center running, including you.

EDIT: this only works if the launchd is being run as root. If it is being run as user, it just needs

/path/to/terminal-notifier -message "$your_message" -title '$your_title'

src73 commented May 8, 2013

I thought that I'd write a conclusion to this thread for n00bs like me...

My shell script was working perfectly when I executed it as a user, but did not work when I ran it as root's launchd. My code was hanging (which for me meant that nothing was appearing in the logs because I had placed the terminal notification first). I fixed it by replacing terminal-notifier -message 'hi', with...

ps aux | grep NotificationCenter | grep -v grep | cut -d' ' -f1 | \
while read JUZR; do
    su - "$JUZR" -c "/path/to/terminal-notifier -message \"$your_message\" -title \"$your_title\""
done

...which will display the notification to all users who are logged in and have a notification center running, including you.

EDIT: this only works if the launchd is being run as root. If it is being run as user, it just needs

/path/to/terminal-notifier -message "$your_message" -title '$your_title'

@alloy alloy closed this in 04e3148 Aug 24, 2013

@alloy

This comment has been minimized.

Show comment
Hide comment
@alloy

alloy Aug 24, 2013

Collaborator

Thanks for the reports and sorry for taking so long, I hope this adequately fixes the problem.

Collaborator

alloy commented Aug 24, 2013

Thanks for the reports and sorry for taking so long, I hope this adequately fixes the problem.

@ddmytrenko

This comment has been minimized.

Show comment
Hide comment
@ddmytrenko

ddmytrenko Oct 20, 2015

The problem still exists. terminal-notifier does not work if running using launchctl load.

ddmytrenko commented Oct 20, 2015

The problem still exists. terminal-notifier does not work if running using launchctl load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment