-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 -
NineCattoRules
  • 105
  • 1
  • 1
  • 10
  • 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 05 '17 at 12:15
  • 2
    why? why not create a script you call from crontab and change that script? – Rinzwind Feb 05 '17 at 13:34
  • @Rinzwind but why did you supposed I'm not calling it from crontab? – NineCattoRules Feb 05 '17 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 05 '17 at 17:30
  • This sounds like an [XY Problem](//meta.stackexchange.com/q/66377/271411). What are you actually trying to achieve? – David Foerster Feb 08 '17 at 11:17
  • @DavidFoerster this is already answered, in any case I tried to make a new set of cron jobs from a bash script. I searched a lot and never found something like that. – NineCattoRules Feb 08 '17 at 15:35
  • I'm referring to Rinzwinds comments of why you're trying to manipulate `crontab` programmatically which is much more complicated and fragile than a static `crontab` entry of a script that defers to the actual command invocation depending on whatever environment settings – because it sounds like that's all you want to achieve in the end. This type of question is categorised as "XY Problem". Alas you never clarified what you want to achieve which is imho the reason for the hold on your question. – David Foerster Feb 08 '17 at 16:07
  • @DavidFoerster why it's fragile? Calling a single script to change a single user crontab...the script is executable from root only. You lost me now – NineCattoRules Feb 08 '17 at 16:19
  • 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. – David Foerster Feb 08 '17 at 16:56

1 Answers1

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 -
SYN
  • 2,651
  • 1
  • 11
  • 18
  • "The sample you posted would print current crontab" ...but I wrote `crontab -r`. Also I need to set multiple Cronjobs – NineCattoRules Feb 05 '17 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 05 '17 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? – NineCattoRules Feb 05 '17 at 12:33
  • 1
    didn't notice that, ... good point: it is wrong. Editing my post ... – SYN Feb 05 '17 at 12:34