6

I tried the below command and crontab stopped running any jobs: echo "@reboot /bin/echo 'test' > /home/user/test.sh"| crontab -

What is the correct way to script adding a job to crontab in linux?

Greg
  • 6,043
  • 8
  • 41
  • 93
  • You overwrote your `crontab` You need to pull the existing entries with `crontab -l > /tmp/my.cron` then append the new cron to this file with `echo "@reboot /bin/echo 'test' > /home/user/test.sh" >> /tmp/my.cron` then finally re-read the file back into crontab with `crontab < /tmp/my.cron`. – alvits Feb 14 '17 at 01:51
  • 1
    Or if you prefer a single line `(crontab -l; echo "@reboot /bin/echo 'test' > /home/user/test.sh") | crontab -`. – alvits Feb 14 '17 at 01:52
  • 1
    Does this answer your question? [How to create a cron job using Bash automatically without the interactive editor?](https://stackoverflow.com/questions/878600/how-to-create-a-cron-job-using-bash-automatically-without-the-interactive-editor) – wesinat0r Jul 08 '20 at 22:48

3 Answers3

8

I suggest you read Cron and Crontab usage and examples .

And you can run this:

➜ ( printf -- '0 4 8-14 * *  test $(date +\%u) -eq 7 && echo "2nd Sunday"' ) | crontab
➜  crontab -l
0 4 8-14 * *  test $(date +\0) -eq 7 && echo "2nd Sunday"            

Or

#!/bin/bash
cronjob="* * * * * /path/to/command"
(crontab -u userhere -l; echo "$cronjob" ) | crontab -u userhere -

Hope this helps.

McGrady
  • 8,768
  • 11
  • 35
  • 57
  • Tried this approach and it returns `new crontab file is missing newline before EOF, can't install.` – Greg Feb 13 '17 at 14:58
  • `crontab: usage error: file name must be specified for replace ` – Pedro Lobito Jun 02 '20 at 00:22
  • @Greg You have an error because you did not add `-` as did not the author in the first command at the end as it is in the second example. Which means, get input from the pipe. – papo Jan 13 '21 at 09:32
  • @Pedro Correctly there should always be a new line at the end of a config file. Sometimes it does not matter. printf does not add it, I dont know why there is printf used and not echo. Just replace `printf --` with `echo`. It adds newline. – papo Jan 13 '21 at 09:35
  • 1
    This is the only correct way how to add new jobs from scripts (not using crontab -e). It's pitty @McGrady did not add some description. It's not documented that crontab binary can take text from pipe. First command: "Do this to create new table for current user while existing jobs are removed." Second: "specify different than current user by -u if needed. To add to existing jobs, list current jobs by crontab -l, add new one and pipe it all to crontab" No bash needed, #!/bin/sh is enough. Also take a note of my previous comments. – papo Jan 13 '21 at 09:37
0

Late answer, but on CentOS I create a new cronjob (for root, change user as needed) from a bash script using:

echo "@reboot command..." >> /var/spool/cron/root

>> will force appending to existing cronjobs or create a new cronjob file and append to it if it doesn't exist.

Pedro Lobito
  • 75,541
  • 25
  • 200
  • 222
0

Im not sure about this but try this one

echo "* * * * * whatever" > /etc/crontabs/root

then check the "crontab -e" you will see your command there

For those who are using alpaine distribution , do not forget to call "crond" to make your crons start

Mhmd
  • 198
  • 1
  • 9