Linux shell scripts integrated to Android, HTML programming and more
There's an endless amount of ways shell scripts may be used today. When they're integrated within a web server the audience could be huge. There are some benefits with using shell scripts over Perl, Python or any other perhaps some other more advanced translators: there's no version conflicts at all! Have you ever been fighting with the Python versions and mismatches between them? Me too and this is why I prefer simple shell scripts over those beasts. We'll illustrate some ideas what the scripts can do.
Shell scripts with Android
It's possible to run shell scripts on Android devices as well. They may be very useful as background processes collecting and analyzing logged data. We'll share a few examples. You need to run them with the sh unlike in normal Linux shell:
Shell scripts rebooting a server
The shell scripts have a lot of power. Imagine a situation in which your server services start to slow down or not working at all. A well constructed script can actually detect the situation and reboot the server for you! I spent some time with a thumbnail bot that captured thumbnails of any web page via a publix API. The service was free and it soon gained a lot of users. The problem was that occasionally, perhaps after every 500000th captured thumbnail the xserver died. Literally it didn't response to anything. I tried to debug it a bit but then decided that it's better just to reboot the machine automatically after such a low-probability event. The solution? A script that detects the situation when the Linux command 'xhost' no longer responds but the call blocks forever. One script periodically checks if such processes are in the system. If so, it shuts down and reboots the server. The script below is a part of a larger loop:
XCHECKS=`/bin/ps -A | /bin/grep "xhostcheck" | /usr/bin/wc -l` if [ $XCHECKS -gt 1 ]; then echo "FATAL: XHOST!" >> /var/www/monitorp.html killall gnome-web-photo /etc/init.d/apache2 stop reboot sleep 30 else /home/thumbuser/xhostcheck & fi
This script 'xhostcheck' is declared below. It's run in the background. If there are more than 1 such process we just reboot as seen in the script above :) The script below also issues a reboot if the xhost command returns an error.
#!/bin/sh # androidcodeexamples.com sleep 5 export DISPLAY=:0.0 export XAUTHORITY=/var/run/lightdm/root/:0 xhost 2>/dev/null >/dev/null xhost +local:thumbuser 2>/dev/null >/dev/null if [ $? -eq 1 ]; then echo "FATAL: REBOOT REASON: XHOST" >> /var/www/monitorp.html sleep 120 reboot sleep 60 fi
The scripts also appends the reason for the reboot into the www page 'monitorp.html'.