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.sh
While 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/error
But that showed no output. What am I doing wrong?
Solution 1:
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.
Solution 2:
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/1001
It will probably be the same on yours, but check first. Now, your script will run every hour and should work as expected.
Solution 3:
A script ending with .sh
is not executed in /etc/cron.hourly
folder:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=308911
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/cron.hourly/Hour-sound.sh /etc/cron.hourly/Hour-sound
or put the script-call into
/etc/crontab
which allows the .sh
ending
Solution 4:
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 abc
command to make executable file.
edit etc/cronrtab file
there has a predefined few lines.edit from your minutes to hours line and save it.
then it will run properly.