Tuesday, October 17, 2017

Check Running Process

What we'd like to do here is to check for the presence of a running process. If present, do x (echo Running). If not, do y (restart said process).

First step is typically done by running ps and doing a grep on the output for the process name (ps -ef|grep process). This merely checks for presence and not the state of the process. In the case of no match, grep will still match the original grep command, thus reporting a hit. To get around this, do an inverse-match for grep (ps -ef|grep -v grep|grep process). Another option is to use pgrep.

So you'd have something like this:
#!/bin/sh

if ps ax | grep -v grep | grep process > /dev/null
then
    echo "Running"
else
    echo "Not running. Starting."
    /opt/process/bin/process restart
fi

If you plan to run the script as a cron job, best to silence all output (/opt/process/bin/process restart &> /dev/null).
Or better yet, run the check commands as the cron job itself (every 15 minutes):
*/15 * * * * pgrep process > /dev/null || /opt/process/bin/process restart &> /dev/null

Cron, Cron Jobs, and Crontab

Cron in a Linux/Unix utility/daemon that runs tasks (cron jobs) in the background at regular intervals.
A crontab (cron table) is basically a file that contains the schedule of cron jobs to be run at specific times.

  • crontab -l to list the crontab entries
  • crontab -e to edit the crontab file
  • crontab -r to remove your crontab file

Cron entries use the following syntax:
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6, where Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Entry: Minute when the process will be started [0-60]
Entry: Hour when the process will be started [0-23]
Entry: Day of the month when the process will be started [1-28/29/30/31]
Entry: Month of the year when the process will be started [1-12]
Entry: Weekday when the process will be started [0-6] [0 is Sunday]

* in the minute field means command will be executed every minute.
*/10 means command will be executed every minute. This repeat pattern is not supported by all OS, so better to just use 0,10,20,30,40,50.
You can also use "-" to represent a range of values. For example, 0 1-2 * * * means command will be run at 1am and 2am only.
For readability, you can also use sun, mon, tue, wed, thu, fri, or sat for day of the week. 7 is also acceptable for Sunday.

After editing the crontab, remember to run "service crond reload" to take the new jobs into effect.

Not sure about your crontab syntax? Try crontab.guru.

Friday, October 13, 2017

STDIN, STDOUT, and STDERR

Have started scripting again, and have forgotten all about stdin, stdout, and stderr - and the various syntaxes. Time for a refresher.

Ok, so if I was to do an ELI5:
Every process is initialized with three open file-descriptors: stdin (standard input), stdout (standard output), and stderr (standard error).

  • stdin (file handle 0) is where the process gets input from you (usually from the keyboard or from pipes)
  • stdout (file handle 1) is where process output is displayed (usually sent to the console or a file or a pipe)
  • stderr (file handle 2) is basically stdout for errors (usually to stdout unless redirected elsewhere)

Let's take this for example:
myscript <infile 2>errfile

  • A process is created for myscript.
  • infile is opened as standard input for myscript (file handle 0)
  • errfile is opened as standard error (file handle 2)
  • myscript output is sent to /dev/tty by default (file handle 1)

Separating stderr from stdout is a great idea when you know command execution will result in errors, and you don't want them cluttering up valid entries in your stdout. Great example is when you're grepping for something. Most of the time, you get one or two hits, yet stdout is filled with "Permission denied" entries for protected directories. What to do?

