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?

1
  • Sounds like your shell does not support the <(...) syntax (since those are the only ( in that line). Perhaps your interactive shell is different from the one running the script at boot time? Commented Jan 24, 2013 at 12:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 <(
0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.