node-notifier icon indicating copy to clipboard operation
node-notifier copied to clipboard

Doesn't work when used within proces runner such as PM2 or CRONtab?

Open kunokdev opened this issue 7 years ago • 7 comments
trafficstars

I've added script to my crontab somewhat like this:

@reboot /path/to/script
* * * * * /path/to/script

code runs, however there is no notification.

return notifier.notify({
  title: 'foo',
  message: randomMessage(),
})

Same issue with PM2. So it appears that when it's being ran by some different process, not from user's shell, then it fails to send notification to desktop.

Same issue when using with gnome-session-properties. Linked SO question: https://stackoverflow.com/questions/49221716/when-using-notify-send-inside-some-process-runner-such-as-cron-or-pm2-notificat

Versions: Ubuntu 16.04 Gnome3 (gubuntu distro). notify-send 0.7.6 "node-notifier": "^5.2.1",

kunokdev avatar Mar 11 '18 00:03 kunokdev

@kunokdev did you end up figuring this out? It seems I'm having the same problem.

NicolasPerl avatar May 22 '18 14:05 NicolasPerl

@NicolasPerl Unfortunately, I haven't. Spent whole night debugging linux notify-send (which is used as shell command within this lib for Linux) and I couldn't fix it. However, I think it's related to users, CRON is one user, Desktop environment is other user, and active shell user is third. So perhaps, desktop environment user has no access to crontab user's actions or vice versa. This needs deeper inspection tho. Also, in some edge cases this code worked, I don't know why and I couldn't debug it because I was doing presentation at that moment (and my bot's notification popped up that time, and never once again or before)

kunokdev avatar May 23 '18 00:05 kunokdev

Can confirm this is working: https://github.com/mikaelbr/node-notifier/issues/148

joshhopkins avatar Jan 23 '19 18:01 joshhopkins

I'm trying to send notifications from cron, and the notifications don't display under ubuntu 19.04. It does work under task scheduler on windows 10, though (using the exact same scripts).

Trying to run notify-send in general from cron does not work properly, but I found a this on SO which makes it work correctly from cron * * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send Hey "this is dog!

So in my .sh script I've added export XDG_RUNTIME_DIR=/run/user/$(id -u) to the top of it, and that has made it work from cron for linux.

SPDUK avatar Apr 23 '19 09:04 SPDUK

My experience with this is, running via pm2, it works out of the box on my Mac, but not on a Win10 machine. (the script run by itself shows the notification on Win10, but not if run via pm2). Still trying to figure it out.

nguiard avatar May 07 '19 14:05 nguiard

Actually, upon further testing, it appears that my issue on Windows 10 was linked to the way I had pm2 starting up. I used nssm (Non-Sucking Service Manager) to make pm2 startup on Win10. At first I made it startup as a system user, which was the main problem. After I edited the pm2 service with nssm to start as a specific regular user, notifications worked for me. Hoping this can help someone.

nguiard avatar May 07 '19 15:05 nguiard

Related to #333

mikaelbr avatar Oct 28 '20 17:10 mikaelbr