I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e
but that will open an editor to edit the current crontab. I want to do this programmatically.
Is it possible to do so?
I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e
but that will open an editor to edit the current crontab. I want to do this programmatically.
Is it possible to do so?
I have written a crontab deploy tool in python: https://github.com/monklof/deploycron
Install your crontab is very easy, this will merge the crontab into the system's existing crontab.
In Ubuntu and many other distros, you can just put a file into the
/etc/cron.d
directory containing a single line with a valid crontab entry. No need to add a line to an existing file.If you just need something to run daily, just put a file into
/etc/cron.daily
. Likewise, you can also drop files into/etc/cron.hourly
,/etc/cron.monthly
, and/etc/cron.weekly
.As a correction to those suggesting
crontab -l | crontab -
: This does not work on every system. For example, I had to add a job to the root crontab on dozens of servers running an old version SUSE (don't ask why). Old SUSEs prepend comment lines to the output ofcrontab -l
, makingcrontab -l | crontab -
non-idempotent (Debian recognizes this problem in the crontab manpage and patched their version of Vixie Cron to change the default behaviour ofcrontab -l
).To edit crontabs programmatically on systems where
crontab -l
adds comments, you can try the following:EDITOR=cat crontab -e > old_crontab; cat old_crontab new_job | crontab -
EDITOR=cat
tells crontab to usecat
as an editor (not the usual default vi), which doesn't change the file, but instead copies it to stdout. This might still fail ifcrontab -
expects input in a format different from whatcrontab -e
outputs. Do not try to replace the finalcrontab -
withcrontab -e
- it will not work.Well
/etc/crontab
just an ascii file so the simplest is to justwhich will add a job which will email you every 15 mins. Adjust to taste, and test via
grep
or other means whether the line was already added to make your script idempotent.On Ubuntu et al, you can also drop files in
/etc/cron.*
which is easier to do and test for---plus you don't mess with (system) config files such as/etc/crontab
.Even more simple answer to you question would be:
You can setup cronjobs on remote servers as below:
In Linux, the default location of the
crontab
file is/var/spool/cron/
. Here you can find thecrontab
files of all users. You just need to append your cronjob entry to the respective user's file. In the above example, the root user's crontab file is getting appended with a cronjob to run/root/test.sh
every day at 1 AM.For user crontabs (including root), you can do something like:
where the file named "filename" contains items to append. You could also do text manipulation using
sed
or another tool in place ofcat
. You should use thecrontab
command instead of directly modifying the file.A similar operation would be:
If you are modifying or creating system crontabs, those may be manipulated as you would ordinary text files. They are stored in the
/etc/cron.d
,/etc/cron.hourly
,/etc/cron.daily
,/etc/cron.weekly
,/etc/cron.monthly
directories and in the files/etc/crontab
and/etc/anacrontab
.