Raspberry Pi camera module shell script

As a quick note today, this is the source code for a Raspberry Pi (RPI) camera module shell script that I created so a friend can turn her Raspberry Pi camera on and off from the RPI command line (i.e., the Linux command line):

#!/bin/bash

function checkIfCameraIsRunning() {
    ps auxw | grep raspistill | grep -v grep > /dev/null 2>&1
    echo $?
}

function stopCameraProcesses() {
    killall raspistill
    killall mjpg_streamer
}

function startCamera() {
    nohup mjpg-streamer-code-182/mjpg-streamer/run.sh 2> /dev/null &
}

function showMenu() {
    clear
    echo ""
    echo ""
    local res=`checkIfCameraIsRunning`
    if [ "$res" = "0" ]; then
        echo "I think the camera is running"
    else
        echo "I don't think the camera is running"
    fi
    echo ""
    echo "1) Start Camera"
    echo "2) Stop Camera"
    echo "3) Restart Camera"
    echo "q) Quit"
    echo ""
}

while true
do
    # (1) prompt user, and read command line argument
    showMenu
    read -p "Command: " answer

    # (2) handle the input we were given
    case $answer in
         [1] ) echo "stand by ..."
               startCamera
               sleep 2
               ;;
      
         [2] ) # STOP
               echo "stand by ..."
               stopCameraProcesses
               sleep 2
               ;;
      
         [3] ) # RESTART
               echo "stand by ..."
               stopCameraProcesses
               sleep 2
               startCamera
               sleep 2
               ;;
      
         [q] ) exit;;
      
         * )   echo "Please enter 1, 2, 3, or q please.";;
  
    esac
done

I’m not going to explain this code too much, but when you first run this script it shows a menu that looks like this:

I don't think the camera is running

1) Start Camera
2) Stop Camera
3) Restart Camera
q) Quit

Command:

The command to start the RPI camera module looks like this:

nohup mjpg-streamer-code-182/mjpg-streamer/run.sh 2> /dev/null &

Unfortunately I forgot to make a copy of that run.sh script so I can’t share those exact commands here, but I do know that it contained two main commands, one to start a long-running raspistill process, and a second command to run the mjpg_streamer process (that command is named that, or something like that).

Once that script is started you can access the mjpg_streamer output at a URL like this:

192.168.1.149:8080/javascript.html

I’ll add more details -- and a newer version of this script -- when I get a chance to work on this RPI system again. (I wrote a newer version of this script that lets you shutdown and reboot your computer using commands like sudo shutdown -h now and sudo reboot.)