1

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.

  • 2
    Just separate the commands with double ampersands: command1 && command2 – l0b0 Jul 3 '17 at 13:03
  • @l0b0 I know syntax with && but semicolon should work too, as it works in terminal. Is it not so? – Sollosa Jul 3 '17 at 13:24
  • 1
    @Sollosa If your problem has been solved by an answer, please indicate so by accepting that answer, by clicking the checkmark outline just below the voting arrows. You can also vote useful answers up. That will indicate to the community that your issue has been resolved. – a CVn Jul 3 '17 at 20:58
5

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.

  • 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.

  • 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. – dr01 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 &&? – a CVn Jul 3 '17 at 20:59
  • @MichaelKjörling The use of && was advised by @l0b0. – dr01 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.