Why putting a script in /etc/cron.hourly is not working?
I put an executable script in /etc/cron.hourly, but that script didn't run every hour actually it never runs at all.
Here is the script(Hour-sound) that i made:
#!/bin/bash mplayer ~/Music/sfx_msg-highlight.wv &> /dev/null & spd-say -r -50 -p 50 -t male3 "The time now is $(date +"%l %p")" notify-send "It's: " "$(date +"%l %p") now." -i ~/Pictures/"first tee.png" -t 5000
My crontab is:
DISPLAY=":0.0"
XAUTHORITY="/home/naruto/.Xauthority"
XDG_RUNTIME_DIR="/run/user/1000"
0 * * * * /home/naruto/Hour-sound.shWhile the mplayer and spd-say commands are run correctly, the notify-send is not. I also tried redirecting error to a file:
0 * * * * /home/naruto/Hour-sound.sh 2>/tmp/errorBut that showed no output. What am I doing wrong?
75 Answers
As you've probably seen in the comments to your question, the cronjobs in /etc/cron.hourly (and the other, similar directories) are executed by run-parts. run-parts is a little picky about filenames. By default it doesn't execute files whose filenames contain anything other than (all of those from ASCII)
- uppercase letters
- lowercase letters
- digits
- underscores
- dashes ("minus signs")
So if your script has a filename of for example "myscript.sh", it just is ignored, because run-parts does not like the dot.
One problem is that you're trying to run a graphical application (notify-send) from cron. That takes a little tweaking. You need to set XAUTHORITY and DISPLAY variables in the crontab so that it can connect to your running X session and you need to set XDG_RUNTIME_DIR so it can connect to your pulseaudio session. Unfortunately, these need to be set in the crontab itself, so you can't use /etc/cron.hourly. Instead, run crontab -e and add these lines:
DISPLAY=":0.0"
XAUTHORITY="/home/YOURUSERNAME/.Xauthority"
XDG_RUNTIME_DIR="/run/user/1000"
0 * * * * /path/to/script.sh Change the value of XDG_RUNTIME_DIR to whatever is returned when you echo them from a terminal. On my system, this is:
$ echo $XDG_RUNTIME_DIR
/run/user/1001It will probably be the same on yours, but check first. Now, your script will run every hour and should work as expected.
9A script ending with .sh is not executed in /etc/cron.hourly folder:
Links or binaries inside a directory handled by run-pars (like /etc/cron.daily) will not run if a period is part of their name.
rename the script
mv /etc/ /etc/or put the script-call into
/etc/crontabwhich allows the .sh ending
Don't use script with extension (abc.sh) and add your need place. (cron.hours).
add your code stuff to abc file and save.(for your need) (this should be bash command)
use
sudo chmod +x abccommand to make executable file.
edit etc/cronrtab filethere has a predefined few lines.edit from your minutes to hours line and save it.
then it will run properly.
An addition to the accepted answer.
You can test your scripts under /etc/cron.* first to ensure it will be executed.
Example, you have this script: /etc/. Execute the below command to check if it is accepted by run-parts:
run-parts --test /etc/cron.hourlyThe output should be something like this:
/etc/
/etc/
...If your script is listed in the output, then you're good to go.