4

I was trying to use my pi and a bluetooth speaker as an alarm clock.

I've setup a cronjob by doing

sudo crontab -e

in the crontab file, I added

40 8 * * 1-5 /home/XXXXX/alarm.sh &> alarm.log

the alarm.sh script is as following:

#!/bin/bash
sudo hciconfig hci0 up
bluez-test-audio connect MAC_OF_SPEAKER
bluez-test-device trusted MAC_OF_SPEAKER yes
mplayer -ao alsa:device=clock MY_MUSIC.m4a

The problem now is that the cronjob won't work. If I run the script manually, the speaker plays the .m4a file correctly. But cronjob won't execute at 08:40 in the morning.

Please give me some advice.

Thanks

2

OK, as @joan mentioned in the comment, I need to "provide full path to files in cron jobs."

Cron jobs were executed, but since I didn't provide full path to alarm.log, it is not in my user home directory.

So what I did to fix the problem:

1.change cronjob to

40 8 * * 1-5 /home/XXXXX/alarm.sh > /home/XXXXX/alarm.log 2>&1

redirect output was not necessary, but it helped me to find out where went wrong.

2.change the alarm.sh script to

#!/bin/bash
sudo hciconfig hci0 up
bluez-test-audio connect MAC_OF_SPEAKER
bluez-test-device trusted MAC_OF_SPEAKER yes
mplayer -ao alsa:device=clock FULL_PATH_TO_MY_MUSIC/MY_MUSIC.m4a //full path is needed
bluez-test-audio disconnect MAC_OF_SPEAKER

The music played as scheduled.

| improve this answer | |
0

Try restarting the cron service with

sudo service cron restart

after editing your cronjob.

If this doesn't help, try using a simple echo bash script as a cronjob.

| improve this answer | |
  • No, it doesn't work. The echo script was not executed... – Eniaczz Jun 25 '15 at 1:14
  • Try: 40 8 * * 1-5 /bin/bash /home/XXXXX/alarm.sh &> alarm.log – scherzkrapferl Jun 25 '15 at 1:40
  • OK, I added */1 * * * * ~/echo_hello.sh > echo.log, the file echo.log was created and has "hello" in it. But if I just register ~/echo_hello.sh, nothing gets printed on the screen. – Eniaczz Jun 25 '15 at 1:51
  • I checked my own cronjobs and found out that I always specified "/bin/bash" like mentioned above. Another possibility that could help is using "crontab -e" instead of "sudo crontab -e". – scherzkrapferl Jun 25 '15 at 1:56
  • 1
    @Eniaczz You should always give full paths to files in cron jobs. For instance mplayer -ao alsa:device=clock MY_MUSIC.m4a assumes that MY_MUSIC.m4a is in the current directory. I have no idea what the default current directory is for a cron job, it's not the directory containing MY_MUSIC.m4a though. – joan Jun 25 '15 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.