0

I am trying to autostart a python script everytime it crashes on my raspberry pi. I am adhering to the shell script solution offered here: https://raspberrypi.stackexchange.com/questions/14735/how-do-i-restart-a-python-program-on-my-pi-when-it-crashes

In the root directory, I created a shell script with sudo nano constantrun.sh.

The contents of this shell script is currently (after taking into account changes offered in the comments and answers):

#!/bin/sh

COMMAND='python home/pi/projects/mypythonscript.py'
LOGFILE=restart.txt

writelog() {
  now=`date`
  echo "$now $*" >> $LOGFILE
}

writelog "Starting"
while true ; do
  $COMMAND
  writelog "Exited with status $?"
  writelog "Restarting"
done

I then ran sudo chmod +x constantrun.sh to make it executable.

Following that, I ran the script with sudo sh constantrun.sh successfully.

The script can now initialise. However, as my python script uses a mysqlconnector module, the error I recieve now is : Traceback (most recent call last): File "mypythonscript.py", line 8, in <module> import mysql.connector as mariadb ImportError: No module named mysql.connector

Using a file explorer, it appears that the mysql.connnector is found installed in home/pi/.local/lib/python2.7/site-packages/mysql_connector-‌​‌​2.1.6.dist-info

How can I resolve this error?

  • I use python mypythonscript.py on terminal – Craver2000 Dec 25 '17 at 1:54
  • The shell acts (almost) exactly the same when run interactively as in a script. Try calling the program the same way you normally would, and see what changes – Fox Dec 25 '17 at 1:57
  • Thanks Fox. Adding the quotes made a difference. I dont have the error as previously described but now I have: Traceback (most recent call last): File "mypythonscript.py", line 8, in <module> import mysql.connector as mariadb ImportError: No module named mysql.connector 'mysql.connector' is one of the modules used in this script. 'mypythonscript.py' works otherwise when I use bash commands python mypythonscript.py on terminal. I was thinking whether there could be something I could add in the shell script to enable the mysql.connector to be imported into the python script? – Craver2000 Dec 25 '17 at 2:38
  • @Fox. In terminal, if I run python mypythonscript.py (without sudo) at the directory where file is stored, it works without problems or the import error as described. What do you mean by checking the environment for these variables? – Craver2000 Dec 25 '17 at 3:06
  • Where/how is the mysql module installed? – multithr3at3d Dec 25 '17 at 3:23
0

You don't have x permission (for execution) on your mypythonscript.py. You need to use chmod for that:

chmod +x /home/pi/projects/mypythonscript.py

Also, you may need a shebang for your python script, for example:

#!/usr/bin/python3

| improve this answer | |
  • 1
    #!/usr/bin/env python3 will work on more systems than #!/usr/bin/python3, since installation in /usr/local, /opt, or other paths isn't uncommon – Fox Dec 25 '17 at 1:47
  • @林自均 I have ran chmod +x /home/pi/projects/mypythonscript.pyin terminal, as well as adding the shebang line into my python script (tried Fox's version as well). The error I still get on terminal is:pi@raspberrypi:~/projects $ sudo sh constantrun.sh constantrun.sh: 3: constantrun.sh: mypythonscript.py: not found – Craver2000 Dec 25 '17 at 1:59
  • Can you update 2 things to your main post: the content of current constantrun.sh and how you run your constantrun.sh? – johnlinp Dec 25 '17 at 7:31
  • @林自均 ok, updated – Craver2000 Dec 25 '17 at 12:04
0

The problem of running the shell script has now been resolved, thanks to the input of those who have commented/answered on my query. Chief among the helpful suggestions were Fox's suggestion to use a quotation at my command line of the shell script and the recommendation to reinstall the module required by my python script globally.

In summary, my shell script reads:

#!/bin/sh

COMMAND='python home/pi/projects/mypythonscript.py'
LOGFILE=restart.txt

writelog() {
  now=`date`
  echo "$now $*" >> $LOGFILE
}

writelog "Starting"
while true ; do
  $COMMAND
  writelog "Exited with status $?"
  writelog "Restarting"
done

I then ran sudo chmod +x constantrun.sh to make it executable. Following that, I ran the script with sudo sh constantrun.sh

I then encountered an importmodule error: ImportError: No module named mysql.connector

This was resolved by doing a pip uninstall of the mysql.connector. I then proceeded with a global install of mysql.connector with sudo pip install mysql-connector-python. My shell script could then be launched successfully with sudo sh constantrun.sh.

However, though it is working currently, I am not entirely sure if this method can indeed help in the long run, of restarting the script automatically if it crashes. I will monitor further to see if this method works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.