patri9ck / a2ln-server

A way to display Android phone notifications on Linux (Server)
GNU General Public License v3.0
98 stars 8 forks source link

Using crontab to start a2ln fails #27

Closed Ao1Pointblank closed 1 year ago

Ao1Pointblank commented 1 year ago
#attempt to let crontab run graphical applications
XDG_RUNTIME_DIR="/run/user/1000"
DISPLAY=:0.0

#start at system boot
@reboot a2ln 46352 --pairing-port 46351 --title-format "{app} | {title}"  --command 'if [ -f ~/.sounds/a2ln/{app}.mp3 ]; then play -Gq --norm=0 ~/.sounds/a2ln/{app}.mp3; else play -Gq --norm=0 ~/.sounds/a2ln/default.mp3; fi'

#reboot a2ln server every 20 minutes
*/20 * * * * pgrep a2ln && pkill a2ln && notify-send -u low --hint=int:transient:1 'A2LN Server Rebooted'; a2ln 46352 --pairing-port 46351 --title-format "{app} | {title}"  --command 'if [ -f ~/.sounds/a2ln/{app}.mp3 ]; then play -Gq --norm=0 ~/.sounds/a2ln/{app}.mp3; else play -Gq --norm=0 ~/.sounds/a2ln/default.mp3; fi'

These are the contents of my crontab (crontab -e, to edit it). I know the second command at least partially works, because I do receive the notification every 20 minutes. However, the @reboot line does not seem to start the server, according to my system monitor.

After some more testing, I determined a2ln can be launched from the command prompt button combo (alt+f2 on Mint by default)

I rewrote the kill/restart script so it isn't just a one-liner anymore, and tried running it from the command prompt.

#!/bin/bash
if pgrep a2ln ; then
    pkill a2ln && sleep 2s ; a2ln 46352 \
        --pairing-port 46351 \
        --title-format "{app} | {title}" \
        --command 'if [ -f ~/.sounds/a2ln/{app}.mp3 ]; then play -Gq --norm=0 ~/.sounds/a2ln/{app}.mp3; else play -Gq --norm=0 ~/.sounds/a2ln/default.mp3; fi';
    notify-send -u low --hint=int:transient:1 'A2LN Server Rebooted';
fi

It kills the server just fine, but doesn't restart it. The notification is also not produced. All these commands can be run from the command prompt individually, but not together when executed as one-liner or from a file, even when prefixed with 'bash -c', within the command prompt. I am using the command prompt as a testbed for command execution without GUI, since it is easier than restarting my computer over and over to test the @reboot function in crontab.

Oh, and when the .sh file is run in terminal, it simply kills any running a2ln server then closes with signal 15 (SIGTERM)

Jan9103 commented 1 year ago

hi,

with the current makefile a2ln is installed into ~/.local/bin/a2ln (on ubuntu), which is not in the default PATH and therefore can probably not be found by a cronjob (but interactive bash can find it due to your ~/.bashrc).
You could try to replace a2ln with /home/USERNAME/.local/bin/a2ln (or wherever it is installed according to whereis a2ln) in your crontab.

it might also help with debugging to see the log (/var/log/cron.log).

Since you are using mint i assume you are also using systemd. in which case a service would probably be a good alternative to a crontab:
~/.config/systemd/user/a2ln.service:

[Unit]
Description=Android 2 Linux Notifications Server

[Service]
ExecStart=/home/USERNAME/.local/bin/a2ln 46352 --pairing-port 46351 --title-format "{app} | {title}"  --command 'if [ -f ~/.sounds/a2ln/{app}.mp3 ]; then play -Gq --norm=0 ~/.sounds/a2ln/{app}.mp3; else play -Gq --norm=0 ~/.sounds/a2ln/default.mp3; fi'
Restart=always

[Install]
WantedBy=graphical.target

and then run systemctl --user enable a2ln to enable autostart and systemctl --user start a2ln to start it and then systemctl --user status a2ln to check if it started (and systemctl --user restart a2ln to restart it).

Ao1Pointblank commented 1 year ago

This is perfect for me!! I also added the line RuntimeMaxSec=20m before Restart=always (i don't know if the order of the lines matters) to make sure it stays working! Thank you @Jan9103 ^^