0

I have a script that executes perfectly fine manually, though two commands in particular are completely ignored by Cron (this is just a snippet):

sinkint=$(pacmd list-sinks | sed -n '/index\W [1-9]/p' | sed 's/.*://g;s/\W//g')

pacmd set-default sink $sinkint
pacmd set-sink-volume $sinkint 20%

I have a feeling it has something to do with the builtin function "set", so I've tried adding a path and using "env pacmd..." for both of the commands, though no luck. I've also tried "$(pacmd...)", as well as disabling the builtin with "enable -n set" - also tried setting the interpreter to sh instead of bash.

Any help on this would be greatly appreciated, I'm completely lost.

Edit: crontab

* * * * * /path/to/script arg1 >> /path/to/log

Edit: error messages

No PulseAudio daemon running, or not running as session daemon.
No PulseAudio daemon running, or not running as session daemon.
You have to specify a sink name/index and a volume
  • 1
    You're not using the built-in command `set` anywhere. – Barmar Sep 13 '21 at 15:03
  • 1
    Your `.bashrc` and `.profile` aren't executed from `cron`. If you add the directory containing `pacmd` to your `$PATH` there, it won't be available in cron. Use the absolute path of the command. – Barmar Sep 13 '21 at 15:04
  • might have been wise to show my crontab ` PATH=$PATH:/usr/bin * * * * * /path/to/script 1starg >> /path/to/log ` something like this – user492155 Sep 13 '21 at 15:21
  • Yes, if you set `PATH` in the crontab, that should make a difference. – Barmar Sep 13 '21 at 15:22
  • sorry about the sloppy formatting. – user492155 Sep 13 '21 at 15:25
  • Put it in the question, not a comment. – Barmar Sep 13 '21 at 15:26
  • 1
    `/usr/bin` is normally in the default PATH, you shouldn't need to add it. – Barmar Sep 13 '21 at 15:26
  • wouldn't that mean it should execute then? pacmd is in /usr/bin – user492155 Sep 13 '21 at 15:29
  • Yes. So the problem is something else. Redirect stderr to a file so you can see the error messaegs. – Barmar Sep 13 '21 at 15:31
  • So apparently, 'No pulseAudio daemon running, or not running as session daemon' plus - 'you have to specify a sink name/index and a volume' which is a new problem entirely haha. Both of these problems must be false, however - since manual run is fine. I just think crontab is executing the command differently somehow. which is why I thought perhaps "set-" was messing something up? idk – user492155 Sep 13 '21 at 15:43
  • Where is `set -`? And where in your script do you use `$1` to get `arg1`? – Barmar Sep 13 '21 at 15:45
  • BTW, there's no need for two `sed` commands: `sed -n '/index\W [1-9]/s/.*://g;s/\W//gp'` – Barmar Sep 13 '21 at 15:46
  • sorry I'm kind of a beginner. I don't have set anywhere in my script, I was under the assumption crontab was executing 'pacmd set-..." incorrectly, though the stderr would put that out of the question. The argument doesn't seem to pose a problem - mpc add $1. – user492155 Sep 13 '21 at 15:49
  • It may be more helpful to post those error messages in a question in [unix.se] – Barmar Sep 13 '21 at 15:51
  • PulseAudio normally runs as a per-session daemon, and `pacmd` will by default manage an instance associated with the same session it runs in itself. That is not suitable for execution via `cron`, which will run the command in its own session. – John Bollinger Sep 13 '21 at 16:32
  • I suppose that makes sense. Is there anything I could possibly do to allow cron to set sink volume, or is that just a dead end? – user492155 Sep 13 '21 at 16:37
  • 1
    Does this answer your question? [Bash Script works but not in when executed from crontab](https://stackoverflow.com/questions/24146481/bash-script-works-but-not-in-when-executed-from-crontab) And many many many more with the same question, crontab questions like this occur almost once a day. – Nic3500 Sep 13 '21 at 17:26
  • I added a second duplicates which has some nice troubleshooting guidance, plus an answer by yours truly which vaguely explains what's going on here (though it discusses GUI problems, not sound; but the same reasoning should apply here). – tripleee Sep 13 '21 at 17:54

0 Answers0