Restart python script automatically even when it crashes in Linux

0 votes

I have a python program that has to be running all the time. If for some reason it was stopped I want to restart it automatically. I thought of having a cron that will run every n number of seconds and check the program is running. My shell script is looks like this:

#!/usr/bin/env bash
CM_COMMAND=`ps aux| grep abc| grep def| grep sudo`
LEN_COMMAND=${#CM_COMMAND}
if[["$LEN_COMMAND" -le "5"]] 
then
    echo "start the python program"
fi
exit

When I run this script I am getting the error: my_prog.sh: line 4: $'if[[118\r -le 5]]\r': command not found'

What is the alternative of doing this and what is the problem with my script?

Sep 11, 2018 in Python by bug_seeker
• 14,970 points
54 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Maybe this would be more robust?

1) save the PID of your process when you start it with:

{your_python_command} & echo $! >>/{some_folder}/your_app.pid

2) This script will check and restart if it can't find the PID..

#!/usr/bin/env bash

PID=`cat /{some_folder}/your_app.pid`

if ! ps -p $PID > /dev/null
then
  rm /{some_folder}/your_app.pid
  {your_python_command} & echo $! >>/{some_folder}/your_app.pid
fi

3) To add it to a cronjob:

crontab -e

choose your text editor and add this row at the end of the file:

*/1 * * * * /{your_path}/{your_script_name}

exit and save

(this will run the script every minute, check crontab manual to set your exact interval)

answered Sep 11, 2018 by Priyaj
• 56,120 points

Related Questions In Python

+4 votes
7 answers
0 votes
2 answers

What do you mean by python scripting? What is a script and a module in python?

A scripting language is a programming language ...READ MORE

answered Mar 14 in Python by rajesh kumar
1,688 views
0 votes
1 answer

Obtaining a value when given a key in python dicionaries

We cannot. Dictionaries aren't meant to be ...READ MORE

answered May 31, 2018 in Python by Nietzsche's daemon
• 4,260 points
14 views
0 votes
1 answer

When to use %r instead of %s in Python? [duplicate]

The %s specifier converts the object using ...READ MORE

answered Aug 2, 2018 in Python by bug_seeker
• 14,970 points
40 views
0 votes
1 answer

External command in Python

you can check the subprocess module in ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 9,660 points
7 views
0 votes
1 answer
+1 vote
2 answers

Execute Pig Script from Grunt Shell

From your current directory run  pig -x local Then ...READ MORE

answered Oct 25, 2018 in Big Data Hadoop by Kunal
474 views
0 votes
1 answer

How to stop messages from being displayed on spark console?

In your log4j.properties file you need to ...READ MORE

answered Apr 24, 2018 in Apache Spark by kurt_cobain
• 9,260 points
613 views
0 votes
1 answer

Restart python script automatically even when it crashes in Linux

Maybe this would be more robust? 1) save ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 56,120 points
89 views
0 votes
1 answer

How do play audio (playsound) in background of Python script?

In windows: Use winsound.SND_ASYNC to play them asynchronously import winsound winsound.PlaySound("filename", winsound.SND_ASYNC ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 56,120 points
1,678 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.