0

I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. It includes a command to add a line to crontab for a user using the answer given here.

directory="/home/intahwebz/current/tools/amazon/"
command="cd $directory && sh backupSQLToS3.sh"
job="15 1 */2 * * $command"
cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -

This script appears to work fine during bootup as it displays no error messages and the cronjob is installed in the crotab.

However I'd also like the script to run during server upgrades. Attempting to run the script from the command line gives the error:

installCrontab.sh: line 14: syntax error near unexpected token `('
installCrontab.sh: line 14: `cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -'

What do I need to fix this error?

Community
  • 1
  • 1
Danack
  • 23,022
  • 14
  • 78
  • 112

2 Answers2

1

your approach is working perfectly for me:

$ whoami
test

$ echo $SHELL
/bin/bash

$ command="cd $directory && sh backupSQLToS3.sh"

$ job="15 1 */2 * * $command"

$ crontab -l


$ cat <(fgrep -i -v "$command" <(crontab -u test -l)) <(echo "$job") | crontab -u test -

$ crontab -l

15 1 */2 * * cd  && sh backupSQLToS3.sh

I missed to set the "directory" variable but your code works fine for me.

slayedbylucifer
  • 20,920
  • 15
  • 84
  • 119
  • Thanks - it worked for me when I did it from the command line, and so I copied and pasted everything into a new script file and it worked there as well. Comparing the two scripts showed I had somehow gotten an extra space in the middle of the first – Danack Jan 24 '13 at 13:28
0

It looks like you are using the bourne shell (/bin/sh) to execute a bash script. Try using bash instead of sh.

FatalError
  • 47,677
  • 13
  • 93
  • 113
  • I'm calling the script as 'bash installCrontab.sh' and it has #!/bin/bash at the top, so I'm pretty sure it's using Bash. – Danack Jan 24 '13 at 12:54