2

I have 2 commands that need to run every hour, so I put these in /etc/cron.hourly/hrcron file, in following format

command1; command2

It should've worked in my opinion, but does anyone have any idea what's stopping it from running?

I'm running CentOS 6.8.

6

Files placed in /etc/cron.hourly, cron.daily and cron.monthly need to be executables. If you place a text file with a single line as shown in your question into that directory, it cannot be run at all for the same reason that you could not run such a file as a shell script from the command line, either.

What you mean to say is this:

#!/bin/sh
command1
command2

You could concatenate the second and third lines with a semicolon, but it simply isn't necessary here. It's a full-on shell script, so you don't need to "stack" commands in that way.

Also, be sure to mark the script executable, else it still won't run.

If all of this seems odd to you, based on your knowledge of crontab entries, realize that executables in these directories are typically run by either anacron or run-parts, not by cron. Thus, the information from man 5 crontab doesn't really apply here.

| improve this answer | |
  • Another thing one may stumble on is cron not finding the commands. Mine were installed in /usr/local/bin, which is not in the default $PATH. So: 1) make sure the commands are executable, 2) specify the path – troglobit Dec 26 '17 at 13:03
3

The cleanest way to do so is to create two files

  • /etc/cron.hourly/file1 containing command1
  • /etc/cron.hourly/file2 containing command2

I do not recommend having a single cronjob running command1 && command2 because if the first command fails, the second will never execute by design.

Edit: in case you need command1 to be run before command2, use the standard /etc/crontab file and fill it in as such:

0 * * * *  root  command1
5 * * * *  root  command2

This will run command1 at 00 mins each hour and command2 at 05 mins each hour.

| improve this answer | |
  • 1
    Would that guarantee sequential execution of command1 before command2? – Kusalananda Jul 3 '17 at 14:39
  • No. In case you need this you can simply use the standard crontab file, adding entries 0 * * * * command1 and 5 * * * * command2. This will run command1 at 00 mins each hour and command2 at 05 mins each hour. – dr_ Jul 3 '17 at 17:49
  • "if the first command fails, the second will never execute by design." Well, maybe that's why OP specified separation by ; not &&? – user Jul 3 '17 at 20:59
  • @MichaelKjörling The use of && was advised by @l0b0. – dr_ Jul 4 '17 at 7:12

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.