grep needle /home/haystack/* > results.txt
Valid results redirected to results.txt; bad results displayed on the console.

grep needle /home/haystack/* 2> errors.log
This time, stderr (2) is redirected to errors.log, while valid results are displayed on the console.

grep needle /home/haystack/* &> output.txt
With this, everything (stdout and stderr) is redirected to output.txt, with nothing sent to tty.

If you want all results displayed on screen and saved on file, pipe them to tee:
grep needle /home/haystack/* |& tee output.txt

If your version of bash is giving you a syntax error, try this less elegant version:
grep needle /home/haystack/* 2>&1 | tee output.txt
This basically redirects stderr (2) to stdout (1).
1>&2 redirects stdout (1) to stderr (2).

What if you simply do:
grep needle /home/haystack/*  | tee results.txt
You get stdout on the screen and in the file, but the console will also display stderr.

Note that the above examples will overwrite if the file exists. Use >> and tee -a to append.

Wednesday, October 4, 2017

Xiaomi Xiaofang 1080P Wireless IP Camera Review

Found some time to finally sit down and write a review of the Xiaomi Xiaofang WiFi IP camera. The camera itself is a glossy white 50mm plastic cube - lens assembly in front, speaker, status LED and USB ports (full-size and micro-USB) at the back, and microSD slot, air vent and reset button at the bottom. The articulated arm and swivel base adds another 6mm to the height. Weight is 100g.

For the price, I'm very impressed with what it supposedly can do.

  • 1080P (1920x1080) full HD resolution video feed via the f2.0 aperture 110-degree lens. If you position two Xiaofangs together, the app supposedly creates a 220-degree panorama.
  • The lens assembly also has an IR-cut double filter with 2 infrared LEDs, which provides night vision up to 9 meters. (You can hear a click when the filter cuts in.)
  • The Mi Home app for the Xiaofang has a Speak button, which enables the intercom functionality.
  • The camera also come with smoke and CO alarm monitor function. Basically, it uses sound recognition to detect that the smoke or CO alarm has triggered, and then sends you a notification.
  • The camera can be configured to record video constantly onto the microSD card (64GB max) or only when motion is detected. Taking of timelapse photos also supported. The videos are also synced to Xiaomi's cloud storage (free!), so you can access/view them remotely.
  • While watching the realtime video feed, you can take stills or record video.
  • Network connection is via WiFi (2.4GHz 802.11 b/g/n). Remote access via WiFi or cellular network.
  • The camera requires 5VDC 1A and is powered via the microSD port. Given that USB ports are almost everywhere (heck, you can even use powerbanks), that opens up options where the Xiaofang can be installed. Add that magnetic base into the mix, the options are limitless!

Initial setup is a bit tricky though, if you don't understand Chinese/Mandarin.
  1. First off, download the Mi Home app from the Google Playstore. The retail box comes with a QR code, which should give you the link, or just do a search from within the Playstore.
  2. Set your location/locale to Mainland China, or the camera will not work. Apparently, it's not supposed to be used outside of China.
  3. Log in to your Xiaomi account, or create one if you don't have one yet.
  4. Click on the "+" icon to Add Device. Select the Xiaomi Xiaofang from the list. Note that you might see two entries. Choose the one with the shorter title, as the other one is for WiFi Direct mode.
  5. Apply power to the camera.
  6. Long press the setup button at the bottom, until you hear a voice prompt, which says/means "Waiting for connection".
  7. At this point, enter the credentials for the WiFi network that the camera is supposed to connect to using the Mi Home app.
  8. Point the camera to the Mi Home, which should now be showing a QR code. Another voice prompt will indicate successful connection.

Note that the camera's default setting is to auto-record. Might want to turn that off.

Been playing with the camera for a couple of days now, and so far so good. Most of the features work as advertised - speak, audio, full screen, look back, timelapse, night vision, and dormancy. There are a few times when I cannot connect to the device, but it's easily fixed with a power reset. Not so convenient when you're in a remote location.

Motion detection is a bit sensitive. The clouds would pass by, changing the sunlight patterns on the blinds, and that's enough to trigger motion detection. Downloading videos also take a while, probably because they're coming from some server in China. Motion-detect videos seems to be limited to 16 seconds.

The camera comes with firmware version 3.0.3.56. The app has been nagging me to upgrade to 3.2.0.30, which I'm holding off, as there are reports that after upgrade:

  • IR LEDs do not turn off even during daytime
  • Camera not working when used outside China (geoblocked?)
  • microSD card not detected

There is an option to lock down access the camera with a PIN password or fingerprint, but not sure how secure that really is. *cough* backdoor *cough*

For those who don't want the video feeds going to China, there is an option to apply some hacks via the microSD card to disable cloud service and stream via RTPS. That said, some of the functionalities become broken. Installation instructions here.

All in all, a neat little WiFi security camera.