13

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?

  • just making sure, did you make the script executable? owned by root? what happens when you run the script from terminal or put a simple script there? does /etc/crontab has the run-parts line for running hourly cron jobs from the directory? – heemayl Apr 19 '15 at 17:47
  • Executable: yes, running the script goes well nothing wrong from the /etc/cron.hourly directory, don't know how to check this?is it in the Startup Applications? @heemayl – Black Block Apr 19 '15 at 17:54
  • 1
    Does the /etc/crontab file have the line 17 * * * * root cd / && run-parts --report /etc/cron.hourly ? – heemayl Apr 19 '15 at 17:58
  • Here @heemayl mediafire.com/view/7tqg7cr24xc7q0y/Selection_016.png – Black Block Apr 19 '15 at 20:54
  • 1
    Please don't post screenshots of text. Especially when that text is displayed in a transparent terminal window and includes distracting colors. – terdon Apr 27 '15 at 12:42
22

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.

  • 1- Here is my files: mediafire.com/view/rg8r85xb7qw26zs/cron.hourly_017.png 2- Did you try any script in /etc/cron.hourly and worked with you? – Black Block Apr 19 '15 at 22:46
  • 2
    I just tested a litte script that just wrote "Hello" to a file in /tmp, and it worked without a problem. Just in case, you are aware that the scripts in /etc/cron.hourly are not necessarily executed "on the hour"? If your cronjob in /etc/crontab has 17 at the minutes position, the scripts are executed at 00:17, 01:17 and so forth. And again just in case, the script's permissions allow the user root to execute them? – Henning Kockerbeck Apr 19 '15 at 23:24
  • Yah but that script of mine never runs at any time. and about the permission see here: mediafire.com/view/psnb4bflawdlyly/… – Black Block Apr 20 '15 at 10:39
  • Maybe it's actually a permission problem. I know that scripts in the more general /etc/cron.d/ must not have write or execute permissions for "group" or "others". "owner" needs execute permission, "group" and "others" must have only read permission. Maybe that applies to /etc/cron.hourly as well, though I didn't test that. – Henning Kockerbeck Apr 20 '15 at 10:59
  • Ok then i have changed the permission for my file mediafire.com/view/s2lq63u7z54ohbo/… that makes the group which is the root group read only and others, so is this going to make any problems? – Black Block Apr 20 '15 at 11:45
5

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.

  • Is that what you mean mediafire.com/view/ucjpvu8b41n3hue/… . this didn't work for me. – Black Block Apr 21 '15 at 14:25
  • I think if the problem was in the notify-send then why other command didn't run as well (like mplayer)? – Black Block Apr 21 '15 at 14:26
  • @BlackBlock there are multiple problems. i) notify-send needs the XAUTHORITY and DISPLAY variables to be set and ii) mplayer needs the XDG_RUNTIME_DIR to be set. Did you try my suggestion? That should fix both issues. You probably also had naming problems because of run-parts as explained in the other answer. – terdon Apr 21 '15 at 14:55
  • Then why this didn't work for me ? i'm gonna be mad! see my image in 1st comment on your answer – Black Block Apr 21 '15 at 14:57
  • @BlackBlock no! You are editing /etc/crontab, don't do that. You want this to be run as your user, not as root and that file has a different format anyway. Just do what I said in my answer, run crontab -e and add the lines there. – terdon Apr 21 '15 at 15:08
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

1

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.

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.