A way to check perl, python and other scripts that make websites

Share your awesome tips and tricks here.
Post Reply
MikeG.6.5
Posts: 917
youtube meble na wymiar Warszawa
Joined: Fri May 15, 2015 1:56 am

A way to check perl, python and other scripts that make websites

Post by MikeG.6.5 »

There really isn't a way to check if a script you have that makes a website on your NAS is working correctly, other than opening the site on your browser. If the script stops working, for whatever reason, in between your refreshes of the page, you have no way to tell it.

Using sh scripting, and the curl command, though, you can make an automatic script to check the operation of the website, and restart it, making an entry in the system log files that the script stopped. If you use AiMaster, you should get a prompt that the script has stopped, so you can go in and check if everything is working correctly.

I'm going to give a couple of example scripts here, one I've already put out in the Plex sub-forums and one that's new.

The old one first. this is checking if Plex Media Server is running:

Code: Select all

#!/bin/sh

curl -I -m 8 "http://localhost:32400/web/index.html"
if [ "$?" -ne "0" ]

then
     /usr/sbin/syslog --log 0 --level 0 --user admin --event "Remote Access to Plex is unavailable"
     /usr/local/AppCentral/plexmediaserver/CONTROL/start-stop.sh restart
fi

exit 0
The first part in the start #!/bin/sh is called a shebang, and then points to the path of the script language interpreter. We are running this as a sh or shell script, so we point it to that interpreter.

the next line is a built in command called curl. It looks at the website you have in it's arguments, to see if there is an output from the site. If so, it exits the script, if not, then it executes the lines between the then and fi. So, assume that Plex has stopped, and this script found it. The line that has "/usr/sbin/syslog ..." in it writes the error to the log file. The next line /usr/local/App Central/ ..." then calls the start-stop.sh script for Plex to have it restart. If you look in the Plex Forums here, you will see a few other things that need to be done to make that happen, though.

This next script is also driven towards Plex, but it calls a tool called PlexPy. PlexPy is a web front-end for a perl script called PlexWatch. Plex Watch looks at the Plex Media Server's log files and determines who, what and when your media has been streamed from the NAS. The PlexPy script just presents this data in a clean interface. Here's the script for checking it:

Code: Select all

#!/bin/sh

curl -I -m 8 "http://localhost:8181/home";
if [ "$?" -ne "0" ]

then
                /usr/sbin/syslog --log 0 --level 0 --user admin --event "PlexPy isn't running:  Starting it"                
                /volume1/misc/PlexPy/PlexPy.py -d
fi

exit 0
This looks at default port set up for PlexPy via it's install, and looks for web output, just like the other script does. Again it makes a log entry, and then makes the call to the proper script to start it again. In this case, it adds a -d on the end to run this script as a daemon, so you don't need to keep an ssh or terminal window open for the script to run.

These 2 scripts are called from a third script, listed here:

Code: Select all

#!/bin/sh

    /volume1/misc/CkPlexPy.sh
    sleep 5
    /volume1/misc/CkPlex.sh


exit 0
The sleep 5 line just puts a 5 second wait in between each of those scripts, to make sure they aren't walking on each other on a busy or slow system.

The last step to put this all together is editing the cron files for making things happen in certain time frames. Using WinSCP or an equivalent product, navigate to this file: /var/spool/cron/crontabs/root Root is a file, even though it has no extension. Just double-click it to open it in WinSCP's editor to see these lines: (I have added a few to make editing easier to keep track of in this file.)

Code: Select all

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
*/20 * * * * /volume1/misc/CkApps.sh
If you look at the last line, and the placement of the /20, it's saying every 20 minutes execute the script. So this checks if Plex, PlexPy and a few other things I didn't include, are running every 20 minutes, starting at on the hour, 20 after and 40 after. If you wanted to make this script run every 5 minutes, replace the /20 with /5. If you wanted it every 20th hour (or 8:00PM) then the line would be

Code: Select all

* */20 * * * /pathtoscript/scripthere
Anyone else have any neat automation tricks like these they want to share?
Post Reply

Return to “Tips & Tricks”