2

I'm writing a bash script which, among other things, will edit crontab on another server. The way I figured out how to do this is with:

crontab -l | sed <stuff> | crontab -

It does what I need it to do, but I'm still not sure how. What exactly does "crontab -" do? When I run it by itself from the shell, it takes over the shell until I hit ctl+c, but doesn't seem to do anything. Is its only purpose to overwrite cron contents with whatever's passed from stdin? I can't seem to find any documentation on it.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Ben C.
  • 135

1 Answers1

5

One syntax for crontab is

crontab <file>

Per Usage of dash (-) in place of a filename your usage of the - is to take stdin which in this case is the stdout coming from sed which is then feeding in to replace the <file> argument and replace the contents of cron instead of from the file you are giving it the stdin that is acting as that file.

WEBjuju
  • 506
  • 1
    This is exactly the information I was looking for. Makes sense now why I couldn't find it in the cron documentation. Thanks! – Ben C. Jan 15 '18 at 15:51
  • One final missing part is to note that the crontab command does not replace any of its cron data until it has received everything from stdin. (So if you start typing, and then Ctrl/C out, you haven't lost the previous/current data.) This is peculiar to crontab and the idiom cat <file | sed ... | cat >file does not hold true in the general sense. – Chris Davies Jan 16 '18 at 13:41