简体   繁体   English

bash脚本在启动时有效,不在终端中

[英]bash script works on bootup, doesn't in terminal

I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. 我正在Amazon ec2中创建服务器,并将bash脚本作为userdata传递给它,该脚本在服务器首次启动时运行。 It includes a command to add a line to crontab for a user using the answer given here . 它包括一个使用此处给出的答案为用户向crontab添加行的命令。

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. 此脚本在启动过程中似乎工作正常,因为它不显示任何错误消息,并且crotab中安装了cronjob。

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? 我需要解决什么错误?

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. 我错过了设置“目录”变量的权限,但是您的代码对我来说很好用。

It looks like you are using the bourne shell ( /bin/sh ) to execute a bash script. 看来您正在使用bourne shell( /bin/sh )执行bash脚本。 Try using bash instead of sh . 尝试使用bash而不是sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM