prupert
prupert

Reputation: 23

Bash script runs when debugged ( -x) but fails when run normally

I have a bash script that I run via cron to restart vlc (I use vlc to show the stream from an IP webcam I use as a baby monitor - the stream gets progressively delayed, so I run this script to kill all instances of vlc and restart it). This script ran fine in Ubuntu 11.04 for a year. I upgraded to Ubuntu 11.10 (oh, silly me) and now it only works correctly if I run it with the bash debugging option turned on (bash -x script.sh). If I run it without the -x option, the script successfully kills all instances of vlc, but then exits and fails to restart vlc.I have no idea why. Note it doesn't matter if I run it via or cron or direct on the commandline, without the -x option, it kills vlc but never runs vlc.

Here it is:

#!/bin/bash
DISPLAY=:0
export DISPLAY

VPID=( $(ps -e | grep vlc | awk '{print $1;}'))
if [ -n "$VPID" ];then
        while [ -n "$VPID" ];do
            kill $VPID
            VPID=( $(ps -e | grep vlc | awk '{print $1;}'))

        done
        vlc --volume=900 http://lily:@192.168.1.43/videostream.asf &
        sleep 10
        WID=( $(xwininfo -display :0 -name "http://192.168.1.43/videostream.asf - VLC media player" -int | awk '/Window id:/ {print $4}'))
        sleep 10
        xdotool windowmove --sync $WID 0 0
        xdotool windowsize --sync $WID 1600 1000
        exit
fi
exit

Upvotes: 1

Views: 448

Answers (1)

SiegeX
SiegeX

Reputation: 140427

  1. Why are you making VPID and WID arrays?
  2. Have you tried putting echo "after while loop" directly after the done in the while loop to see if your script ever falls out of the loop when not in debug mode?
  3. There is no need for explicit exit the way your script is written

Try the following:

#!/bin/bash
export DISPLAY=":0"

VPID=$(pgrep vlc)
if [[ -n "$VPID" ]]; then
  while [[ -n "$VPID" ]]; do
    kill $VPID
    VPID=$(pgrep vlc)
  done

  vlc --volume=900 http://lily:@192.168.1.43/videostream.asf &
  sleep 10
  WID=$(xwininfo -display :0 -name "http://192.168.1.43/videostream.asf - VLC media player" -int | awk '/Window id:/ {print $4}')
  sleep 10
  xdotool windowmove --sync "$WID" 0 0
  xdotool windowsize --sync "$WID" 1600 1000
fi

Upvotes: 1

Related Questions