WOL automatically for PLEX. Need help with syntax.
-
Hello all,
Hopefully someone can help me. I am trying to automate my PC to sleep when not needed. Starting it is simple for me, but I have several family members who use my Plex server and would not be interested in dealing with sending WOL etc.
I have made rules to tag traffic to the Plex server (TCP matching certain ports) and have confirmed that is working. It tags it either "PLEX LAN" or "PLEX WAN".
I started adapting a script that someone else used for Tomato. I haven't even been able to get to the point where I can attack the functionality (which I am just hoping works at this point) because when I try to run the script as a shell command I get
[2.4.2-RELEASE][root@pfSense.localdomain]/usr/local/etc/rc.d: sh WOLplex.sh
: not found
: not found
: not found
: not found
WOLplex.sh: 38: Syntax error: "done" unexpected (expecting "then")I don't think I have any further "then" statements. I tried finishing my script with "end" instead of "done" and it tells me it didn't expect the end of file.
Here is my script:
#!/bin/sh
TARGET=xxx.xxx.xxx.xxx
MAC=xx:xx:xx:xx:xx:xx
PORTS="(xxxxx)"INTERVAL=2
NUMP=1OLD_LC=
wc -l /var/log/messages | awk '{print $1}'
while sleep $INTERVAL
do
# Only care about new lines since the script last ran
LC=wc -l /var/log/messages | awk '{print $1}'
NEWLINES=expr $LC - $OLD_LC
if [ "$LC" -ne "$OLD_LC" ]
then
# Could handle WAN vs LAN different if I wanted. Just do the same thing on either for now…
LINE=tail -$NEWLINES /var/log/messages | egrep "PLEX .* DST=$TARGET .* DPT=$PORTS" | tail -1
SRC=echo $LINE | awk '{print $10}' | sed -e "s/SRC=//g"
if [ "$SRC" != "" ]
then
# Found a matching line. Try to ping the server
RET=ping -c $NUMP -W 1 $TARGET 2> /dev/null | awk '/packets received/ {print $4}'
if [ "$RET" -ne "$NUMP" ]
then
# Guess it's sleeping. Send WoL.
echo "[date -Iseconds
] $SRC causes WOL. Line was $LINE" >> /var/log/wol
/usr/sbin/ether-wake $MAC
# Could sleep for 20 minutes I guess…I mean, there's no real reason to check again.
# Whatever...10 seconds is fine.
sleep 10
fi
fi
fi
OLD_LC=wc -l /var/log/messages | awk '{print $1}'
doneLastly I am trying to run it using:
sh WOLplex.shI get "Command not found." if I use:
./WOLplex.shAny help is greatly appreciated. I have had decent luck with PFsense, and I consider myself good at understanding pseudocode, but I am not fluen in programming (especially shell scripting) so if I am doing something super stupid just point me in the right direction.
Thanks.
-
Bump.
No one has written a shell script and can help?
-
Read your own script ;)
I would check every line, from top to bottom - trying them out all by 'hand' before even thinking of using them in a script.
This line :OLD_LC=`wc -l /var/log/messages | awk '{print $1}'`
A very first question will be : does the file " /var/log/messages " actually exist on pfSense ?
I guess not - it doesn't exists on my pfSense installation. (and I also know that most log files are have a binary format, for that matter, so counting words in such a file has no sens)
pfSEnse is replying :wc: /var/log/messages: open: No such file or directory
which is correct.
So, counting words (the wc command - does exists on pgFense) in a non-existing file will not work.
edit :
The scripts uses a command called "ether-wake"[2.4.2-RELEASE][admin@pfsense.brit-hotel-fumel.net]/root: /usr/sbin/ether-wake /usr/sbin/ether-wake: Command not found.
Doesn't exist on pfSense.
I guess you are trying to adapt a script that was written for some kind of Linux bases system on pfSense, which is based on FreeBSD.
Btw : pfSense does know how to "wol" devices. Up to you to find the command ;)I didn't check other lines.
-
What is the cost savings you think you will get from this? I just never understand complexing up something that works for a what a $ or 2 a month?
-
Read your own script ;)
I would check every line, from top to bottom - trying them out all by 'hand' before even thinking of using them in a script.
This line :OLD_LC=`wc -l /var/log/messages | awk '{print $1}'`
A very first question will be : does the file " /var/log/messages " actually exist on pfSense ?
I guess not - it doesn't exists on my pfSense installation. (and I also know that most log files are have a binary format, for that matter, so counting words in such a file has no sens)
pfSEnse is replying :wc: /var/log/messages: open: No such file or directory
which is correct.
So, counting words (the wc command - does exists on pgFense) in a non-existing file will not work.
edit :
The scripts uses a command called "ether-wake"[2.4.2-RELEASE][admin@pfsense.brit-hotel-fumel.net]/root: /usr/sbin/ether-wake /usr/sbin/ether-wake: Command not found.
Doesn't exist on pfSense.
I guess you are trying to adapt a script that was written for some kind of Linux bases system on pfSense, which is based on FreeBSD.
Btw : pfSense does know how to "wol" devices. Up to you to find the command ;)I didn't check other lines.
Thanks for the help. I had already found the correct WOL command, just hadn’t got to that point. Didn’t know if I needed to change it so I was waiting.
I guess I need to find where the Plex tagged items are being logged by Pfsense and find a way to parse them out. Then I could probably use word count to determine if there is a new hit.
I’m not positive if that will fix the “done unexpected” error. That error was throwing me, I expected the Linux garbage before that to be the part that errored out.
I don’t know how much energy I will save but this is my gaming pc so it’s not an energy efficient setup as is.
-
Just did the math to sanity check whether this was worth it. Using 120W on, 5W sleeping, and 14 hours of “sleep-able” time per day (all low estimates) this would save $7.80 a month.
That to me is justifiable.
-
Lets say your gaming rig uses 60W idle.. When is plex going to do its background maint? This is normally done after hours, etc. this is when it can work on transcoding all that stuff in the background.. Creating all the Video thumbnails so where you can see where your at when you fast forward/rewind a movie. Are you not using this feature?
https://support.plex.tv/articles/201553286-scheduled-tasks/
But lets say you just shut it down and lets say your plex box is now off 12 hours of the 24.. To 60Watts 24/7 for a month your talking at 12 cents per Kwh $5.40 for the month… So if you can shut it down for 12 hours your talking about saving a 2.70 a month... So less than a cup of coffee.. Does your rig even idle at 60w... Why don't you put a killawatt meter on it, or do you not have a ups that shows you draw?
lets say its it would be off 18 hours of the day.. Wow $4 savings... Save that up for a year and you might be able to order a couple of pizza's and six pack of beer ;) heheeh You really need to understand how much juice your box is drawing idle to know if going to be worth all the effort of putting it to sleep and scripting some sort of wol, etc. How much do you pay for elec?? Im in Chicago and its high to be honest about 15.8 cents per kwh... I believe the national average is like 13 cents per kwh..
Why don't you just have your fiends using your plex buy you a beer or 2 now and then to cover the extra cost of the electric to leave it on 24/7 This way the thing can actually do the maint it is suppose to be doing off hours, etc. let alone the PITA its going to be when the thing doesn't want to come out of standby/hibernate when someone wants to watch something. Or for that matter the extra time involved in having to wait for it to spin up when it works correctly ;)
-
Lets say your gaming rig uses 60W idle.. When is plex going to do its background maint? This is normally done after hours, etc. this is when it can work on transcoding all that stuff in the background.. Creating all the Video thumbnails so where you can see where your at when you fast forward/rewind a movie. Are you not using this feature?
https://support.plex.tv/articles/201553286-scheduled-tasks/
But lets say you just shut it down and lets say your plex box is now off 12 hours of the 24.. To 60Watts 24/7 for a month your talking at 12 cents per Kwh $5.40 for the month… So if you can shut it down for 12 hours your talking about saving a 2.70 a month... So less than a cup of coffee.. Does your rig even idle at 60w... Why don't you put a killawatt meter on it, or do you not have a ups that shows you draw?
lets say its it would be off 18 hours of the day.. Wow $4 savings... Save that up for a year and you might be able to order a couple of pizza's and six pack of beer ;) heheeh You really need to understand how much juice your box is drawing idle to know if going to be worth all the effort of putting it to sleep and scripting some sort of wol, etc. How much do you pay for elec?? Im in Chicago and its high to be honest about 15.8 cents per kwh... I believe the national average is like 13 cents per kwh..
Why don't you just have your fiends using your plex buy you a beer or 2 now and then to cover the extra cost of the electric to leave it on 24/7 This way the thing can actually do the maint it is suppose to be doing off hours, etc. let alone the PITA its going to be when the thing doesn't want to come out of standby/hibernate when someone wants to watch something. Or for that matter the extra time involved in having to wait for it to spin up when it works correctly ;)
All valid points. My energy cost is about 15.8 cents per kWh. Also my library is fairly “static” and since I have a powerful pc I don’t transcode ahead of time. So it really doesn’t do much during the maintenance. Not using the thumbnail feature.
I don’t have a power meter unfortunately but I am under the impression that the difference between idle and sleep is bigger than that. If I can get my hands on a meter that would certainly be interesting.
All that being said my primary reason for this is to tinker. If it doesn’t work out, whatever.
It looks like I can use clog to make the log files readable by grep, then just compare the output to the previous one to see when there is a new hit.
-
Yeah I love to tinker myself.. The thing I found with putting pcs to sleep - is they more often than not have issues coming out of it.. So just when I want to do something, the F'ing thing decides to have a fit about coming out of standby/hibernate..
It normally not worth it..
So you do not use thumbnails - shame that is one of the best features… Now its more like netflix and you can tell exactly where your at when you fastforward/rewind. Did you take a look at all the other stuff it downs on scheduled tasks.. Optimize the database, metadata info, etc..
Problem people have is they think oh my PC has 1000W power supply in it.. Doesn't mean its using anything close to that.. My PC sits idle at like 45 watts.. The 2 monitors are what use more juice.. So just took a look at my ups records.. Now keep in mind that I have quite a bit of stuff running off this ups.. My PC, my sg-4860, my sg300-28 switch. 4 different raspberry pi's... My 2 monitors, alexa dot, speakers... This is total usage not just idle.. So in the month of dec used 100Kwh.. At 16 cents per kwh your talking 16$ My PC is small portion of that.. The few bucks going to save putting my pc to sleep just not worth it.. If you want to tinker I would suggest you tinker towards what your currently using so you will understand if worth the time and effort to put your stuff in standby or buy more power friendly devices, etc.
Killawatt meter great tool to have in your tool belt when looking to see how much juice your stuff is drawing now, when idle over X number of days, etc..
This the one I have https://www.amazon.com/P3-P4400-Electricity-Usage-Monitor/dp/B00009MDBU
And then the attached is from running business software for my cyberpower ups..
-
….
It looks like I can use clog to make the log files readable by grep, ….CO2 or not, you have a scripting future ;)
-
Well johnpoz, you convinced me. I set up Preview thumbnails. I had been putting it off because I didn't want to eat up my OS drive, but I used a symlink to put that stuff on a data drive and started it up.
I also got a smartthings plug for a separate project and it will report power draw. I plugged my PC into that and as I suspected I am drawing about 160 W at idle. I draw 8.5 during sleep. Using those numbers and 10 hours of on time per day (which is probably a high estimate) I see that I can save $10 a month. That is not insignificant. I agree waking computers from sleep is occasionally troublesome, but for $10 a month I will try it.
I know $120 a year almost makes it seem worth it to get a more efficient Plex server. But TBH my computer (Dell T-5500) was free (I have upgraded it, but most parts are original) and I can use it for Plex, game on it, and do movie editing etc super quick.
Now to figure out this script…
-
Here is my script now. I am still getting errors, but I think this is better. The clog | grep part works if I type it into the shell, but it doesn't run as a script.
#!/bin/sh TARGET='xxx.xxx.xxx.xxx' MAC='xx:xx:xx:xx:xx:xx' INTERVAL=2 NUMP=1 OLD_LINE=clog /var/log/filter.log | grep "1497674444" | tail -1 echo "$OLD_LINE" while sleep $INTERVAL do # Only care about new lines since the script last ran NEW_LINE=clog /var/log/filter.log | grep "1497674444" | tail -1 if [ "$NEW_LINE" -ne "$OLD_LINE" ] then echo "NEW TRAFFIC" RET=`ping -c $NUMP -W 1 $TARGET 2> /dev/null | awk '/packets received/ {print $4}'` if [ "$RET" -ne "$NUMP" ] then # Guess it's sleeping. Send WoL. echo "[`date -Iseconds`] WOL. Line was $NEW_LINE" >> /var/log/wol wol -i $TARGET $MAC # Could sleep for 20 minutes I guess...I mean, there's no real reason to check again. # Whatever...10 seconds is fine. sleep 1200 fi fi fi OLD_LINE='clog /var/log/filter.log | grep "1497674444" | tail -1' done
Here is the errors I get
[2.4.2-RELEASE][root@pfSense.localdomain]/usr/local/etc/rc.d: sh WOLplex.sh : not found : not found : not found WOLplex.sh: /var/log/filter.log: Permission denied tail: illegal option -- -1 WOLplex.sh: 31: Syntax error: "done" unexpected (expecting "then")
Note I cannot execute WOLplex.sh, or ./WOLplex.sh. I bet this might be something simple about permissions, but unfortunately I skipped all the basics so I don't know what I'm missing.
-
Try this :
#!/bin/sh OLD_LINE=`clog /var/log/filter.log | grep "1497674444" | tail -1` echo "$OLD_LINE"
-
"drawing about 160 W at idle"
Wow that is running hot… What is it exactly? That was idle - the cpu not doing anything... Or were you mining crypto while it wasn't doing anything ;) hehehe
What does it draw when actually doing something like transcoding a movie or 2?
I did a quick google for T5500 and looks like this one is only drawing 93w idle?
https://www.youtube.com/watch?v=7T4kMcpJVFc -
"drawing about 160 W at idle"
Wow that is running hot… What is it exactly? That was idle - the cpu not doing anything... Or were you mining crypto while it wasn't doing anything ;) hehehe
What does it draw when actually doing something like transcoding a movie or 2?
I did a quick google for T5500 and looks like this one is only drawing 93w idle?
https://www.youtube.com/watch?v=7T4kMcpJVFcI looked at that video, he only has one processor. Mine has two.
It was at idle. I think it's just older server hardware, not designed for idle efficiency.
-
IT WORKS! (mostly…)
Had to fix a lot of syntax (shellcheck.net helped a ton) and switch my line break style from windows to unix.
#!/bin/sh TARGET='192.168.1.200' MAC='00:25:64:b8:33:85' INTERVAL=2 NUMP=1 OLD_LINE=$( clog /var/log/filter.log | grep "1497674444" | tail -1 ) echo "Found this $OLD_LINE" while true do # Only care about new lines since the script last ran NEW_LINE=$( clog /var/log/filter.log | grep "1497674444" | tail -1 ) echo "New $NEW_LINE" if [ "$NEW_LINE" != "$OLD_LINE" ] then echo "NEW TRAFFIC" RET=$( ping -c 1 -W 1 192.168.1.200 | awk '/packets received/ {print $4}' ) if [ "$RET" -ne "$NUMP" ] then # Guess it's sleeping. Send WoL. echo "WOL. Line was $NEW_LINE" >> /var/log/wol wol -i $TARGET $MAC # Could sleep for 20 minutes I guess...I mean, there's no real reason to check again. # Whatever...10 seconds is fine. sleep 1200 fi fi echo "WAITING" OLD_LINE=$( clog /var/log/filter.log | grep "1497674444" | tail -1 ) sleep $INTERVAL echo "Old $OLD_LINE" done
It works exactly like it should and wakes my computer no problems when accessed from WAN. Plex is ready in about 10 seconds. I plan to do some other automation (send WOL packet when the living room tv is turned on) so that we don't see that 10 second wait usually.
When I try to access Plex from a device on my network it doesn't work, and double checking it does not appear I have a firewall rule that is tagging that traffic effectively.
Any ideas? Right now I am looking for LAN traffic with a destination port of 32400-32450 which should capture all the ports. I get my LAN/WAN, source/destinations mixed up all the time though.
-
why would you have a range of ports? Do you have 50 plex servers? Plex by default is port 32400, that is the only port it would be listening on. You get what mixed up source/dest?
-
I just re-read my last post and it is not even close to clear, so I apologize for that.
I read somewhere that Plex used 32410-32414 or so for network discovery, I think. I figured I would just set a range and catch it all in there.
Traffic from a device on my local network to my Plex device would be going to that port as the destination on LAN correct?
If both these devices are on the same switch, they might be communicating directly there. Would the router see that traffic for the purposes of firewall rules? I would think so, especially if the Plex server is sleeping and the client has to "look" for it.
-
If you have a network 192.168.x/24 lets call it - and your on 192.168.x.100 and plex is on 192.168.x.101 - no pfsense would never see that traffic. The only thing plex might have to do is resolve plex.whatever.tld your using to 192.168.x.101
Yes if your .100 box is talking to your .101 plex server the dest would be 32400.. That is IT.. and the source would be whatever random high port your client is using for that session.. Something above 1024 and below 65515..
The only time pfsense would be involved in the traffic is if it was routing it.. so clients on 192.168.x/24 while your plex is on 192.168.y/24