Reputation: 12487
This is the command I want to run:
00 03 * * * backup.sh
I understand that this will run the script backup.sh at 3am every morning. How can I add this cron command on my linux server using a bash script?
Upvotes: 2
Views: 2370
Reputation: 286
As root:
echo "00 03 * * * root backup.sh" >>/etc/crontab
or
echo "00 03 * * * root backup.sh" >/etc/cron.d/mybackupjob
As your own user:
crontab -l >tmp; echo "00 03 * * * backup.sh" >>tmp; crontab tmp; rm tmp
Upvotes: 1
Reputation: 55382
crontab -e
will attempt to invoke your EDITOR
, so your first script could set this to a second script which just has to append the line in question to the crontab:
#!/bin/sh
EDITOR=/path/to/second/script crontab -e
Second script:
#!/bin/sh
echo "00 03 * * * /path/to/bin/backup.sh" >> $1
Upvotes: 0
Reputation: 311288
How can I add this cron command on my linux server using a bash script?
If you want to run this as root, you could place a file in /etc/cron.d
named backup
with the following contents:
00 03 * * * root backup.sh
This assumes that backup.sh
is in the standard PATH
, you probably want to use a fully qualified path here instead of relying on PATH
:
00 03 * * * root /path/to/bin/backup.sh
On many distributions, you could also place (probably via a symlink) the backup.sh
script into something like /etc/cron.daily
and it would run every night. This is often simpler and more maintainable than writing your own crontab entries.
If you want to run this as a user, you can run...
crontab -e
...to edit your own crontab file and adding the entry there:
00 03 * * * /path/to/bin/backup.sh
Note that there we don't need to specify a user name (that's only necessary in /etc/cron.d
, /etc/crontab
, and other global system locations.
Upvotes: 2
Reputation: 22675
You know about the /etc/cron.d directory, right? If not, type 'man cron'.
Upvotes: 0