A lil help with grep

I plan on moving my server to ubuntu server from windows ... in my testing the ubuntu has performed way better than windows. Everything is setup on ubuntu just finalizing the maintenance jobs I use.

I want to be able to run a job on ubuntu only if grep comes back ok.

I want to execute the move below only if the dvr is not busy ,,, using curl http://10.0.0.106:8089/dvr | grep "busy\":false," to find when the dvr is not busy.

mv "/media/storage1/public/dvr/The Equalizer" "/media/storage2/public/dvr"

curl http://10.0.0.106:8089/dvr | grep "busy\":false" &&
mv "/media/storage1/public/dvr/The Equalizer" "/media/storage2/public/dvr"

The && construct will only execute the following command if the previous one returns true. (Conversely, the || (double pipe) construct will only execute the following command if the previous was false. They are the logical and/or for process execution.)

1 Like

Thanks a lot.

Alternatively:

#!/bin/bash
set -e
if curl -s http://10.0.1.106:8089/dvr | grep -q "busy\": false"; then
  mv ...
fi

Just an FYI the mv command only runs once as it tries to recreate the destination directory again and fails.

Instead I use cp and rm -rf the source directory after successful cp

You're probably better off using rsync. I was simply sharing how to execute a command if the previous one was successful.

If you're looking for a proper and robust way to transfer files, you should look into solutions that do things such as verify your sources make it to the destination before removing them, as well as ensuring permissions are intact; but that was not what you asked.

(Actual research and knowledge is always superior to blind copypasta.)

I am not criticizing you or your post just posting my findings for those that might be trying to do the same thing... I appreciate your input if I came off as criticizing you I apologize. Everything is working great now thanks to your input.

The reason I am doing this is that I record to an SSD and it performs much better when recording many streams and running comskip ... I then move to my attached 10 TB USB3 drive when there is no activity in the DVR.

2 Likes