-1

I need to create a bash script in order to:

  1. Empty my root Crontab;
  2. Insert new Cronjobs via bash script.

For the first point I can use crontab -r

For the second point instead here I found this script:

#!/bin/bash

lines="* * * * * /path/to/command"
(crontab -u root -l; echo "$lines" ) | crontab -u root -

How can I cook this together in a bash script?

Something like this:

#!/bin/bash 

crontab -r 

line="* * * * * /path/to/command; 
      * * * * * /path/to/command2;
      * * * * * /path/to/command3" 

(crontab -u root -l; echo "$line" ) | crontab -u root -
9
  • Have you tried your example, and found it lacking? Otherwise I'm not sure what you're asking, since you already have the script.
    – muru
    Feb 5, 2017 at 12:15
  • 2
    why? why not create a script you call from crontab and change that script?
    – Rinzwind
    Feb 5, 2017 at 13:34
  • @Rinzwind but why did you supposed I'm not calling it from crontab? Feb 5, 2017 at 15:33
  • Because is is far less complicated? a script is easier to maintain; create 10 scripts, and copy 1 of them over the one you activate from cron and you changed your script to start by cron. Scripts can have their own user so no need for root.
    – Rinzwind
    Feb 5, 2017 at 17:30
  • 1
    In general, changing configuration files "dynamically" is not a good idea and you should avoid it if possible for various reasons, among other reasons because the program may break the configuration file and the source of the change would be hard to track. The configuration is only supposed to change due to manual user intervention. In this case the configuration option invokes an arbitrary command, so it would be more straight-forward to use a static command that defers to a "dynamic" command depending on some other environment state. Feb 8, 2017 at 16:56

1 Answer 1

1

The sample you posted would print current crontab and inject new directives.

If you intend to just inject new directives, wiping the current crontab, instead of your

lines="* * * * * /path/to/command"
( crontab -u root -l; echo "$lines" ) | crontab -u root -

Go with:

lines="* * * * * /path/to/command"
echo "$lines" | crontab -u root -

And, as you pointed it out in the comments, it is wrong, adding multiple crons, to use semicolons as a separator. You can go with:

lines=" line1
line2"

Or:

crontab -u root - <<EOF
line1
line2
EOF

Or:

(
    echo line1
    echo line2
) | crontab -u root -
4
  • "The sample you posted would print current crontab" ...but I wrote crontab -r. Also I need to set multiple Cronjobs Feb 5, 2017 at 12:25
  • 1
    Indeed, your second snipped does a crontab -r. Although it is unclear which crontab is wiped. Point is, you don't need it, neither would you need to list (-l) if you intend to inject a new tab.
    – SYN
    Feb 5, 2017 at 12:28
  • is it correct how I setup multiple Cronjobs? (every line separated by semicolon) On command crontab -u root do I need to declare my root password? Feb 5, 2017 at 12:33
  • 1
    didn't notice that, ... good point: it is wrong. Editing my post ...
    – SYN
    Feb 5, 2017 at 12:34

Not the answer you're looking for? Browse other questions tagged .