Patiobar icon indicating copy to clipboard operation
Patiobar copied to clipboard

patiobar with cron

Open K37V1N opened this issue 3 years ago • 4 comments

I'm trying to run patiobar/pianobar with crontab but I can't seem to get the audio to actually work. The only error message in a log I've gotten is something like "can't start audio device". Any suggestions for how to run this with Cron?

K37V1N avatar Jul 23 '21 14:07 K37V1N

From a quick Google search, there's an env variable that needs to be set (which isn't set when cron jobs run). I am not too familiar with getting sound working with cron, so I can't give any more info than that to help.

However, a workaround is to do something similar to what I do. I have a service that detects when my phone is connected to the network. If it's been an hour or more since the last connection, it'll trigger patiobar to start

Cloud9Developer avatar Jul 23 '21 14:07 Cloud9Developer

@K37V1N Yeah, I'd say this is almost certainly an env var issue, as @cph015 mentioned.
Is this in your own crontab (crontab -e), or root's crontab, or some other user? If it is your own user, you should already be in the audio group.

From what I'm reading, you'll need to get the XDG_RUNTIME_DIR env var from your current session:

env|grep -i runt
XDG_RUNTIME_DIR=/run/user/1000

And then set that in your crontab, something like: * 1 2 3 4 5 XDG_RUNTIME_DIR=/run/user/1000 /path/to/what/you/are/starting.sh

But let me ask, why are you running Patiobar in cron? I'm curious about your usecase.

kylejohnson avatar Jul 27 '21 14:07 kylejohnson

I'm doing this on a raspberry pi, and using Raspbian for the OS. I'm editing the cron with crontab -e, so I believe it's running with the default user of "Pi".

User case is running music for animals in a shelter/adoption environment. I'm wanting to be able to turn on music at say 11am which is after the feeding and walking in the morning, and before the adoption floor is open. We've setup an hour long nap time for them, so I'd like some calm classical music type stuff to play during that time and for it to automatically start playing without anyone having to do anything. I'd also like it to be able to stop at the end of the day so the music isn't playing the entire night. Our behaviorists have talked about calming music possibly being a calming influence on the dogs behavior and I'm curious if it will be. I'm also curious if the music starts at the exact same time everyday (the same time the lights turn off/dim) if that will help trigger the dogs to take a nap.

I'll give the XDG_RUNTIME_DIR a test and see what I can get to happen. Just for clarification, what is the "=/run/user/1000" part of that command?

K37V1N avatar Jul 29 '21 13:07 K37V1N

That is a very noble cause! Sounds like a good use case for running this via cron.

  1. env | grep -i runt: If you run env, it lists all of your current environment variables. | grep -i runt pipes the output of env in to grep, and searches for a case-insensitive (-i) match for runt. runt matches XDG_RUNTIME_DIR. Doing env | grep XDG_RUNTIME_DIR would give you the same result.
  2. XDG_RUNTIME_DIR=/run/user/1000: This is the output from the env | grep -i runt command. In this case, XDG_RUNTIME_DIR is the name of the variable, and /run/user/1000 is the value assigned to that variable.

Here's an example where I'm searching for the PWD, which returns two variables, and then for OLDPWD, which only returns one. The env | wc -l line shows that there are 50 environment variables in my current session.

kjohnson@Kyles-MacBook-Pro ~ % env | wc -l
      50
kjohnson@Kyles-MacBook-Pro ~ % env | grep PWD
PWD=/Users/kjohnson
OLDPWD=/Users/kjohnson/Patiobar
kjohnson@Kyles-MacBook-Pro ~ % env | grep OLDPWD
OLDPWD=/Users/kjohnson/Patiobar
kjohnson@Kyles-MacBook-Pro ~ % 

kylejohnson avatar Jul 29 '21 14:07 kylejohnson