Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crontab Editing in a shell script

Status
Not open for further replies.

tekpr00

IS-IT--Management
Jan 22, 2008
186
CA
Hello All:

I have a shell script that should open a crontab and add a new line and save after. However I cannot make it work. Any suggestion will be appreciated.

cron.sh
crontab -l > oracle.cron.20100509
newline="the contents of the new crontab line"
(crontab -l; echo "$newline") | crontab -

however the above gave me an error:
$ ./cron.sh
crontab: can't open your crontab file.
./cron.sh: line 2: 1177 Broken Pipe crontab -l

Please help
 
Hi

There are two [tt]crontab[/tt] instances running in the same time, one is reading the crontab file, the other is trying to write it. But of course fails.

Fortunately you are creating a copy of the crontab file. So use that instead :
Code:
crontab -l [teal]>[/teal] oracle[teal].[/teal]cron[teal].[/teal][purple]20100509[/purple]
[navy]newline[/navy][teal]=[/teal][green][i]"the contents of the new crontab line"[/i][/green]
[teal]([/teal][highlight]cat oracle[teal].[/teal]cron[teal].[/teal][purple]20100509[/purple][/highlight][teal];[/teal] echo [green][i]"$newline"[/i][/green][teal])[/teal] [teal]|[/teal] crontab -

Feherke.
 
Another approach using the line-based ex editor:

Code:
crontab -l > oracle.cron.$(date +%Y%m%d)

echo '$a
00 05 * * * /usr/bin/true a new cron job
.
wq' | EDITOR=ex crontab -e

Annihilannic.
 
Why not this?
Code:
NEWJOB="0 12 * * * /path/to/newjob parameters"

crontab -l > crontab.dat
cat $NEWJOB >> crontab.dat
crontab crontab.dat
Simple, no editing to get working correctly. Just get the current crontab, append the line, then make it the current crontab.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top