72

Ok, So I've made my script, I dropped it in /etc/cron.hourly then I chmod 777 the file but it won't run (automatically). If I manually run it, it works fine. Do I need to do anything else?

Rinzwind
  • 299,756
Switchkick
  • 3,883

6 Answers6

188

Entries in cron.hourly are run by the run-parts mechanism (man run-parts for more info). And run-parts is choosy about what filenames it considers valid.

For example, giving your script an extension will make it invalid and result in the job not being run. [a-zA-Z0-9_-] are the valid characters, so the '.' makes it invalid.

When adding a job to /etc/cron.hourly ( or .daily, .weekly, etc), always test afterwards that run-parts will actually run it by issuing the command:

run-parts --test /etc/cron.hourly

If by running this command it shows your file it means it worked. Otherwise, if doesn't show anything your file name is not valid.

What was the name of your script?

muru
  • 197,895
  • 55
  • 485
  • 740
DaithiF
  • 2,451
  • There are two: cerebro_random_response.sh and Cerebro Temperature.sh no good? – Switchkick May 14 '11 at 00:41
  • 27
    correct, no good :) [a-zA-Z0-9_-] are the valid characters, so the '.' makes it invalid. This is what i meant above by 'giving your script an extension will make it invalid' – DaithiF May 19 '11 at 16:43
  • 15
    oMG, This just saved a huge headache. THANK YOU! giving your script an extension will make it invalid and result in the job not being run – Jonathan S. Fisher Dec 05 '13 at 22:16
  • You may also use --report to actually run the scripts. – Pablo Bianchi Feb 05 '18 at 16:24
  • 3
    Thanks for this info. My issue was that the script wasn't executable, chmod +x /etc/cron.hourly/myscript did the trick and run-parts listed it as expected. – Yvan Nov 08 '19 at 10:08
  • 3
    That's pretty annoying, especially since cron.hourly directory has a dot in its name, which suggests that dot is okay – galets Dec 19 '20 at 19:40
  • 2
    a fundmantal tenet of UX is "don't surprise the user". Why on earth would a . not be a valid character for something running under run-parts? What kind of incompetent developer makes such choices? – JayEye Apr 01 '21 at 07:40
  • This works for me. – hsfzxjy Sep 06 '21 at 06:24
  • 1
    @Yvan you are correct. For others: please note, that chmod u+x is not sufficient even if root owns it and process is executed as root it really has to be chmod +x – Martin Mucha May 25 '22 at 20:22
  • @JayEye ... especially, if naming is pointless and using hidden/dot files would be just enough and would work as expected, especially if even they use dotted file as a placeholder. – Martin Mucha May 25 '22 at 20:23
  • No file extensions are allowed inside this folder. Good trick :) – Maf Jun 27 '22 at 00:17
  • If the answer above did not solve your problem (you, viewer of this comment), I've aggregated a list of things to check that may help you: https://unix.stackexchange.com/a/487471/307715 – Adrian B. Jun 05 '23 at 22:10
12

Why not using crontab ( /etc/crontab ) and use */1 in the hour field. I have used this to run a script every 5 min and it works well:

# m h dom mon dow user  command
* */1  * * *   user    command
oli206
  • 360
  • 7
    Using cron.daily and friends works a bit better if the machine is not running all the time, because anacron tries to approximate the right schedule whereas plain cron will just not run them if the machine is not on all night. For hourly jobs this probably doesn't matter so much. – poolie Dec 02 '10 at 00:20
  • 10
    I really appreciate the next answer, as it addresses the problem with cron.hourly, rather than finding a workaround. – tishma Jan 24 '14 at 14:24
  • 5
    This is not an answer to the question – josh123a123 Nov 16 '16 at 15:48
  • @poolie As can be seen on /etc/crontab: cron.daily, cron.weekly and cron.monthly run with anacron (if available) but hourly run-parts --report /etc/cron.hourly. – Pablo Bianchi Feb 05 '18 at 15:24
  • Copy a script into specific directory or just delete it inside, it's much easier to deploy. – Shawyeok Sep 15 '22 at 11:32
8

DaithiF's answer should be the right answer.

Also, my script didn't have #!/bin/bash in the first line. Even though the script could be executed with the command line, run-parts rejected it saying "Exec format error".

Changing the file name from scriptname.sh to scriptname and adding the #!/bin/bash into first line enabled my script to run hourly.

Videonauth
  • 33,355
  • 17
  • 105
  • 120
Ben Lin
  • 297
  • 2
  • 3
5

Your problem is probably down to the overly open permissions, which allows anybody to edit your file. Try 755 instead.

Looking in the cron entries in your syslog output should confirm this.

Cry Havok
  • 1,322
2

In my case I made these errors due to which cron.hourly wont execute my script:

  1. I forgot to check if crond service is running: systemctl status crond

  2. I haven't added new empty line in the very end of my script file like cron's scripts in other folders have.

  3. I created my script file using WinSCP on Windows thus inserted Windows-like EOL symbol, which cron can't interpret correctly. So I needed to execute sed -i -e 's/\r$//' /etc/cron.hourly/myscript to fix it.

Kyo
  • 21
-1

When you run

crontab -l

is this task on the list?

if not, add it

crontab -e

add this line

0 * * * * yourScript

if it is in this list, try to add the path of programing language to the top of your script

Example:

bash: #!/bin/bash

This 2 things always solved my problems :)

Wolfy
  • 7,920
  • Done it all and the script is good, it works like a charm. It's in the crontab list too but no hourly task runs. Thanks anyway :( – Switchkick Oct 20 '10 at 08:17