1

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?

  • 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 at 12:47
  • If @Gryu's answer doesn't work for whoever ends up here, check this answer on SO. – André Yuhai Sep 7 at 18:32
1

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}')
| improve this answer | |
  • 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? – HARUN SASMAZ Mar 2 at 13:44
  • @HARUNSASMAZ check answer. I've updated it. – Gryu Mar 2 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? – HARUN SASMAZ Mar 2 at 13:58
  • updated answer you could try – Gryu Mar 2 at 14:02
  • You are great! Thanks a lot – HARUN SASMAZ Mar 2 at 14:05

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.