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?

2
  • How do you edit your crontab? Do you use crontab -e or sudo crontab -e? Do you want to run this alarm only for one user or for each user that could be logged in?
    – Gryu
    Mar 2, 2020 at 12:47
  • If @Gryu's answer doesn't work for whoever ends up here, check this answer on SO. Sep 7, 2020 at 18:32

1 Answer 1

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}')
10
  • Thank you, but why I cannot run my bash file? Or how should I prepare a bash file to schedule a crontab like your example? Mar 2, 2020 at 13:44
  • @HARUNSASMAZ check answer. I've updated it.
    – Gryu
    Mar 2, 2020 at 13:50
  • How should I run the first part you mentioned? Starting with XDG. Just line by line on command line? May I create a bash file and run directly? Mar 2, 2020 at 13:58
  • updated answer you could try
    – Gryu
    Mar 2, 2020 at 14:02
  • You are great! Thanks a lot Mar 2, 2020 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .