0

I have a cgi script running but I am having issues with the cgi script not waiting for all of the output of the commands.

Here is the cgi script:

#!/bin/bash
echo Content-type: text/plain
echo ""

case "$QUERY_STRING" in
  start)
    service servicename start
    ;;
  stop)
    service servicename stop
    ;;
  restart)
    service servicename restart
    ;;
  backup)
    service servicename backup
    ;;
  checkisup)
    service servicenamet checkisup
    ;;
  status)
    service servicename status
    ;;
  *)
  echo "Usage: $0 {checkisup|start|stop|restart|backup|status|restart}"
  exit 1
  ;;
esac

exit 0

When i call the status with script.cgi?status

it executes the status function form my service

  if pgrep -u $USERNAME -f $SERVICE > /dev/null
  then
    pre_log_len=`wc -l "$PATH/server.log" | awk '{print $1}'`
    echo "$SERVICE is running... executing command"
    as_user "screen -p 0 -S screenname -X eval 'stuff \"$command\"\015'"
    sleep .1
    tail -n $[`wc -l "$PATH/server.log" | awk '{print $1}'`-$pre_log_len] "$PATH/server.log"
  fi

The part i am having the issue with is when the function gets to sleep .1 the cgi script will just continue without waiting for the output of tall.

Any ideas?

1
  • sleep .1, maybe good for a working system, but can't help in debugging this situation. Try sleep 10 and see if that changes things. Good luck! Commented Feb 3, 2013 at 14:59

1 Answer 1

1

You may not be seeing any output from tail because the result of your arithmetic for the -n argument may be 0:

anew@Wintermute:work$ wc -l dev-configuration.json 
     105 dev-configuration.json
anew@Wintermute:work$ tail -n 0 dev-configuration.json 
anew@Wintermute:work$ 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes but it is not zero when i run the command in terminal, I thought this could have been the issue too so i set the sleep time to 1 sec but that still did not help.
Bummer. I'd still troubleshoot it by setting the tail -n 5 or a similar static value to validate that. I'd say that's your culprit if their haven't been any additions to your logfile. Throw an if/else in there if there's no change to the logfile size output a warning or something. Hope it helps.
I think I found the issue, the cgi scrip is running at the Apache user an it can't access the file. But I thought it would output that error, apparently not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.