Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top