2

I am trying to add a new command to my own shell such as:

alarm 7.15 music.wav

I want to create a cron job so it can schedule alarm daily at given time. So, I create a file for each call of alarm, named musicFile.txt

Here is my bash file to run: (musicFile.txt)

#!/bin/bash 
15 7 * * * XDG_RUNTIME_DIR=/run/user/$(id -u) play -q /home/harun/Desktop/music.wav > alarmFile 
crontab alarmFile 
rm -f alarmFile 
rm -f musicFile.txt 

And I execute this comment with execvp:

bash musicFile.txt

However, nothing happens. How should I set an alarm with crontab?

1 Answers1

4

In my case I run crontab using current user session:

$ crontab -e
          • /usr/bin/play -q /home/user/test/bell.wav

This makes a sound playing every minute.

First try using simpler commands.

If you want to configure alarm for different users, you could use crontab -e from their terminal sessions. To switch to other user terminal session, use su username command.

But, better, instead of using commands with parameters in crontab, use script and specify its path in crontab. Don't forget to chmod +x yourscriptpath and also, don't forget to specify full paths to binaries and check your scripts by executing sh pathtoyour/script.sh

If you could run the next in terminal:

XDG_RUNTIME_DIR=/run/user/$(id -u) play -q /home/harun/Desktop/music.wav > alarmFile 
crontab alarmFile 
rm -f alarmFile 
rm -f musicFile.txt 

Then you could try to add it in your crontab file by crontab -e command execution and by editing it:

15 7 * * * XDG_RUNTIME_DIR=/run/user/$(id -u) play -q /home/harun/Desktop/music.wav > /home/harum/alarmFile && /bin/rm -f /home/harun/alarmFile && /bin/rm -f /home/harum/musicFile.txt 

To check if it works now, just replace 15 7 by * *.

Script creation:

vim.tiny /home/harum/alarm.sh

#!/bin/bash XDG_RUNTIME_DIR=/run/user/$(id -u) play -q /home/harun/Desktop/music.wav > /home/harum/alarmFile && /bin/rm -f /home/harun/alarmFile && /bin/rm -f /home/harum/musicFile.txt

Making script executable:

chmod +x /home/harum/alarm.sh

Adding script to crontab:

crontab -e

15 7 * * * /home/harum/alarm.sh

Finally, as one of commentators suggested, you could use XDG_RUNTIME_DIR in your script by specifying the specific user directly (if you want to run your script as root but use other's user id in your script:

XDG_RUNTIME_DIR=/run/user/$(id yourSpecificUser | awk -F" " '{print $1}' | awk -F"(" '{print $1}' | awk -F"=" '{print $2}')
Gryu
  • 7,559
  • 9
  • 33
  • 52