Crontab fails to run a shell

87 Views Asked by At

there's a command to start up a service which is su -s /bin/bash nuance -c '$JAVA_HOME/bin/java -Dlog4j.configurationFile=$NLPS_HOME/config/log4j2.xml -jar $NLPS_HOME/lib/nlps.jar --spring.config.location=$NLPS_HOME/config/,$NUANCE_DATA_DIR/system/config/User-nlps01.properties watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1 &' I want this command to run at startup in my rhel 7 linux. So I tried crontab, which is @reboot sh /execute/nlp.sh

But it doesn't work, I don't know why. Please tell me what I'm doing wrong I'm so stuck Inside nlp.sh:

#!/bin/bash
/bin/su -s /bin/bash nuance -c '$JAVA_HOME/bin/java -Dlog4j.configurationFile=$NLPS_HOME/config/log4j2.xml -jar $NLPS_HOME/lib/nlps.jar --spring.config.location=$NLPS_HOME/config/,$NUANCE_DATA_DIR/system/config/User-nlps01.properties  watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1 &'
2

There are 2 best solutions below

2
On

You need to define $JAVA_HOME,$NLPS_HOME and $NUANCE_DATA_DIR environment variables are needed to be defined and exported before the execution of the script.

export JAVA_HOME=/path/to/java/home
export NUANCE_DATA_DIR=/path/to/nuance/dir
export NLPS_HOME=/path/to/npls/home
1
On

You have to use double quotes to interpolate variables. Single quotes won't interpolate anything, but double quotes will.

/bin/su -s /bin/bash nuance -c "$JAVA_HOME/bin/java -Dlog4j.configurationFile=$NLPS_HOME/config/log4j2.xml -jar $NLPS_HOME/lib/nlps.jar --spring.config.location=$NLPS_HOME/config/,$NUANCE_DATA_DIR/system/config/User-nlps01.properties  watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1 &"

From Bash manual:

3.1.2.2 Single Quotes

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

3.1.2.3 Double Quotes

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

The special parameters * and @ have special meaning when in double quotes (see Shell Parameter Expansion).