==== Process Check script for cron ====
This script will check process if it is not running
and will start the process if not running.
==== First ====
#!/bin/bash
# check process
ps -ef | grep -v grep | grep 2200:localhost:22
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
ssh -f -N -R 2200:localhost:22 k2patel@71.245.248.138
echo "Process not running - Started"
else
echo “Process Running - do nothing”
fi
==== Second ====
I made some changes as there is actually no need to equality check:
#!/bin/bash
# check process
# if can parse the statement and watch the exit code
if [ "$(ps -ef | grep -v grep | grep 2200:localhost:22)" ]
then
ssh -f -N -R 2200:localhost:22 k2patel@71.245.248.138
echo "Process not running - Started"
else
echo “Process Running - do nothing”
fi
==== Third ====
This is in python :)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 4 14:46:52 2011
@author: k2patel@hotmail.com
"""
import subprocess
import psutil
# reset running variable
running = 0
for proc in psutil.get_process_list():
if proc.username == 'kpatel':
## Actual Command
## ssh -f -N -i /home/kpatel/.ssh/id_rsa -R 2200:localhost:22 k2patel@71.245.248.138
## Actual CMDLINE output
## ssh-f-N-i/home/kpatel/.ssh/id_rsa-R2200:localhost:22k2patel@71.245.248.138
if proc.cmdline == (['ssh', '-f', '-N', '-i', '/home/kpatel/.ssh/id_rsa', '-R', '2200:localhost:22', 'k2patel@71.245.248.138']):
running = 1
if running is 1:
print "Process is running"
else:
subprocess.call(['ssh', '-f', '-N', '-i', '/home/kpatel/.ssh/id_rsa', '-R', '2200:localhost:22', 'k2patel@71.245.248.138'])