This shows you the differences between two versions of the page.
process_check_script_for_cron [2012/04/06 15:13] k2patel [Third] |
process_check_script_for_cron [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Process Check script for cron ==== | ||
- | This script will check process if it is not running | ||
- | and will start the process if not running. | ||
- | |||
- | ==== First ==== | ||
- | |||
- | <code bash | 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 k2patel@71.245.248.138 | ||
- | echo "Process not running - Started" | ||
- | else | ||
- | echo “Process Running - do nothing” | ||
- | fi | ||
- | </code> | ||
- | |||
- | ==== Second ==== | ||
- | |||
- | I made some changes as there is actually no need to equality check: | ||
- | |||
- | |||
- | <code bash | 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 k2patel@71.245.248.138 | ||
- | echo "Process not running - Started" | ||
- | else | ||
- | echo “Process Running - do nothing” | ||
- | fi | ||
- | </code> | ||
- | |||
- | ==== Third ==== | ||
- | |||
- | |||
- | This is in python :) | ||
- | |||
- | <code 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']) | ||
- | | ||
- | </code> |