Home > Software design >  How to grep the output of a command inside a shell script when scheduling using cron
How to grep the output of a command inside a shell script when scheduling using cron

Time:12-30

I have a simple shell script where I need to check if my EMR job is running or not and I am just printing a log but it does not seem to work properly when scheduling the script using cron as it always prints the if block statement because the value of "status_live" var is always empty so if anyone can suggest what is wrong here otherwise on manually running the script it works properly.

#!/bin/sh

status_live=$(yarn application -list | grep -i "Streaming App")

if [ -z $status_live ] 
then
  echo "Running spark streaming job again at: "$(date) &
else
  echo "Spark Streaming job is running, at: "$(date)
fi

CodePudding user response:

# $? will have the last command status in bash shell scripting
# your complete command here below and status_live is 0  if it finds in grep (i.e. true in shell if condition.)
yarn application -list | grep -i "Streaming App"
status_live=$?
echo status_live: ${status_live}
if [ "$status_live" -eq 0 ]; then
    echo "success
else
    echo "fail" 
fi

CodePudding user response:

Your script cannot run in cron because cron script has no environment context at all.

For example try to run your script as another use nobody that has no shell.

 sudo -u nobody <script-full-path>

It will fail because it has no environment context.

The solution is to add your user environment context to your script. Just add source to your .bash_profile

 sed -i "2a source $HOME/.bash_profile" <script-full-path>

Your script should look like:

#!/bin/sh
source /home/<your user name>/.bash_profile

status_live=$(yarn application -list | grep -i "Streaming App")

if [ -z $status_live ] 
then
  echo "Running spark streaming job again at: "$(date) &
else
  echo "Spark Streaming job is running, at: "$(date)
fi

Now try to run it again with user nobody, if it works than cron will work as well.

 sudo -u nobody <script-full-path>

Note that cron has no standard output. and you will need to redirect standard output from your script to a log file.

<script-full-path> >> <logfile-full-path>
  • Related