2

I'm trying to add a cron job without actually opening vim or nano. Something like this:

$ crontab -e << echo '* 0/10 * * *  some command'

I'm seeing stuff online like this and this, but honestly it's kinda confusing.

Zanna
  • 69,223
  • 56
  • 216
  • 327
Laura
  • 201
  • 2
  • 9

1 Answers1

4

First create make a copy of your user's crontab file using:

crontab -l > ${USER}_crontab

then you can easily work with username_crontab as it's a normal file, edit it or redirect anything to it in different ways.

For example append a new job:

echo '* 0/10 * * *  some command' >> ${USER}_crontab

then install the file using:

crontab ${USER}_crontab

You can also do it all in once like this:

cat <<< "* * * * *  cmd1" > my_jobs; crontab my_jobs

or:

cat > my_jobs <<E                   
* * * * *  cmd1
* * * * *  cmd2
E

crontab my_jobs
Ravexina
  • 54,268
  • 25
  • 157
  • 179
  • Thanks for these solutions! This is interesting. Both methods require we write (>) to a file first before adding a cron. Is it not possible then without first writing to a separate file? – Laura Sep 27 '18 at 10:02
  • You can write to any file and then install it using `crontab file_name` like the last two solution. – Ravexina Sep 27 '18 at 10:05
  • Sorry if my english is bad. I'm asking if its possible to do without writing to file. – Laura Sep 27 '18 at 10:06
  • 1
    There is no way that I'm aware of... when you are using `crontab -e` you are actually asking for an editor to edit your crontab file. – Ravexina Sep 27 '18 at 10:13