Skip to content

Part 2. Monitor a Process

jrbail01 edited this page Jan 6, 2017 · 9 revisions

Whenever you run a program or script on your Pi, you initiate a process. Each running process will have a unique process ID number (pid). You can see every process owned by you by typing the following at your Pi's command prompt:

$ ps

You will see a response that looks something like this

  PID TTY          TIME CMD
 3359 pts/0    00:00:01 bash
 3373 pts/0    00:00:00 ps

If you want to find every running python process, you can use the following command:

$ ps -ef | grep py

root      2287     1  0 Dec09 ?        00:00:00 sudo python /home/pi/weather/weather.py

In the example above, the pid of my weather Python script is 2287. Once we know the pid of the process we want to monitor, we can pass this number in as a parameter into a simple Python script that will monitor the existence of that process and send either a "Running" or "Exited" response to our dashboard.

We will use the Python script located at https://github.com/initialstate/pi-process-dashboard/blob/master/monitor_process.py. Before we can use this script, we need to set each of our user settings at the top of the file.

# --------- User Settings ---------
# Initial State settings
BUCKET_NAME = ":computer: Processes" 
BUCKET_KEY = "pr1208"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
PROCESS_NAME = "PLACE THE NAME OF YOUR PROCESS HERE"
# Set the time between checks
MINUTES_BETWEEN_READS = 15
# ---------------------------------
  • BUCKET_KEY specifies the data bucket our script will stream into. Make sure you use the same BUCKET_KEY for each script that should send data into the same dashboard.
  • ACCESS_KEY is your unique Initial State account key. This key is used to send data into your account. You can find your ACCESS_KEY in the Streaming Access Keys section of your Initial State account settings.
  • PROCESS_NAME is the name you want to display on your dashboard of the specific process that you are monitoring. For example, PROCESS_NAME = "Franklin Weather Station".

You can run this script as follows:

$ python monitor_process.py <pid>

To run this script in the background w/o interruption:

$ nohup python monitor_process.py <pid> &

Manually finding the pid is cumbersome. Let's create another simple script that will find the pid automatically and launch our monitor_process script.

<< Part 1: Initial State - Part 3: Launch a Monitored Process >>