Process check script for cron
Process Check script for cron¶
This script will check process if it is not running and will start the process if not running.
First¶
File: proc_script.sh
#!/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 [email protected]
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:
File: proc_scriptjt.sh
#!/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 [email protected]
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: [email protected]
"""
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 [email protected]
## Actual CMDLINE output
## ssh-f-N-i/home/kpatel/.ssh/id_rsa-R2200:localhost:[email protected]
if proc.cmdline == (['ssh', '-f', '-N', '-i', '/home/kpatel/.ssh/id_rsa', '-R', '2200:localhost:22', '[email protected]']):
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', '[email protected]'])