-1

I am getting a "-bash: syntax error near unexpected token `crontab'" when I am attempting to write a cron job to crontab in one line. (This is for a launch configuration on EC2.) I am following this guide here.

This is my command:

sudo { crontab -l -u ec2-user; echo "* * * * * touch /home/ec2-user/dummy/dummy$ENV"; } | crontab -u ec2-user -

If I run without sudo, it tells me I "must be privileged to use -u." When I run with sudo I get "bash: syntax error near unexpected token `}'"

I am sure I have my syntax wrong with the sudo, but I am not sure where.

Brandon
  • 41
  • 7

2 Answers2

0

sudo can only run external commands, not shell syntax like brace groups (or loops/functions/if-statements/etc). Use it on the individual commands you need special privileges for:

{ sudo crontab -l -u ec2-user; echo "* * * * * touch /home/ec2-user/dummy/dummy$ENV"; } | sudo crontab -u ec2-user -
that other guy
  • 116,971
  • 11
  • 170
  • 194
0

Another way to accomplish what you want:

sudo bash -c "{ crontab -l -u ec2-user; echo '* * * * * touch /home/ec2-user/dummy/dummy$ENV'; } | crontab -u ec2-user -"

This way, you only have to specify sudo once.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439